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

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

        $breadcrumb = array(
            "Visitas" => ""
        );
        $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('visita/page');
        $config['total_rows'] = $this->Visita_model->get_all_visitas_count();
        $config['per_page'] = $params['limit'];
        $config['use_page_numbers'] = TRUE;
        $config['first_url'] = base_url('visita');
        $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'] = 'visita';
        $data['visitas'] = $this->Visita_model->get_all_visitas($params);
        
        $data['contenido'] = 'visita/index';
        $data['titulo'] = 'Listado de Visitas';
        $data['_view'] = 'visita/index';
        $this->load->view('templates/template_admin', $data);
    }

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

		$this->form_validation->set_rules('registrado_por','Registrado Por','required');
		
		if($this->form_validation->run())     
        {   
            $params = array(
				'registrado_por' => $this->input->post('registrado_por'),
				'nombre' => $this->input->post('nombre'),
				'fecha_registro' => $this->input->post('fecha_registro'),
				'hora_estimada_visita' => $this->input->post('hora_estimada_visita'),
				'codigo_entrada' => $this->input->post('codigo_entrada'),
				'escaneado_por' => $this->input->post('escaneado_por'),
				'fecha_escaneado' => $this->input->post('fecha_escaneado'),
				'estado' => $this->input->post('estado'),
            );
            
            $visita_id = $this->Visita_model->add_visita($params);
            redirect('visita/index');
        }
        else
        {
			$this->load->model('Usuario_model');
			$data['all_usuarios'] = $this->Usuario_model->get_all_usuarios();
            
            $data['_view'] = 'visita/add';
            $this->load->view('layouts/main',$data);
        }
    }  

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

			$this->form_validation->set_rules('registrado_por','Registrado Por','required');
		
			if($this->form_validation->run())     
            {   
                $params = array(
					'registrado_por' => $this->input->post('registrado_por'),
					'nombre' => $this->input->post('nombre'),
					'fecha_registro' => $this->input->post('fecha_registro'),
					'hora_estimada_visita' => $this->input->post('hora_estimada_visita'),
					'codigo_entrada' => $this->input->post('codigo_entrada'),
					'escaneado_por' => $this->input->post('escaneado_por'),
					'fecha_escaneado' => $this->input->post('fecha_escaneado'),
					'estado' => $this->input->post('estado'),
                );

                $this->Visita_model->update_visita($id,$params);            
                redirect('visita/index');
            }
            else
            {
				$this->load->model('Usuario_model');
				$data['all_usuarios'] = $this->Usuario_model->get_all_usuarios();

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

    /*
     * Deleting visita
     */
    function remove($id)
    {

        $visita = $this->Visita_model->get_visita($id);

        // check if the visita exists before trying to delete it
        if(isset($visita['id']))
        {
            $this->Visita_model->delete_visita($id);
            redirect('visita/index');

        }
        else
            show_error('The visita you are trying to delete does not exist.');
    }

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

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

        // check if the visita exists before trying to edit it
        $data['visita'] = $this->Visita_model->get_visita($id);
        
        if(isset($data['visita']['id']))
        {
           
            $this->load->model('Usuario_model');
            $data['usuario'] = $this->Usuario_model->get_usuario($data['visita']['registrado_por']);

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