<?php 
class Customer_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}

 	function getCustomer($customer_data = array()){
 		$cond = (isset($customer_data['phone']) && !empty($customer_data['phone']))?
											" AND phone LIKE '%".trim($customer_data['phone'])."'":"";
		$veh_data = array();
		if(!empty($customer_data['customer_id'])){
			$cond .= " AND customer_id = '".trim($customer_data['customer_id'])."'";
			$veh_data = $this->db->get_where(
				    'customer_vehicle',array('status'=>'1','customer_id'=>$customer_data['customer_id']));
			$veh_data = (!empty($veh_data))?$veh_data->result():'';
		}

		$result = $this->db->query("SELECT * FROM customers WHERE status IN (0,1) $cond");
		if(empty($result)){
			return;
		}
		$ret_data = $result->result();
		if(!empty($customer_data)){
			$ret_data = $result->row();
			$ret_data->vehicle_data = $veh_data;
		}
		return $ret_data;
 	}

 	function createCustomer($customer_data = array(), $saved_vehicles = array()){
 		if(empty($customer_data))
 			return 0;

 		if(isset($customer_data['email']) && !empty($customer_data['email'])){
	 		$emailChk = $this->db->get_where('customers',array('email'=>$customer_data['email'],'status !='=>'2'));
	 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
	 			return 2;
	 		}
 		}
 		if(isset($customer_data['phone']) && !empty($customer_data['phone'])){
	 		$phoneChk = $this->db->get_where('customers',array('phone'=>$customer_data['phone'],'status !='=>'2'));
	 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
	 			return 3;
	 		}
	 	}
		$status = $this->db->insert('customers',$customer_data);
		if($status && !empty($saved_vehicles) && !empty($cust_id = $this->db->insert_id())){
			$this->db->query("UPDATE customer_vehicle SET status='1',customer_id='$cust_id' 
								 	 WHERE customer_veh_id IN ($saved_vehicles)");
		}
		return ($status)?1:0;;
 	}

 	function updateCustomer($customer_id = '', $customer_data = array(), $saved_vehicles = array()){
 		if(empty($customer_id) || empty($customer_data))
 			return 0;

 		$emailChk = $this->db->get_where('customers',array('email'=>$customer_data['email'],
 														   'customer_id !='=>$customer_id,
 														   'status !='=>'2'));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$phoneChk = $this->db->get_where('customers',array('phone'=>$customer_data['phone'],
 														   'customer_id !='=>$customer_id,
 														   'status !='=>'2'));
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
 			return 3;
 		}
 		
		$status = $this->db->update('customers',$customer_data,array('customer_id'=>$customer_id));

		if($status && !empty($saved_vehicles)){
			$this->db->query("UPDATE customer_vehicle SET status='1',customer_id='$customer_id' 
								 	 WHERE customer_veh_id IN ($saved_vehicles)");
		}

		return ($status)?1:0;;
 	}

 	function changeStatus($customer_id = '', $status = '0'){
 		if(empty($customer_id)){
 			return 0;
 		}
 		$status = $this->db->update('customers',array('status'=>$status), array('customer_id'=>$customer_id));
 		return $status;
 	}

 	function checkCustomerLogin($userLogData){
 		$respArr = array('status'=>0);
 		if(empty($userLogData)){
 			return $returnStatus;
 		}
 		$result = $this->db->get_where('customers',array('email'=>$userLogData['email'],'status'=>'1'));
 		if(empty($result) || $result->num_rows() < 1 || empty($custData = $result->row())){
 			$respArr['status'] = 2;
 			return $respArr;
 		}

 		$result = $this->db->get_where('customers',array('email'=>$userLogData['email'],
 														 'password'=>md5($userLogData['password']),
 														 'status'=>'1'));
 		$respArr['status'] = 3;
 		if(!empty($result) && $result->num_rows() == 1 && !empty($custData = $result->row())){
 			$respArr['data'] = $custData;
 			$respArr['status'] = 1;
 		}
 		return $respArr;
 	}

 	function genCustForgotPassLink($userLogData = array()){
 		$respArr = array('status'=>0);
 		if(empty($userLogData)){
 			return $respArr;
 		}
 		$result = $this->db->get_where('customers',array('email'=>$userLogData['email'],'status'=>'1'));
 		if(empty($result) || $result->num_rows() < 1 || empty($custData = $result->row())){
 			$respArr['status'] = 2;
 			return $respArr;
 		}

 		$mail_id = $custData->email;
 		$cust_id = $custData->customer_id;
 		$unique_id = generate_unique();
 		
 		$status = $this->db->insert('forgot_password_link',array('user_type'=>'1','user_id'=>$cust_id,
 															     'token'=>$unique_id));
 		if($status){
 			$respArr['status'] = 1;
 		}
 		return $respArr;
 	}

 	function customer_registration($userData = array()){
 		$respArr = array('status'=>0);
 		if(empty($userData)){
 			return $respArr;
 		}
 		$result = $this->db->get_where('customers',array('email'=>$userData['email'],'status'=>'1'));
 		if(!empty($result) && $result->num_rows() >= 1){
 			$respArr['status'] = 2;
 			return $respArr;
 		}
        $userData['password'] = md5($userData['password']);
 		$status = $this->db->insert('customers',$userData);
 		if($status){
 			$respArr['status'] = 1;
 		}
 		return $respArr;
 	}
}
?>