<?php 
class Garage_profile_model extends CI_Model {
	public function _consruct(){
		parent::_construct();
 	}
		
 	function getAdminData($user_id = ''){
 		if(empty($user_id)){
 			return 0;
 		}
 		
		$query = $this->db->get_where('admin',array('id'=>$user_id));

		if($query->num_rows() > 0 && !empty($query)){
			$result = $query->row();
            return $result;
			
			}
			return 0;
			
		}

 	function updateUser($user_id = '',$user_data = array()){
 		if(empty($user_id) || empty($user_data)){
 			return 0;
 		}

	
 			$userData['username'] = $user_data['username'];
 			
 			if(isset($user_data['password']) && !empty($user_data['password'])){
 				$userData['password'] = $user_data['password'];
 			}
 			if(isset($user_data['profile_image']) && !empty($user_data['profile_image'])){
 				$userData['profile_picture'] = $user_data['profile_image'];
 			}
 			
 			$this->db->update('admin',$userData,array('id'=>$user_id));

			if(!empty($usrData)){
				$this->session->set_userdata('id',$usrData->id);
				
				$this->session->set_userdata('username',$usrData->username);
				$this->session->set_userdata('logged_in','1');
				$this->session->set_userdata('user_type','1');
			}
			return 1;
 		}



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




 		
 	
}
?>