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

class Login extends CI_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Crud_model');
	}
	public function index()
	{
		if($this->input->post('submit'))
		{		
			$this->form_validation->set_error_delimiters('<div style="color:red;" class="error"><strong>', '</strong></div>');
			$this->form_validation->set_rules('username', 'Username', 'trim|required|valid_email');
        	$this->form_validation->set_rules('password', 'Password', 'trim|required');
            if ($this->form_validation->run() == TRUE) 
            {	
            	$username 	=	$this->input->post('username');//mysql_real_escape_string(stripslashes($this->input->post('username')));
				$password	= 	$this->input->post('password');//mysql_real_escape_string(stripslashes($this->input->post('password')));
			    $password   = 	md5($password);
 				$table_name = 	USERS_TBL;
			    $conditions = 	array("usr_email"	=>	$username,"usr_password"	=>	$password);
			    $select 	= 	"count(*) as cnt,usr_email,usr_password";
			    $by_row 	=	1;//0- result array, 1- row
			    $join_array =	array();
				$order_field=	'';
				$order_type =	"DESC";
				$group_filed=	'';
			    $limit 		= 	'';
			    $offset		=	null;
				$from_db 	= 	$this->Crud_model->select_data($table_name,$select,$conditions,$by_row,$join_array,$order_field,$order_type,$group_filed,$limit,$offset);
				if($from_db->cnt!=0)
				{
					if($this->input->post('remember'))
					{
					 	$cookie_user 	= 	array(
													'name'   => 'cookie_login_name',
													'value'  => $username,
													'expire' => '86500'
												);
						$this->input->set_cookie($cookie_user); 
						$cookie_pass = array(
												'name'   => 'cookie_login_password',
												'value'  => $password,
												'expire' => '86500'
											);
						$this->input->set_cookie($cookie_pass); 
							
					}
	 				$table_name = 	USERS_TBL;
				    $conditions = 	array("usr_email"	=>	$username,"usr_password"	=>	$password);
				    $select 	= 	"*";
				    $by_row 	=	1;//0- result array, 1- row
					$order_field=	'';
					$order_type =	"DESC";
					$group_filed=	'';
					$limit 		= 	'';
					$offset		=	null;
					$result		= 	$this->Crud_model->select_data($table_name,$select,$conditions,$by_row,$join_array,$order_field,$order_type,$group_filed,$limit,$offset);
					
					if($result->usr_type==0)
					{
						$not_count 	=	$this->Crud_model->select_data(NOTIFICATION_TBL,"COUNT(*) count",array("not_date <="=>date('Y-m-d',strtotime('+ 5 days')),"not_status"=>0),1,array(),'','','','','');
						$newdata = 	array(
											'user_id'         	=> 	$result->usr_reg_id,
											'username'        	=> 	$result->usr_email,
											'login_name'      	=>  'Admin',
											'emp_name'		 	=>	'Admin',
											'desig_name'		=>	'Admin',
											'emp_created_time'	=>	 date('Y-m-d'),
											'login_image'	 	=> 	'admin_profile.png',
											'user_type'       	=> 	$result->usr_type,
					   						'notification_count'=> 	$not_count->count,
											'logged_in'       	=> 	TRUE
										);
						$this->session->set_userdata($newdata);
					}
					else
					{
		 				$table_name 		= 	USERS_TBL;
						$conditions 		= 	array("usr_email"	=>	$username,"usr_password"	=>	$password);
						$select 			= 	"usr_reg_id,usr_email,usr_type,emp_branch_branch_id,emp_name,desig_name,emp_created_time,emp_image";
						$by_row 			=	0;//0- result array, 1- row
						$join_array     	= 	array(	0=>array(0=>"employee",1=>"emp_id = usr_reg_id",2=>''),
						    								1=>array(0=>"employee_branch",1=>"emp_branch_emp_id = emp_id",2=>''),					    								
						    								2=>array(0=>"designation",1=>"desig_id = emp_desig_id",2=>'')
						    							);
						$order_field		=	'';
						$order_type 		=	"DESC";
						$group_filed		=	'';
						$limit 				= 	'';
						$offset				=	null;
						$result  			=	$this->Crud_model->select_data($table_name,$select,$conditions,$by_row,$join_array,$order_field,$order_type,$group_filed,$limit,$offset);
						$result 			=	end($result);
						$not_count 			=	$this->Crud_model->select_data(NOTIFICATION_TBL,"COUNT(*) count",array("not_date <="=>date('Y-m-d',strtotime('+ 5 days')),"not_user_id"=>$result['usr_reg_id'],"not_status"=>0),1,array(),'','','','','');
						$loginRegId			= 	$result['usr_reg_id'];
						$loginUsername    	= 	$result['usr_email'];
						$loginEmp_name 		=	$result['emp_name'];
						$loginDesig_name	=	$result['desig_name'];
						$loginCreated_time	=	$result['emp_created_time'];
						$loginUserimage    	= 	$result['emp_image'];
						$loginUserType    	= 	$result['usr_type'];
						$loginGodown_id[] 	=	$result['emp_branch_branch_id'];
						$loginName        	= 	$result['emp_name'];
						$newdata 			=	array(
														   	'user_id'         	=> $loginRegId,
														   	'username'        	=> $loginUsername,
														    'emp_name'		  	=> $loginEmp_name,
														    'desig_name'	  	=> $loginDesig_name,
														    'emp_created_time'	=> $loginCreated_time,
											   				'login_image'	  	=> $loginUserimage,
														   	'user_type'       	=> $loginUserType,
														   	'godown_id'       	=> $loginGodown_id,
														   	'login_godown_id' 	=> $loginGodown_id,
														   	'login_name'     	=> $loginName,
					   										'notification_count'=> $not_count->count,
														   	'logged_in'       	=> TRUE
														);
						$this->session->set_userdata($newdata);			
					}
					if (session_status() == PHP_SESSION_NONE)
						session_start();
					$_SESSION['login_session'] = $newdata;
						redirect('Dashboard');				
				}
				else
				{
					$this->session->set_flashdata('msg', 'The email or password you entered is incorrect.');
				}
			}
		}			
		$data['page_title'] 	= 	'Login';
        $data['page_name']  	= 	'login';
		$this->load->view('login', $data);
	}
	public function logout()
	{		
		$this->session->sess_destroy();
		redirect(base_url());
	}
}
