<?php
class Login extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->library('App_utilities');
	}
	/**/
	function index(){
		$UIData['main_view']="login_view";
		$UIData['section_title']="Inicio";
		$UIData['structure_only']=true;
		$UIData['jsfiles']=array('miracle.login');
		$UIData['plugins']=array();
		
		if($this->input->get('c')){
			$UIData['caja']=$this->input->get('c');
		}else{
			$UIData['caja']=0;
		}
		
		$this->load->view('template', $UIData);
	}
	function check(){
		$user=$this->Generic_model->get('usuarios','*',array('username_usuario'=>$this->input->post('username'),'password_usuario'=>sha1($this->input->post('password'))));
		if(count($user)>0){echo 'true';}else{echo 'false';}
	}
	function logon(){
		$user=$this->Generic_model->get('usuarios','*',array('username_usuario'=>$this->input->post('username'),'password_usuario'=>sha1($this->input->post('password'))));
		$caja=$this->Generic_model->get('cajas','*',array('idh_caja'=>$this->input->post('caja')));
		if(count($user)>0){
			$idUser=$user[0]->id_usuario;
			$nombre=$user[0]->nombre_usuario;
			if(count($caja)){
				$dirIp=$caja[0]->direccion_ip_caja;
			}else{
				$dirIp = 0;
			}
			$session_id = substr(md5(uniqid(rand())),0,32);
			$data = array(
				'id_session'=> $session_id,
				'username'=> $this->input->post('username'),
				'userid'		=> $idUser,
				'hostdir'		=> $dirIp,
				'is_logged_in' 	=> true
			 );
			$this->session->set_userdata('userInfo', $data);
			$this->Generic_model->update('usuarios',array('conectado_usuario'=>1),'id_usuario',$idUser);
			/* $this->app_utilities->send($user[0]->nombre_usuario); */
			redirect(base_url("Home"));
		}else{
			redirect(base_url("Login"));
		}
	}
	function logout(){
		$session = $this->session->userdata('userInfo');
		$this->Generic_model->update('usuarios',array('conectado_usuario'=>0),'id_usuario',$session['userid']);
		$this->session->unset_userdata('userInfo');
		$this->session->sess_destroy();
		
		redirect(base_url("Login"));
	}
	function sessiondestroy(){
		$this->session->unset_userdata('userInfo');
		$this->session->sess_destroy();
		redirect(base_url("Login"));
	}
	function test(){
		echo phpinfo();
	}
}
?>