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

 	public function getGarage_responseData($Garage_response_id='',$view=''){

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

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

	    $Garage_responseData = $this->db->query($sql);
	  //echo $this->db->last_query();exit;
	    if(isset($Garage_responseData)){

	    	return (empty($Garage_response_id))?$Garage_responseData->result():$Garage_responseData->row();
	    }
	    return 0;
 	}





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

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

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



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

        $query = "SELECT * FROM Garage_response AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									   WHERE USR.status!='2' AND CUST.cust_id!='".$Garage_response_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_response($request_id,$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('garage_request.status!=','2');
       $this->db->where('garage_response.req_id',$request_id);
        $this->db->group_by('garage_response.id'); 
    	$this->db->order_by('garage_request.id','desc');
    	$this->db->select("garage_response.*,garage_request.requirement,garage_request.request_code,garage_request.status AS garage_request_status,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,garage.garage_code,garage.name as garage_name,garage.email as garage_email,garage.phone_no as garage_phone1,garage.altered_no as garage_phone2,garage.address as garage_address");
    	$this->db->from('garage_response');
        $this->db->join('garage', 'garage.garage_id = garage_response.garage_id');
        $this->db->join('garage_request', 'garage_request.id = garage_response.req_id');
    	$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');
        $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();
    	//echo $this->db->last_query();exit;
    	return $res->result();
    }
public function Garage_ResponseData($request_id){
 // print_r($request_id);exit;
       
        $this->db->where('garage_request.status!=','2');
        //$this->db->where('garage_request.id','$request_id');
        $this->db->order_by('garage_request.id','desc');
        $this->db->select("garage_response.*,garage_request.requirement,garage_request.request_code,garage_request.status AS garage_request_status,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,garage.garage_code,garage.name as garage_name,garage.email as garage_email,garage.phone_no as garage_phone1,garage.altered_no as garage_phone2,garage.address as garage_address");
        $this->db->from('garage_response');
        $this->db->join('garage', 'garage.garage_id = garage_response.garage_id');
        $this->db->join('garage_request', 'garage_request.id = garage_response.req_id');
        $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');
        $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();
    
    }



}
?>