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

 	public function getExpressData($express_id='',$view=''){

 		
 		$sql = "SELECT * FROM express WHERE carrier_id='$express_id'  Order by carrier_id DESC";

	    $ExpressData = $this->db->query($sql);
	
	    
	    if(isset($ExpressData)){

	    	return ($ExpressData->row());
	    }
	    return 0;
 	}

 	public function addExpress($Express_data = array()){

	if(empty($Express_data))
 			return 0;
          $userNameChk = $this->db->query("SELECT * FROM users 
 									     WHERE status!='2' AND user_type='4' AND username='".$Express_data['email']."'");

 		if(!empty($userNameChk) && $userNameChk->num_rows() > 0) return 2; 

 		 // $userNameChk = $this->db->query("SELECT * FROM Express AS CUST 
 			// 						   INNER JOIN users AS USR ON (USR.user_id=CUST.id)
 			// 						   WHERE USR.status!='2' AND CUST.id!='".$Express_id."' AND 
				// 				   			 USR.username='".$Express_data['email']."'");
 		 // if(!empty($userNameChk) && $userNameChk->num_rows() > 0) return 2;

 		$emailChk = $this->db->query("SELECT * FROM express 
								      WHERE email_id='".$Express_data['email']."'");

 		if(!empty($emailChk) && $emailChk->num_rows() > 0) return 2;

 		$phoneChk = $this->db->query("SELECT * FROM express 
 									 
									  WHERE phone_no='".$Express_data['phone']."'");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) return 3;

    $status = $this->db->insert('express', 
									 array('name'=>$Express_data['name'],
										   'email_id'=>$Express_data['email'],
										   'code'=>$Express_data['phone_code'],
										   'phone_no'=>$Express_data['phone'],
										   'altered_no'=>$Express_data['altered_no'],
										   'location'=>$Express_data['location'],
										   'lat'=>$Express_data['lat'],
										   'lng'=>$Express_data['lng'],
										   'express_code'=>$Express_data['express_code'],
										   'address'=>$Express_data['address'],
										   'image'=>$Express_data['profile_image']));
		                       $Express_id = $this->db->insert_id();


 

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

         $status = $this->db->insert('users', array('user_type'=>'4',
										   'profile_id'=>$Express_id,
										   'username'=>$Express_data['email'],
										   'phone_no'=>$Express_data['phone'],
										   'password'=>$Express_data['password']));

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


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

   
    $data['status']=1;
     $data['profile_id']=$Express_id;
 		}


       }
       return $data;	   
 		
 	}




 	function updateExpress($Express_id = '', $Express_data = array()){

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

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

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

 		                 $upMecArr =  array('name'=>$Express_data['name'],
										   'email_id'=>$Express_data['email'],
										   'code'=>$Express_data['phone_code'],
										   'phone_no'=>$Express_data['phone'],
										   'altered_no'=>$Express_data['altered_no'],
										   'location'=>$Express_data['location'],
										   'lat'=>$Express_data['lat'],
										   'lng'=>$Express_data['lng'],
										   'express_code'=>$Express_data['express_code'],
										   'address'=>$Express_data['address']);
                               
 		$admUpdateArr = array('username'=>$Express_data['email']);
							  

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

		$status = $this->db->update('express',$upMecArr,array('carrier_id'=>$Express_id));

 		return $status;
 	}

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

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

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



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

        $query = "SELECT * FROM express AS CUST 
 									   INNER JOIN users AS USR ON (USR.user_id=CUST.carrier_id)
 									   WHERE USR.status!='2' AND CUST.id!='".$Express_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;
    }

public function get_all_Express($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', 4);
    	$this->db->where('users.status!=','2');
    	$this->db->order_by('express.carrier_id','desc');
    	$this->db->select('express.*,users.status');
    	$this->db->from('express');
    	$this->db->join('users', 'users.profile_id = express.carrier_id');
        
    	$res = $this->db->get();
    //echo $this->db->last_query();exit;
    //print_r($res->result());exit;
    	return $res->result();
    }

public function getExpressServices(){

 		

 		$sql = "SELECT *
			    FROM services WHERE status!='3' Order by id DESC";

	    $ExpressData = $this->db->query($sql);
	 
	    if(isset($ExpressData)){

	    	return ($ExpressData->result());
	    }
	    return 0;
 	}
 	public function getAmenities(){

 		

 		$sql = "SELECT *
			    FROM amenities WHERE status!='3' Order by id DESC";

	    $amenitiesData = $this->db->query($sql);
	 
	    if(isset($amenitiesData)){

	    	return ($amenitiesData->result());
	    }
	    return 0;
 	}

}
?>