<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_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() . 'login/verify_login';
        $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('login');
            exit(0);
        }
        else if ($user->status == '0') {
            $this->session->set_flashdata('message', 'User Account has been Blocked. Please contact site administrator!');
            redirect('login');
            exit(0);        
        }
        
        #$setting = $this->basic_settings_model->get_by_id();
        $userdata = array(
            'role' => $user->role,
            'user_id' => $user->id,
            'name' => $user->name
        );
        
        if (!empty($this->input->post('time_zone_offset'))) {
            $userdata['time_zone_offset'] = $this->input->post('time_zone_offset');
        }
        $this->session->set_userdata(TBL_PREFIX . 'user_logged_in', $userdata);
        
        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('login', 'refresh');
    }
}
?>