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

 	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 addGarage($Garage_data = array()){

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

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

 		$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.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.phone_no='".$Garage_data['phone']."')");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) return 3;

    $status = $this->db->insert('garage', 
									 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'],
										   // 'working_hours'=>$Garage_data['working_hours'],
										   'profile_pic'=>$Garage_data['profile_image']));
		                       $Garage_id = $this->db->insert_id();

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

    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')");
    } 
}
 		
 
 				

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

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

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


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

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


       }
       return $data;	   
 		
 	}




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

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

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

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



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

        $query = "SELECT * FROM garage AS CUST 
 									   INNER JOIN users AS USR ON (USR.user_id=CUST.garage_id)
 									   WHERE USR.status!='2' AND CUST.id!='".$Garage_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_Garage($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', '2');
    	$this->db->where('users.status!=','2');
    	$this->db->order_by('garage.garage_id','desc');
    	$this->db->select('garage.*,users.status');
    	$this->db->from('garage');
    	$this->db->join('users', 'users.profile_id = garage.garage_id');
        
    	$res = $this->db->get();
    	//echo $this->db->last_query();exit;
    	//print_r($res->result());exit;
    	return $res->result();
    }

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


 	public function getSubservicesData($sub_id){

 		

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

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

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

}
?>