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

 	public function addVehicle($vehicle_data = array()){
 		if(empty($vehicle_data)){
 			return 0;
 		}
 		$typeChk = $this->db->get_where('vehicles',array('vehicle_type'=>$vehicle_data['vehicle_type'],'status !='=>'2'));
 		if(!empty($typeChk) && $typeChk->num_rows() > 0){
 			return 2;
 		}
 		$status = $this->db->insert('vehicles',$vehicle_data);
		return ($status)?1:0;
 	}

 	function getVehicle($vehicle_id = ''){
 		$cond = (!empty($vehicle_id))?" AND vehicle_id = '$vehicle_id'":"";

		$result = $this->db->query("SELECT * FROM vehicles WHERE status IN (0,1) $cond");
		if(empty($result)){
			return;
		}
		return (empty($vehicle_id))?$result->result():$result->row();
 	}

 	function changeStatus($vehicle_id = '', $status = '0'){
 		if(empty($vehicle_id)){
 			return 0;
 		}
 		$status = $this->db->update('vehicles',array('status'=>$status), array('vehicle_id'=>$vehicle_id));
 		return $status;
 	}

 	function updateVehicle($vehicle_id = '', $vehicle_data = array()){
 		if(empty($vehicle_id) || empty($vehicle_data)){
 			return 0;
 		}

 		$typeChk = $this->db->get_where('vehicles',array('vehicle_type'=>$vehicle_data['vehicle_type'],'status !='=>'2','vehicle_id !='=>$vehicle_id));
 		if(!empty($typeChk) && $typeChk->num_rows() > 0){
 			return 2;
 		}
 		if(empty($vehicle_data['vehicle_photo'])){
 			unset($vehicle_data['vehicle_photo']);
 		}
 		$status = $this->db->update('vehicles',$vehicle_data,array('vehicle_id'=>$vehicle_id));
		return ($status)?1:0;
 	}

}
?>