<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Appointment_model');
        $this->load->model('Available_model');
    }

    public function index()
    {
        $this->load->view('login');
    }

    public function login()
    {
        $post = $this->input->post(null, true);
        $user = $post['user'];
        $pass = $post['pass'];

        if ($user == 'adminadmin' && $pass == "12345678") {
            
            $newdata = array(
                'username'  => 'admin',
                'logged_in' => TRUE
            );
        
            $this->session->set_userdata($newdata);  
            redirect('admin/appointments');          

        } else {
            $this->session->set_flashdata('error', 'Usuario no existe o contraseña incorrecta');
            redirect('admin');
        }
    }

    public function logout()
    {
        $this->session->sess_destroy();
        redirect('admin');
    }

    public function appointments()
    {
        if (!$this->session->logged_in){
            redirect('admin');
        }
        $data['appointments'] = null;
        $data['search_date'] = null;

        $this->load->view('appointments', $data);
    }

    public function search_appointment()
    {
        if (!$this->session->logged_in){
            redirect('admin');
        }

        $post = $this->input->post(null, true);
        $appointments = Appointment_model::where('request_date', $post['search_date'])
            ->orderBy('request_time', 'ASC')->get();
        
        $date_appointments = array();

        foreach ($appointments as $appointment) {
            $date_appointments[$appointment->request_time][] = array(
                'name' => $appointment->parent,
                'email' => $appointment->email,
                'phone' => $appointment->phone,
                'request_date' => $appointment->request_date,
                'request_time' => $appointment->request_time,
            );        
        }

        $data['appointments'] = $date_appointments;
        $data['search_date'] = $post['search_date'];
        $this->load->view('appointments', $data);
    }

    public function available() 
    {
        if (!$this->session->logged_in){
            redirect('admin');
        }
        $available = Available_model::first();

        $data['start_date'] = date('Y-m-d', strtotime($available->start_date));
        $data['end_date'] = date('Y-m-d', strtotime($available->end_date));
        $data['start_date_m'] = date('Y-m-d', strtotime($available->start_date_m));
        $data['end_date_m'] = date('Y-m-d', strtotime($available->end_date_m));

        $this->load->view('available', $data);
    }

    public function available_update()
    {
        $post = $this->input->post(null, true);

        $available = Available_model::first();
        $available->start_date = $post['start_date'];
        $available->end_date = $post['end_date'];
        $available->start_date_m = $post['start_date_m'];
        $available->end_date_m = $post['end_date_m'];

        if ($available->save()) {
			$this->session->set_flashdata('success', 'Los datos se han actualizado');
			
		} else {
			$this->session->set_flashdata('error', 'Los datos no se han podido actualizar');
         }
         
         redirect('admin/available');
    }
}