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

 	function addVehicle($vehicle_data = array()){
 		if(empty($vehicle_data)){
 			return 0;
 		}
 		$status = $this->db->insert('customer_vehicle',$vehicle_data);
 		return ($status)?$this->db->insert_id():'0';
 	}

 	function changeStatus($vehicle_id = '', $action = '0'){
 		if(empty($vehicle_id)){
 			return 0;
 		}
 		if($action == 3){
			$status = $this->db->delete('customer_vehicle',array('customer_veh_id'=>$vehicle_id));
 		} else {
 			$status = $this->db->update('customer_vehicle',array('status'=>$action),
 														   array('customer_veh_id'=>$vehicle_id));
 		}
 		return $status;
 	}

 	public function scheduleBooking($postData = array()){
      	if(empty($postData)){
        	return 0;
      	}
		$vehData = $postData['vechile_info'];
		$car_name = $vehData['modelYear'].' '.$vehData['maker'].' '.$vehData['modelName'];

      	$vehJson = array('vehicle' => $car_name,
                         'attributes' => 
							array(
								'Year'      => $vehData['modelYear'],
								'Make'      => $vehData['maker'],
								'Trim'      => $vehData['trim'],
								'Model'     => $vehData['modelName'],
								'Engine'    => $vehData['emgine']
							));

      	$insert_array = array(
	  						'customer_id'   => $postData['customer_id'],
	                        'car_name'      => $car_name,
	                        'car_model'     => $vehData['modelName'],
	                        'car_maker'     => $vehData['maker'],
	                        'car_loc_lat'   => $postData['pickup_data']['pickup_lat'],
	                        'car_loc_lng'   => $postData['pickup_data']['pickup_lng'],
	                        'car_location'  => $postData['pickup_data']['pickup_loc'],
	                        'vehicle_data'  => json_encode($vehJson),
	                        'car_model_year'=> $vehData['modelYear'],
	                        'status'        => '3');

		if($this->db->insert('customer_vehicle',$insert_array)){
			$last_id = $this->db->insert_id();

			$book_data = array(
								'customer_veh_id'  => $last_id,
								'customer_id'      => $postData['customer_id'],
								'mechanic_id'      => $postData['mechanic_id'],
								'scheduled_date'   => $postData['schedule_date']['date'],
								'scheduled_time'   => $postData['schedule_date']['time'],
								'mileage'          => $vehData['milage'],
								'status'           => '0');

			if($this->db->insert('bookings',$book_data)){
			  	return 1;
			}
		}
      	return 0;
    }
}
?>