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

class Login extends CI_Controller {

	public function __construct()
	{
		parent:: __construct();
		$this->load->library('form_validation');
	}
	public function index()
	{
		$this->load->view('login');
	}
	
	public function login()
	{
		$this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		if($this->form_validation->run() == TRUE) {
			$username = $_POST['username'];
			$password = $_POST['password'];
			
			$this->db->select('*');
			$this->db->from('user');
			$this->db->where(array('username' => $username));
			$this->db->where(array('br_id' => $password));
			$query = $this->db->get();
		    if($query->num_rows() > 0)
		    {
    			$user = $query->row();
    			
    			if(password_verify($password,$user->password)) {
    			//if($user->username) {
    				$this->session->set_flashdata("success", "You are logged in");
    				$_SESSION['user_logged'] = TRUE;
    				$_SESSION['username'] = $user->username;
    				$_SESSION['user_id'] = $user->user_id;
    				$_SESSION['branch_id'] = $user->branch_id;
    				$_SESSION['branch_code'] = $user->branch_code;
    				$_SESSION['branch_name'] = $user->branch_name;
    				$_SESSION['full_name'] = $user->full_name;
    				$_SESSION['user_group'] = $user->user_group;
    				$_SESSION['authority'] = $user->authority;
    				if($_SESSION['branch_name'] == 'CW4_CEO' || $_SESSION['branch_name'] == 'CW4_ACC' ){
    				    $_SESSION['cw_ceo'] = 'CW4';
    				    
    				}
    				else if($_SESSION['branch_name'] == 'CW5_CEO' || $_SESSION['branch_name'] == 'CW5-ACC'){
    				    $_SESSION['cw_ceo'] = 'CW5';
    				}
    				else if($_SESSION['branch_name'] == 'CW6_CEO' || $_SESSION['branch_name'] == 'CW6_ACC'){
    				    $_SESSION['cw_ceo'] = 'CW6';
    				}
    				else{
    				    $_SESSION['cw_ceo'] = 'CW';
    				}
    				//$_SESSION['role'] = $user->role;
    				//if($_SESSION['role'] == 'user')
    				//{
    					//redirect("User/module_creation", "refresh");
    					if($_SESSION['username']=='backend_user'){
    						redirect("User/module_creation", "refresh");
    					}else{
    						redirect("User/dashboard", "refresh");
    					}
    				//}
    				/*else if($_SESSION['role'] == 'admin')
    				{
    					redirect("user/enshipment", "refresh");
    				}*/
    			} else {
    				$this->session->set_flashdata("error", "No such account exists in database");
    				redirect("Login/index");
    			}
    		}
    		else
    		{
    		    $this->session->set_flashdata("error", "No such account exists in database");
    				redirect("Login/index");
    		}
		}
		$this->load->view('login');
	}
	
	function exe_download($filename = NULL) {
        // load download helder
        $this->load->helper('download');
        // read file contents
        $data = file_get_contents(base_url($filename));
        force_download($filename, $data);
    }
	
	public function logout_user()
	{
		$_SESSION['user_logged'] = FALSE;
		//$this->session->sess_destroy();
		$this->load->view('login');
	}
}
