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

class Reserva extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Reserva_model');
        $this->load->model('Usuario_model');
        $this->load->model('Amenidad_model');

    } 

    /*
     * Listing of reservas
     */
    function index($page = FALSE)
    {
        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };

        $breadcrumb = array(
            "Reservas" => ""
        );
        $data['breadcrumb'] = $breadcrumb;

        $params['page'] = $page;
        $params['limit'] = 5; 
        $params['offset'] = $page ? (($page - 1) * $params['limit']) : 0;
        
        $config = $this->config->item('pagination');
        $config['base_url'] = site_url('reserva/page');
        $config['total_rows'] = $this->Reserva_model->get_all_reservas_count();
        $config['per_page'] = $params['limit'];
        $config['use_page_numbers'] = TRUE;
        $config['first_url'] = base_url('reserva');
        $config['attributes'] = array('class' => 'page-link');
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li class="page-item">';
        $config['num_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li class="page-item">';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li class="page-item">';
        $config['next_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li class="page-item">';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li class="page-item">';
        $config['last_tag_close'] = '</li>';
        $config['first_link'] = "<<";
        $config['last_link'] = ">>";
        $this->pagination->initialize($config);

        $data['listado'] = FALSE;
        $data['controlador'] = 'reserva';
        $data['reservas'] = $this->Reserva_model->get_all_reservas($params);
        
        $data['contenido'] = 'reserva/index';
        $data['titulo'] = 'Reservas de Amenidades';
        $data['_view'] = 'reserva/index';
        $this->load->view('templates/template_admin', $data);
    }

    /*
     * Adding a new reserva
     */
    function add()
    {   
        $this->load->library('form_validation');

		$this->form_validation->set_rules('costo','Costo','required');
		$this->form_validation->set_rules('id_amenidad','Id Amenidad','required');
		$this->form_validation->set_rules('day','Day','required');
		
		if($this->form_validation->run())     
        {   
            $params = array(
				'month' => $this->input->post('month'),
				'id_autor' => $this->input->post('id_autor'),
				'id_amenidad' => $this->input->post('id_amenidad'),
				'costo' => $this->input->post('costo'),
				'estado' => $this->input->post('estado'),
				'day' => $this->input->post('day'),
				'year' => $this->input->post('year'),
				'id_condominio' => $this->input->post('id_condominio'),
				'fecha_registro' => $this->input->post('fecha_registro'),
				'fecha_modificacion' => $this->input->post('fecha_modificacion'),
				'hora_inicio' => $this->input->post('hora_inicio'),
				'hora_fin' => $this->input->post('hora_fin'),
            );
            
            $reserva_id = $this->Reserva_model->add_reserva($params);
            redirect('reserva/index');
        }
        else
        {
			$this->load->model('Usuario_model');
			$data['all_usuario'] = $this->Usuario_model->get_all_usuario();

			$this->load->model('Amenidad_model');
			$data['all_amenidades'] = $this->Amenidad_model->get_all_amenidades();
            
            $data['_view'] = 'reserva/add';
            $this->load->view('layouts/main',$data);
        }
    }  

    /*
     * Editing a reserva
     */
    function edit($id)
    {   
        // check if the reserva exists before trying to edit it
        $data['reserva'] = $this->Reserva_model->get_reserva($id);
        
        if(isset($data['reserva']['id']))
        {
            $this->load->library('form_validation');

			$this->form_validation->set_rules('costo','Costo','required');
			$this->form_validation->set_rules('id_amenidad','Id Amenidad','required');
			$this->form_validation->set_rules('day','Day','required');
		
			if($this->form_validation->run())     
            {   
                $params = array(
					'month' => $this->input->post('month'),
					'id_autor' => $this->input->post('id_autor'),
					'id_amenidad' => $this->input->post('id_amenidad'),
					'costo' => $this->input->post('costo'),
					'estado' => $this->input->post('estado'),
					'day' => $this->input->post('day'),
					'year' => $this->input->post('year'),
					'id_condominio' => $this->input->post('id_condominio'),
					'fecha_registro' => $this->input->post('fecha_registro'),
					'fecha_modificacion' => $this->input->post('fecha_modificacion'),
					'hora_inicio' => $this->input->post('hora_inicio'),
					'hora_fin' => $this->input->post('hora_fin'),
                );

                $this->Reserva_model->update_reserva($id,$params);            
                redirect('reserva/index');
            }
            else
            {
				$this->load->model('Usuario_model');
				$data['all_usuario'] = $this->Usuario_model->get_all_usuario();

				$this->load->model('Amenidad_model');
				$data['all_amenidades'] = $this->Amenidad_model->get_all_amenidades();

                $data['_view'] = 'reserva/edit';
                $this->load->view('layouts/main',$data);
            }
        }
        else
            show_error('The reserva you are trying to edit does not exist.');
    } 

    /*
     * Deleting reserva
     */
    function remove($id)
    {
        $reserva = $this->Reserva_model->get_reserva($id);

        // check if the reserva exists before trying to delete it
        if(isset($reserva['id']))
        {
            $this->Reserva_model->delete_reserva($id);
            redirect('reserva/index');
        }
        else
            show_error('The reserva you are trying to delete does not exist.');
    }

    /*
     * Detalle de reserva
     */
    function detail($id)
    {   
        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };

        $breadcrumb = array(
            "Reservas" => "../",
            $id => ""
        );
        $data['breadcrumb'] = $breadcrumb;

        // check if the reserva exists before trying to edit it
        $data['reserva'] = $this->Reserva_model->get_reserva($id);
        
        if(isset($data['reserva']['id']))
        {

           
            $data['usuario'] = $this->Usuario_model->get_usuario($data['reserva']['id_autor']);

            
            $data['amenidad'] = $this->Amenidad_model->get_amenidad($data['reserva']['id_amenidad']);

            $data['controlador'] = 'reserva';
            $data['contenido'] = 'reserva/detail';
            $data['titulo'] = 'Detalle de Reserva';
            $data['_view'] = 'reserva/detail';
            $this->load->view('templates/template_admin', $data);
            
        }
        else
            show_error('The reserva you are trying to show does not exist.');
    } 
    
}
