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

define('NO_RELOAD', TRUE);

class Admin extends REF_Controller {
    public function __construct() {
        parent::__construct();
        
        // Check whether User logged in or not
        /*if (!empty($this->session->userdata('user_logged_in')))
            redirect( 'home', 'refresh' );*/
		$this->load->model('login_details_model');
    }

    public function index() {
        $data = array();
        $data['title'] = 'Login';
        $data['form_action'] = base_url() . 'admin/verify_login';
        //echo 'Load'.$this->session->userdata('request_uri'); die;
        $this->load->view('login', $data);
    }
    
    public function verify_login() {
        $this->load->model('basic_settings_model', '', TRUE);
        $data = array();
        $user = $this->user_model->authenticate_user($this->input->post('user_name'), $this->input->post('password'));
        if (empty($user)) {
            $this->session->set_flashdata('message', 'Invalid User Name or Password!');
            redirect('admin');
            exit(0);
        }
        else if ($user->status == '0') {
            $this->session->set_flashdata('message', 'User Account has been Blocked. Please contact site administrator!');
            redirect('admin');
            exit(0);        
        }
        
        #$setting = $this->basic_settings_model->get_by_id();
        set_user_data($user);
        
        if (empty($this->session->userdata('request_uri'))) 
            redirect('home', 'refresh');
        else {
            $request_uri = $this->session->userdata('request_uri');
            $this->session->unset_userdata('request_uri');
            redirect($request_uri, 'refresh');
        }
    }
    
    public function logout() {
       $user_data 		= get_user_data();
		$userid 		= $user_data["user_id"];
		$ip_address 	= $this->login_details_model->get_ipaddress_by_userid($userid);
        $this->session->unset_userdata(TBL_PREFIX . 'user_logged_in');
		$ip_user_data = get_session_ipdata();
		$userdata 		= array("ls_id"=>$ip_user_data["ls_id"],"ip_address"=>$ip_address,"user_id"=>$userid);
		$this->session->set_userdata(TBL_PREFIX . 'ip_user_data', $userdata);
        redirect( IS_KSK ? '' : 'admin' , 'refresh');
    }
}
?>