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

 	public function getCustomerData($customer_id='',$view=''){

 		$cond  = (!empty($view))?" USR.user_type IN ($view) ":" USR.status != '2' ";
 		$cond .= (!empty($customer_id))?" AND CUST.cust_id='$customer_id' ":"";

 		$sql = "SELECT CUST.*,USR.status,USR.password
			    FROM customer AS CUST 
			    INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
			    WHERE $cond AND USR.user_type='1' Order by id DESC";

	    $customerData = $this->db->query($sql);
	  //echo $this->db->last_query();exit;
	    if(isset($customerData)){

	    	return (empty($customer_id))?$customerData->result():$customerData->row();
	    }
	    return 0;
 	}

 	public function addCustomer($customer_data = array()){

	if(empty($customer_data))
 			return 0;

 		$userNameChk = $this->db->query("SELECT * FROM users 
 									     WHERE status!='2' AND user_type='1' AND username='".$customer_data['email_id']."'");
 		if(!empty($userNameChk) && $userNameChk->num_rows() > 0) return 2;

 		$emailChk = $this->db->query("SELECT * FROM customer AS CUST 
 									  INNER JOIN users AS USR ON (USR.id=CUST.cust_id)
								      WHERE USR.status!='2' AND (USR.username='".$customer_data['email_id']."')");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) return 2;

 		$phoneChk = $this->db->query("SELECT * FROM customer AS CUST 
 									  INNER JOIN users AS USR ON (USR.id=CUST.cust_id)
									  WHERE USR.status!='2' AND (USR.phone_no='".$customer_data['phone_no']."')");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) return 3;

    $status1 = $this->db->insert('customer', array('name'=>$customer_data['name'],
										   'email_id'=>$customer_data['email_id'],
										   'code'=>$customer_data['phone_code'],
										   'phone_no'=>$customer_data['phone_no'],
										   'address'=>$customer_data['address'],
										   'area'=>$customer_data['area'],
										   'lat'=>$customer_data['lat'],
										   'lng'=>$customer_data['lng'],
										   'profile_photo'=>$customer_data['profile_photo']));
		                       $customer_id = $this->db->insert_id();



 		
 		if(empty($customer_id) && !isset($customer_id)){
 			return 0;
 		}else{

         $status = $this->db->insert('users', array('user_type'=>'1',
										   'profile_id'=>$customer_id,
										   'username'=>$customer_data['email_id'],
										   'phone_no'=>$customer_data['phone_no'],
										   'password'=>$customer_data['password']));

		$insertId = $this->db->insert_id();								  
        


        if(empty($insertId) && !isset($insertId)){
 			return 0;
 		}else{

    $data['customer_id']=$customer_id;
    $data['status']=$status;
 		}


       }

									   
 		return $data;
 	}




 	function updateCustomer($customer_id = '', $customer_data = array()){

 		
 		if(empty($customer_id) || empty($customer_data))
 			return 0;
 		$userIdChk = $this->db->query("SELECT * FROM customer AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									   WHERE USR.status!='2' AND CUST.cust_id!='".$customer_id."' AND 
								   			 USR.username='".$customer_data['email']."'");
 		if(!empty($userIdChk) && $userIdChk->num_rows() > 0) { return 4; }

 		$emailChk = $this->db->query("SELECT * FROM customer AS CUST 
 									  INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									  WHERE USR.status!='2' AND CUST.cust_id!='".$customer_id."' AND 
								   			CUST.email_id='".$customer_data['email']."'");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) { return 2; }

 		$phoneChk = $this->db->query("SELECT * FROM customer AS CUST 
 									  INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									  WHERE USR.status!='2' AND CUST.cust_id!='".$customer_id."' AND 
								   			CUST.phone_no='".$customer_data['phone']."'");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) { return 3; }

 		                 $upMecArr =  array('name'=>$customer_data['name'],
										   'email_id'=>$customer_data['email_id'],
										   'code'=>$customer_data['phone_code'],
										   'phone_no'=>$customer_data['phone_no'],
										   'address'=>$customer_data['address'],
										   'area'=>$customer_data['area'],
										   'lat'=>$customer_data['lat'],
										   'lng'=>$customer_data['lng']);
                               
 		$admUpdateArr = array('username'=>$customer_data['email']);
							  

 		if(isset($customer_data['profile_image']) && !empty($customer_data['profile_image'])){
 			$upMecArr['profile_photo'] = $customer_data['profile_image'];
 			//$admUpdateArr['profile_photo'] = $customer_data['profile_image'];
 		}
//echo json_encode($upMecArr);exit;
 		$status = $this->db->update('users',$admUpdateArr,array('profile_id'=>$customer_id,'user_type'=>'1'));
 		//echo $this->db->last_query();exit;
 		if(!$status) { return 0; }

		$status = $this->db->update('customer',$upMecArr,array('cust_id'=>$customer_id));

 		return $status;
 	}

 	function changeStatus($customer_id = '', $status = '0'){

 		if(empty($customer_id)){
 			return 0;
 		}

 		$status = $this->db->update('users',array('status'=>$status),array('profile_id'=>$customer_id,'user_type'=>'1'));
 		//echo $this->db->last_query();exit;
 		return $status;
 	}



function email_availability_check($email)
    {
if($email!=''){

        $query = "SELECT * FROM customer AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									   WHERE USR.status!='2' AND CUST.cust_id!='".$customer_id."' AND 
								   			 USR.username='".$email."'";
        $query=$this->db->query($query,array($email));
        if($query->num_rows()>0)
        {
            return $query->row_array();
        } else
        {
            return false;
        }

        }
    return false;
    }

 	function get_all_customer($filter=null) {
    	if($filter){
    		$this->db->order_by($filter['order_by'], $filter['order_type']);
    		if(!empty($filter['where'])){
    			$this->db->where($filter['where']);
    		}
    		if($filter['length']!=-1)
    			$this->db->limit($filter['length'],$filter['start']);
    	}
    	$this->db->where('users.user_type', 1);
    	$this->db->where('users.status!=','2');
    	$this->db->order_by('customer.cust_id','desc');
    	$this->db->select('customer.*,users.status');
    	$this->db->from('customer');
    	$this->db->join('users', 'users.profile_id = customer.cust_id');
        
    	$res = $this->db->get();
    	
    	return $res->result();
    }


}
?>