<?php 

class Regrequest_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;
 	}

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;
 	}

	

public function getGarageServices(){

 		

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

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

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


 	public function getGarageData($Garage_id='',$view=''){

 		
 		$sql = "SELECT garage.*
			    FROM garage
                INNER JOIN users AS USR ON (USR.profile_id=garage.garage_id)
			    WHERE garage.garage_id='$Garage_id' AND USR.user_type='2' Order by garage.garage_id DESC";

			   

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

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

 	public function getServiceData($Garage_id='',$view=''){

 		
 		$sql = "SELECT garage_services.*,services.service_name,sub_services.sub_services as subservice_name
			    FROM garage_services 
			    INNER JOIN sub_services ON (sub_services.id=garage_services.subservice_id)
			    INNER JOIN services ON (services.id=sub_services.id)
			    WHERE garage_services.garage_id='$Garage_id' AND garage_services.status='1' Order by garage_services.garage_id DESC";

	    $GarageData = $this->db->query($sql);

	    if(isset($GarageData)){

	    	return ($GarageData->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;
 	}



function updateGarage($Garage_id = '', $Garage_data = array()){

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

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

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

 		                 $upMecArr = array('name'=>$Garage_data['name'],
										   'email'=>$Garage_data['email'],
										   'code'=>$Garage_data['phone_code'],
										   'phone_no'=>$Garage_data['phone'],
										   'altered_no'=>$Garage_data['altered_no'],
										   'location'=>$Garage_data['location'],
										   'lat'=>$Garage_data['lat'],
										   'lng'=>$Garage_data['lng'],
										   'garage_code'=>$Garage_data['garage_code'],
										   'working_from'=>$Garage_data['working_from'],
										   'working_to'=>$Garage_data['working_to'],
										   'website'=>$Garage_data['website'],
										   'amenities' => implode(",",$Garage_data['amenities']),
										   'address'=>$Garage_data['address']);
                               
 		$admUpdateArr = array('username'=>$Garage_data['email']);
							  

 		if(isset($Garage_data['profile_image']) && !empty($Garage_data['profile_image'])){
 			$upMecArr['profile_pic'] = $Garage_data['profile_image'];
 			
 		}

 		$status = $this->db->update('users',$admUpdateArr,array('profile_id'=>$Garage_id,'user_type'=>'1'));
 		
 		if(!$status) { return 0; }


if (isset($Garage_data['services']) && isset($Garage_data['amount']) && 
	!empty($Garage_data['services']) && !empty($Garage_data['amount'])) {

      $this->db->where('garage_id',$Garage_id);
     $dltstatus = $this->db->delete('garage_services'); 
if($dltstatus) { 

    foreach ($Garage_data['services'] as $key => $service_id) {
        $amount   = $Garage_data['amount'][$key];
        $services = $Garage_data['services'][$key];

        $this->db->query("INSERT INTO garage_services (subservice_id, cost, garage_id) VALUES ('$services', '$amount', '$Garage_id')");
    } 

     }
}
 		


		$status = $this->db->update('garage',$upMecArr,array('garage_id'=>$Garage_id));

 		return $status;
 	}




public function getPosData($Pos_id=''){

 		
 		$sql = "SELECT * FROM pos WHERE pos_id='$Pos_id' Order by pos_id DESC";

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

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




function updatePos($Pos_id = '', $Pos_data = array()){

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

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

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

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

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

		$status = $this->db->update('pos',$upMecArr,array('pos_id'=>$Pos_id));

 		return $status;
 	}







}