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

 	// public function getGarage_requestData($Garage_request_id='',$view=''){

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

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

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

	 //    	return (empty($Garage_request_id))?$Garage_requestData->result():$Garage_requestData->row();
	 //    }
	 //    return 0;
 	// }

function getGarage_requestData($Garage_request_id='') {
       
        
        $this->db->where('garage_request.status!=','2');
        $this->db->where('garage_request.id!=','$Garage_request_id');
        $this->db->order_by('garage_request.id','desc');
        $this->db->select("garage_request.*,sub_Garages.sub_Garages,GROUP_CONCAT(Garages.Garage_name) as Garage_name,customer.name as customer_name,customer.phone_no,customer.area,vehicle_model.make,vehicle_model.model,vehicle_model.year,vehicle_model.vehicle_name,vehicle_model.mileage,vehicle_model.vehicle_image");
        $this->db->from('garage_request');
        $this->db->join("sub_Garages","find_in_set(sub_Garages.id,garage_request.subGarage_ids)<> 0","left",false);
        
        $this->db->join('Garages', 'Garages.id = sub_Garages.Garage_id');
        $this->db->join('customer', 'customer.cust_id = garage_request.cust_id');
        $this->db->join('vehicle_model', 'vehicle_model.id = garage_request.vehicle_id');
        $res = $this->db->get();
        
        return $res->result();
    }



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

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

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



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

        $query = "SELECT * FROM Garage_request AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									   WHERE USR.status!='2' AND CUST.cust_id!='".$Garage_request_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_Garage_request($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('FIND_IN_SET(garage_request.garage_ids,33)');
    	//$this->db->where('garage_request.status!=','2');
        $this->db->order_by('garage_request.id','desc');
        $this->db->select("garage_request.*,sub_services.sub_services,GROUP_CONCAT(services.service_name) as service_name,customer.name as customer_name,customer.phone_no,customer.area,vehicle_model.make,vehicle_model.model,vehicle_model.year,vehicle_model.vehicle_name,vehicle_model.mileage");
        $this->db->from('garage_request');
        $this->db->join("sub_services","find_in_set(sub_services.id,garage_request.subservice_ids)<> 0","left",false);
        
        $this->db->join('services', 'services.id = sub_services.service_id','left');
        $this->db->join('customer', 'customer.cust_id = garage_request.cust_id','left');
        $this->db->join('vehicle_model', 'vehicle_model.id = garage_request.vehicle_id','left');
        $this->db->group_by('garage_request.id');
        $res = $this->db->get();
    	
    	
    	return $res->result();
    }
public function getGarageData(){

        
        $sql = "SELECT garage.garage_id,garage.garage_code,garage.name as garage_name
                FROM garage
                INNER JOIN users AS USR ON (USR.profile_id=garage.garage_id)
                WHERE USR.user_type='2' AND USR.status!='2' Order by garage.garage_id DESC";

               

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

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



    function Garage_assign($garages,$req_id){

        if(!isset($garages)){
            return 0;
        }
      $garage = implode(",",$garages);
    
    $status = $this->db->update('garage_request',array('garage_ids'=>$garage),array('id'=>$req_id));
        //echo $this->db->last_query();exit;
        return $status;
    }

public function getAssignGarage(){

        
        $sql = "SELECT GROUP_CONCAT(garage.name SEPARATOR '#') as garage_name,GROUP_CONCAT(garage.phone_no SEPARATOR '#') as phone_no,GROUP_CONCAT(garage.garage_code SEPARATOR '#') as garage_code,GROUP_CONCAT(garage.location SEPARATOR '#') as location
                FROM garage_request

                INNER JOIN garage ON FIND_IN_SET(garage.garage_id,garage_request.garage_ids)
                INNER JOIN users AS USR ON (USR.profile_id=garage.garage_id)
                WHERE USR.user_type='2' AND USR.status!='2' Order by garage.garage_id DESC";

               

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

//$datas = json_decode($data);
$garage = $datas[0];
$garage_name = explode('#', $garage->garage_name);
$phone_no = explode('#', $garage->phone_no);
$garage_code = explode('#', $garage->garage_code);
$location = explode('#', $garage->location);
$garage = array();
for($i=0;$i<count($garage_name);$i++){
    $gar = array(
                'garage_name'=>$garage_name[$i],
                'phone_no'=>$phone_no[$i],
                'garage_code'=>$garage_code[$i],
                'location'=>$location[$i]
                );
    $garage[] = $gar;
}





            return ($garage);


        }
        return 0;
    }

function approve_garages($datas){
        if(!isset($datas)){
            return 0;
        }
     $base=array("req_id"=>$datas['req_id'],"garage_id"=>$datas['garage_id'],"quote_type"=>'1',"labour_cost"=>$datas['bl_cost'],"parts_cost"=>$datas['bp_cost']);
      $medium=array("req_id"=>$datas['req_id'],"garage_id"=>$datas['garage_id'],"quote_type"=>'2',"labour_cost"=>$datas['ml_cost'],"parts_cost"=>$datas['mp_cost']);
       $premium =array("req_id"=>$datas['req_id'],"garage_id"=>$datas['garage_id'],"quote_type"=>'3',"labour_cost"=>$datas['pl_cost'],"parts_cost"=>$datas['pp_cost']);
  
$result = array($base,$medium,$premium);

foreach($result as $value){
    $status = $this->db->insert('garage_response',$value);
    }
        return $status;
    }



public function getGarage_QuoteData($request_id){

        
        $sql = "SELECT garage_response.*,garage_request.request_code FROM garage_response LEFT JOIN garage_request on garage_request.id = garage_response.req_id WHERE garage_response.req_id=$request_id Order by garage_response.id DESC";

               

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

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





}
?>