<?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 - Vinayak Webtech +91 90336 22526";
        $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, 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'], 
                'login_date_time' => date("Y-m-d H:i:s"), 
                'validated' => true,
            );
             
            $this->session->set_userdata($data);  
              
            $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'); 
        $this->session->sess_destroy(); 
        $data["extra_js"] = array("login");
        $data['controller'] = base_url('login');
        $data['page_title'] = "CMS - Synergy Superspeciality Hospital | Vinayak Webtech +91 90336 22526";
        $this->load->view('Login/Before/login', $data);
    }
 
}
