<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Caseta extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Caseta_model');
    } 

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

        $breadcrumb = array(
            "Casetas de Vigilancia" => ""
        );
        $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('caseta/page');
        $config['total_rows'] = $this->Caseta_model->get_all_casetas_count();
        $config['per_page'] = $params['limit'];
        $config['use_page_numbers'] = TRUE;
        $config['first_url'] = base_url('caseta');
        $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'] = TRUE;
        $data['controlador'] = 'caseta';
        $data['casetas'] = $this->Caseta_model->get_all_casetas($params);
        
        $data['contenido'] = 'caseta/index';
        $data['titulo'] = 'Casetas de Vigilancia';
        $data['_view'] = 'caseta/index';
        $this->load->view('templates/template_admin', $data);
    }

    /*
     * Adding a new caseta
     */
    function add()
    {   

        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };
        $breadcrumb = array(
            "Casetas de Vigilancia" => "./",
            "Agregar" => ""
        );
        $data['breadcrumb'] = $breadcrumb;

        $this->load->library('form_validation');

		$this->form_validation->set_rules('nombre','Nombre','required');
		$this->form_validation->set_rules('ubicacion','Ubicacion','required');
		
		if($this->form_validation->run())     
        {   

            $params = array(
				'nombre' => $this->input->post('nombre'),
				'ubicacion' => $this->input->post('ubicacion'),
				'telefono' => $this->input->post('telefono'),
				'id_condominio' => $this->session->userdata('condominio'),
				'id_autor' => $this->session->userdata('id'),
				'fecha_registro' => date("Y-m-d H:i:s"),
				'fecha_modificacion' => date("Y-m-d H:i:s"),
				'estado' => 1,
            );
            
            $caseta_id = $this->Caseta_model->add_caseta($params);
            redirect('caseta');
        }
        else
        {       

            $data['controlador'] = 'caseta';
            $data['contenido'] = 'caseta/add';
            $data['titulo'] = 'Nueva Caseta de Vigilancia';
            $data['_view'] = 'caseta/add';
            $this->load->view('templates/template_admin', $data);
        }
    }  

    /*
     * Editing a caseta
     */
     function edit($id)
    {   

        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };
        $breadcrumb = array(
            "Casetas de Vigilancia" => "../",
            "Editar" => ""
        );
        $data['breadcrumb'] = $breadcrumb;
        // check if the caseta exists before trying to edit it
        $data['caseta'] = $this->Caseta_model->get_caseta($id);
        
        if(isset($data['caseta']['id']))
        {
            $this->load->library('form_validation');

            $this->form_validation->set_rules('nombre','Nombre','required');
            $this->form_validation->set_rules('ubicacion','Ubicacion','required');
        
            if($this->form_validation->run())     
            {   
                $params = array(
                    'nombre' => $this->input->post('nombre'),
                    'ubicacion' => $this->input->post('ubicacion'),
                    'telefono' => $this->input->post('telefono'),
                    'id_condominio' => $this->input->post('id_condominio'),
                    'id_autor' => $this->input->post('id_autor'),
                    'fecha_registro' => $this->input->post('fecha_registro'),
                    'fecha_modificacion' => date("Y-m-d H:i:s"),
                    'estado' => $this->input->post('estado'),
                );

                $this->Caseta_model->update_caseta($id,$params);            
                redirect('caseta');
            }
            else
            {
                $data['controlador'] = 'caseta';
                $data['contenido'] = 'caseta/edit';
                $data['titulo'] = 'Editar Caseta de Vigilancia';
                $data['_view'] = 'caseta/edit';
                $this->load->view('templates/template_admin', $data);
            }
        }
        else
            show_error('The caseta you are trying to edit does not exist.');
    } 
    /*
     * Deleting caseta
     */
    function remove($id)
    {
        $caseta = $this->Caseta_model->get_caseta($id);

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