<?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' );*/
    }

    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() {
        $this->session->unset_userdata(TBL_PREFIX . 'user_logged_in');
        redirect( IS_KSK ? '' : 'admin' , 'refresh');
    }
}
?>