<?php

class Login extends CI_Controller
{
    private $t;
    public $db2;
    public function __construct(){
        parent::__construct();
        $helpers=array("url");
        $libraries=array("session");
        $this->load->helper($helpers);
        $this->load->library($libraries);
        
        $this->conexion=$this->load->database("hclinica",true);
        $this->load->model("hclinica/UsuarioModel","usuarioModel");
        //$this->load->model("configure/models/UsuarioModel");
    }
    public function index(){
        $this->load->view("hclinica/login");
    }
    public function signin()
    {
        $res=Array("res"=>false,"mensage"=>"Usuario o password no válido");
        if($this->input->server("REQUEST_METHOD")=="POST")
        {
            //base64_decode($this->input->post("password"));
            if(isset($_POST["usuario"],$_POST["password"]))
            {
                if(trim($this->input->post("usuario"))!="" || trim($this->input->post("pass")))
                {
                    $usuario=$this->usuarioModel->getArrayUsuarioByLogin($this->input->post("usuario"));

                    if($usuario!=null)
                    {
                        if($usuario["pass"]==base64_decode($this->input->post("password")))
                        {
                            
                            $this->load->library("NavSession",NULL,"navSession");
                            $this->navSession->setUsuario($usuario);
                            $this->session->set_userdata("session",serialize($this->navSession));
                            //aqui se carga la info del emisor
                            $res["res"]=true;
                            $res["mensage"]="Acceso concedido";
                        }
                    }
                }
                
            }
        }
        echo json_encode($res);
    }
    public function logout()
    {
        //if($this->session->userdata("session"))
        if($this->session->has_userdata('session'))
        {
            $this->session->sess_destroy();
        }
        redirect("login");
    }
    private function cargarEmisor()
    {

    }
}

