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

class Login extends CI_Controller
{ 	 
	public function __construct() 
	{
		parent::__construct();        
		$this->load->helper('url');
		
		$this->load->model('Login_model', 'category');
		$this->load->model('Login_model');
	}
	
	public function index()
	{
        $this->load->helper('url');        
        $data["extra_js"] = array("login");
        $data['controller'] = base_url('login');
        $data['page_title'] = "CMS - Jeet Info +91 90990 95951";
        $this->load->view('Login/Before/login', $data);
	}  
 
    public function check_auth() 
    {
        $this->load->library('session');
        date_default_timezone_set('Asia/Calcutta');
        $current_date_time = date("Y-m-d H:i:s");
        $new_date_time = strtotime($current_date_time);


        $result = array();
        $username = $this->input->post('username'); 
        $password = md5($this->input->post('password')); 

        $sql = "select id, display_name, username, display_name, password, activated from ". TBL_USERS ." where  username='$username' and password='$password' and activated='1' ";
        $query = $this->db->query($sql);
        $res = $query->row_array();
        if ($res !== null) 
        {    
 
            $data = array(
                'user_id' => $res['id'],  
                'username' => $res['username'], 
                'display_name' => $res['display_name'], 
                'login_date_time' => date("Y-m-d H:i:s"), 
                'validated' => true,
            );
             
            $this->session->set_userdata($data);  

            $login_attemp_data1 = 
            array(
                    'ula_user_id' => $res['id'], 
                    'ula_details' => "Login",
                    'ula_ip' => $_SERVER['REMOTE_ADDR'],
                    'ula_type' => 'Admin', 
                    'date_time' => date("Y-m-d H:i:s"), 
                ); 
            $this->category->save_ula_data($login_attemp_data1);

              
            $result['status']  = 'Success'; 
            $result['message'] = 'Login Successfully! Please wait...';           
        } 
        else 
        {   
            $result['status']  = 'Alert';  
            $result['message'] = "Enter correct details.";
        }
           
        header("Content-Type: application/json");
        echo json_encode($result);
        die; 
    }  
    
    public function logout() 
    { 
        $this->load->library('session');
        date_default_timezone_set('Asia/Calcutta');
        $username = $this->session->userdata('user_id');  
        if($username)
        { 
            $login_attemp_data = array(
                'ula_user_id' => $this->session->userdata('user_id'), 
                'ula_type' => "Admin",
                'ula_ip' => $_SERVER['REMOTE_ADDR'], 
                'ula_details' => "Logout", 
                'date_time' => date("Y-m-d H:i:s"), 
            ); 
            $this->category->save_ula_data($login_attemp_data);
        }
        $this->session->sess_destroy(); 
        $data["extra_js"] = array("login");
        $data['controller'] = base_url('login');
        $data['page_title'] = "CMS - Vinayak Webtech +91 90336 22526";        
        $this->load->view('Login/Before/login', $data);
    }
 
}
