<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model {
   
	public function __construct()
	{
		date_default_timezone_set("Asia/Dubai");
	}
	public function get_user_details($username,$password)
	{
		$this->db->select("A.*");
		$this->db->from('users A');
		$this->db->where('A.usr_email',$username);
		$this->db->where('A.usr_password',$password);
		$result  = $this->db->get();
		$res	 = $result->row();
		
		
		if($res->usr_email == $username && $res->usr_password==$password && $res->usr_type==0)
		{
			$newdata = array(
					   'user_id'         => $res->usr_reg_id,
					   'username'        => $res->usr_email,
					   'login_name'      => 'Admin',
					   'user_type'       => $res->usr_type,
					   'logged_in'       => TRUE
			);
			$this->session->set_userdata($newdata);
		}
		else
		{
			$this->db->select("A.usr_id,A.usr_reg_id,A.usr_email,A.usr_type,C.eg_emp_gdwn_id,B.emp_shop_id,B.emp_name");
			$this->db->from('users A');
			$this->db->join('employee B','B.emp_id = A.usr_reg_id');
			$this->db->join('employee_godown C','C.eg_emp_id = B.emp_id');
			$this->db->where('A.usr_email',$username);
			$this->db->where('A.usr_password',$password);
			$result  = $this->db->get();
			$godown_id = array();
			foreach($result->result_array() as $row)
			{
				$loginRegId       = $row['usr_reg_id'];
				$loginUsername    = $row['usr_email'];
				$loginUserType    = $row['usr_type'];
				$loginGodown_id[] = $row['eg_emp_gdwn_id'];
				$loginShopId      = $row['emp_shop_id'];
				$loginName        = $row['emp_name'];
			}
			
			
			$newdata = array(
					   'user_id'         => $loginRegId,
					   'username'        => $loginUsername,
					   'user_type'       => $loginUserType,
					   'godown_id'       => $loginGodown_id,
					   'login_godown_id' => $loginGodown_id,
					   'shop_id'         => $loginShopId,
					   'login_name'      => $loginName,
					   'logged_in'       => TRUE
			);
			$this->session->set_userdata($newdata);			
		}
		if (session_status() == PHP_SESSION_NONE)
			session_start();
		$_SESSION['login_session'] = $newdata;
	}
	
	
	/*public function password_recovery_mail($emp_id){
		$to_email   	= $this->input->post('email');
		$from_mail  	= 'info@accounts.com';
		$subject		= 'Reset your password';
		$new_password	= $this->generateRandomString();
		$message        = "Hi,
		The password for Accounts was recently changed.\n\r
		Please use the new password  given below:\n\r
		Password:".$new_password."
		Best regards,
		Accounts (info@accounts.com) ";
		$this->send($from_mail,$to_email ,$subject,$message,$attachment=""); 
		$this->update_password($new_password,$emp_id);
	}
	public function send($from_mail,$to_mail,$subject,$message,$attachment="")
	{
		$this->load->library('email');
		$this->email->from($from_mail, 'Mint Event Rentals');
		$this->email->to($to_mail);
		$this->email->subject($subject);
		$this->email->message($message);
		if($attachment){
		$this->email->attach($attachment);
		}
		$this->email->send();
		$this->email->clear(TRUE);
		
	}
	function generateRandomString($length = 6) {
    	return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
	}
	public function update_password($new_password,$emp_id){
		$this->db->where('usr_reg_id',$emp_id);
		$this->db->update('users',array('usr_password'=>md5($new_password)));
		
	}
	public function get_user_id_by_email($email_id)
	{
		$this->db->select('emp_id');
		$this->db->where('emp_email',$email_id);
		$result	= $this->db->get('employee');
		$emp_id	='';
		if($result->num_rows()>0){
            $res = $result->row() ;
			$emp_id	= $res->emp_id;
        }
        return $emp_id;
	}*/
}

?>