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

class Welcome extends CI_Controller {

    public static $tiposDeVehiculos = array(
        "CAMIONETA" => "CAMIONETA",
        "AUTO COMPACTO" => "AUTO COMPACTO",
        "MONTACARGAS" => "MONTACARGAS",
        "CAMION" => "CAMION",
        "AUTO CARGA" => "AUTO CARGA",
        "MOTOCICLETA" => "MOTOCICLETA",
        "AUTO" => "AUTO",
        "UNIDAD MOVIL" => "UNIDAD MOVIL",
        "VEHICULO" => "VEHICULO",
        "MAQUINARIA" => "MAQUINARIA",
        "AUTOMOVIL" => "AUTOMOVIL",
        "TRAILER" => "TRAILER"
    );

    public function __construct(){
        parent::__construct();
        $this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
        $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this->output->set_header('Pragma: no-cache');
        $this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        $this->load->database();
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->library('grocery_CRUD');
    }

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

    public function _example_output($output = null){
        if($this->session->userdata('user_logeado') == FALSE || $this->session->userdata('user_logeado') == '' ){
            redirect( base_url() . 'login' );
        }else{
            $this->load->view('welcome_message', $output);
        }
    }

    public function logout(){
        redirect(base_url() . 'index.php/login/logout_ci');
    }

    public function clientes(){
        $crud = new grocery_CRUD();
        //$crud->set_language("Spanish");
        $crud->set_table('clientes');
        $crud->set_subject("Clientes");
        $crud->add_action("Contratos", base_url() . 'assets/grocery_crud/themes/flexigrid/css/images/multiple.png', '', '' , array($this,'linkContratos'));
        $output = $crud->render();
        $this->_example_output($output);
    }

    function linkContratos($primary_key, $row){
        return base_url(). 'index.php/welcome/contratos/' . $row->ID_CTE;
    }

    public function contratos($idCliente){
        $crud = new grocery_CRUD();
        //$crud->set_language("Spanish");
        $crud->set_table('contratos');
        $crud->where("clientes_ID_CTE = $idCliente");
        $crud->set_subject("Contratos del cliente $idCliente");
        $crud->columns("ID_CONTRATO", "FECHA_INICIO", "CONTRATO", "ANEXO", "TIPO_VEHICULO", "PLACAS", "SERIE");
        $crud->callback_column("noPedidos", array( $this, 'callback_noPedidos'));
        $crud->callback_column("tiempo", array( $this, 'callback_tiempo'));
        $crud->display_as("solicitada", "Solicitada")
            ->display_as("entregada", "Entregada")
            ->display_as("litrosRequeridos", "Litros Requeridos")
            ->display_as("motivoCancelacion", "Motivo Cancelacion")
            ->display_as("metodoPago", "Metodo de Pago");
        $crud->field_type("motivoCancelacion", "text");
        $crud->field_type("mensaje", "text");
        $crud->field_type("clientes_ID_CTE", "hidden", $idCliente);
        $crud->field_type("TIPO_VEHICULO", "dropdown", self::$tiposDeVehiculos );
        $this->config->set_item('grocery_crud_file_upload_allow_file_types','pdf|gif|jpeg|jpg|png');
        $crud->set_field_upload('PDF_POLIZA','assets/uploads/files');
        //$crud->unset_add();
        //$crud->unset_delete();
        //$crud->unset_edit();
        //$crud->unset_read();
        $output = $crud->render();
        $this->_example_output($output);
    }

    public function telefonos(){
        $crud = new grocery_CRUD();
        //$crud->set_language("Spanish");
        $crud->set_table('telefonos');
        $crud->set_subject("Telefonos");
        $output = $crud->render();
        $this->_example_output($output);
    }

    public function procedimientos(){
        $crud = new grocery_CRUD();
        //$crud->set_language("Spanish");
        $crud->set_table('procedimientos');
        $crud->set_subject("Procedimientos");
        $output = $crud->render();
        $this->_example_output($output);
    }

    public function tips(){
        $crud = new grocery_CRUD();
        //$crud->set_language("Spanish");
        $crud->set_table('tips');
        $crud->set_subject("Tips");
        $output = $crud->render();
        $this->_example_output($output);
    }

    public function usuarios(){
        $crud = new grocery_CRUD();
        //$crud->set_language("Spanish");
        $crud->set_table('usuarios');
        $crud->set_subject("Usuarios");
        $crud->callback_edit_field('PASS',array($this,'set_password_input_to_empty'));
        $crud->callback_add_field('PASS',array($this,'set_password_input_to_empty'));
        $crud->callback_before_insert(array($this,'encrypt_password'));
        $crud->callback_before_update(array($this,'encrypt_password_update'));
        $output = $crud->render();
        $this->_example_output($output);
    }

    function encrypt_password($post_array, $primary_key = null){
        $post_array['PASS'] = md5($post_array['PASS']);
        return $post_array;
    }

    function encrypt_password_update($post_array, $primary_key = null){
        if(!empty($post_array['PASS'])){
            $post_array['PASS'] = md5($post_array['PASS']);
        }else{
            unset($post_array['PASS']);
        }
        return $post_array;
    }

    function set_password_input_to_empty() {
        return "<input type='password' name='PASS' value='' />";
    }

}
