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

 	// public function getService_requestData($Service_request_id='',$view=''){

 	// 	$cond  = (!empty($view))?" USR.user_type IN ($view) ":" USR.status != '2' ";
 	// 	$cond .= (!empty($Service_request_id))?" AND CUST.cust_id='$Service_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";

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

	 //    	return (empty($Service_request_id))?$Service_requestData->result():$Service_requestData->row();
	 //    }
	 //    return 0;
 	// }

function getService_requestData($Service_request_id) {
       
       
        //$this->db->where('garage_request.status!=','2');
        $this->db->where('garage_request.id',$Service_request_id);
        $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,vehicle_model.vehicle_image");
        $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');
        $this->db->group_by('garage_request.id');
        
        $res = $this->db->get();
        //echo $this->db->last_query();exit;
        return $res->result();
    }



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

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

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



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

        $query = "SELECT * FROM Service_request AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
 									   WHERE USR.status!='2' AND CUST.cust_id!='".$Service_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_Service_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('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();
    	//echo $this->db->last_query();exit;
    	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,'status'=>'2'),array('id'=>$req_id));
      //echo $this->db->last_query();exit;
        return $status;
    }

public function getAssignGarage($request_id){

        
      
          $sql = $this->db->query("SELECT DISTINCT garage.name AS garage_name,garage.garage_code, garage.phone_no,garage.location,COALESCE(garage_response.id,0) AS gara_count, garage.garage_id AS id FROM garage_request LEFT JOIN garage ON FIND_IN_SET(garage.garage_id, garage_request.garage_ids) LEFT JOIN garage_response ON garage_response.garage_id = garage.garage_id WHERE garage_request.id = ".$request_id." GROUP BY garage.garage_id");
          //echo $this->db->last_query();exit;
         if($sql->num_rows() > 0){
            $garage = $sql->result_array();
         } else {
            $garage = array();
         }
         return $garage;
        }

public function getGarage_ServiceData($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_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');
        $this->db->join('customer', 'customer.cust_id = garage_request.cust_id');
        $this->db->join('vehicle_model', 'vehicle_model.id = garage_request.vehicle_id');
       $this->db->group_by('garage_request.id');
        $res = $this->db->get();
        
        return $res->result();
    
    }



}
?>