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

    public function getService_bookingData($Service_booking_id='',$view=''){

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

        $sql = "SELECT CUST.*,USR.status,USR.password
                FROM garage_booking 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";

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

            return (empty($Service_booking_id))?$Service_bookingData->result():$Service_bookingData->row();
        }
        return 0;
    }





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

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

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



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

        $query = "SELECT * FROM garage_booking AS CUST 
                                       INNER JOIN users AS USR ON (USR.profile_id=CUST.cust_id)
                                       WHERE USR.status!='2' AND CUST.cust_id!='".$Service_booking_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_booking($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_booking.garage_id',$garage_id);
        $this->db->group_by('garage_booking.id');
        $this->db->order_by('garage_booking.id','desc');
        $this->db->select("garage_booking.*,customer.name as customer_name,customer.phone_no,customer.area,GROUP_CONCAT(sub_services.sub_services) as service_name,vehicle_model.make,vehicle_model.model,vehicle_model.year,vehicle_model.vehicle_name,vehicle_model.mileage,garage.name as garage_name,garage.garage_code,garage.phone_no as garage_phone");
        $this->db->from('garage_booking');
        $this->db->join("sub_services","find_in_set(sub_services.id,garage_booking.subservices_ids)<> 0","left",false);
        $this->db->join('garage','garage.garage_id = garage_booking.garage_id','left');
        $this->db->join('services','services.id = sub_services.service_id','left');
        $this->db->join('customer','customer.cust_id = garage_booking.cust_id','left');
        $this->db->join('vehicle_model', 'vehicle_model.id = garage_booking.vehicle_id','left');
        $res = $this->db->get();
        
        return $res->result();
    }


public function Garage_BookingData($request_id){
 // print_r($request_id);exit;
       
        //$this->db->where('garage_booking.status!=','2');
    $this->db->where('garage_booking.id',$request_id);
        $this->db->order_by('garage_booking.id','desc');
        $this->db->select("`garage_booking`.booking_id,garage_booking.cost,garage_booking.requirement,garage_booking.booking_date,sub_services.sub_services,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_booking');
        $this->db->join("sub_services","find_in_set(sub_services.id,garage_booking.subservices_ids)<> 0","left",false);
        
       
        $this->db->join('customer', 'customer.cust_id = garage_booking.cust_id');
        $this->db->join('vehicle_model', 'vehicle_model.id = garage_booking.vehicle_id');
       $this->db->group_by('garage_booking.id');
        $res = $this->db->get();
        //echo $this->db->last_query();exit;
        return $res->result();
    
    }






}
?>