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

class Auth extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("Usuarios_model");
    }

    public function index()
    {
        if ($this->session->userdata("login")) {
            redirect(base_url() . "backend/dashboard");
        } else {
            $this->load->view('backend/login');
        }

    }

    public function login()
    {
        $Cedula = $this->input->post("Cedula");
        $pass  = $this->input->post("password");
        
        $this->load->model('Usuarios_model');
        $this->load->model('estudiantes_model');

        $res = $this->Usuarios_model->logear($Cedula, md5($pass));
        
        if ($res->idtipousuario != "5") {
            $res2 = $this->estudiantes_model->comprobarCedula($Cedula);
            $SecUsu = $res2->Seccion;
        } else {
            $SecUsu = '';
        }

        if ($res) {
            /*echo "Sucess";*/
            $data = array(
                'id_user' => $res->id_usuario,
                'user'    => $res->nombre . ' ' . $res->apellidos ,
                'Rol'    => $res->denominacion ,
                'Rolid'    => $res->idtipousuario ,
                'Seccion'    => $SecUsu ,
                'login'   => true,
                'imagenperfil' => 'perfil.jpg',
            );
            $this->session->set_userdata($data);
            redirect(base_url() . "backend/dashboard");
        } else {
            $this->session->set_flashdata("error","<span><strong>Lo sentimos,</strong> El numero de Cedula y contraseña ingresadas no coinciden con nuestros registros</span>");
            redirect(base_url()."cpanel");
        }
    }

    public function logout()
    {
        $this->session->sess_destroy();
        redirect(site_url() . "cpanel");
    }
}
