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

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

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

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

 	function changeVehicleTypeStatus($vehicle_id = '', $status = '0'){
 		if(empty($vehicle_id)){
 			return 0;
 		}
 		if($status == 2){
 			$status = $this->db->delete('vehicle_types',array('vehicle_id' => $vehicle_id));
 			return $status;
 		}
 		$status = $this->db->update('vehicle_types',array('status'=>$status), array('vehicle_id'=>$vehicle_id));
 		return $status;
 	}

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

 		$typeChk = $this->db->get_where('vehicle_types',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('vehicle_types',$vehicle_data,array('vehicle_id'=>$vehicle_id));
		return ($status)?1:0;
 	}

 	function createVehicle($vehicle_data = array()){
 		if(empty($vehicle_data)){
 			return 0;
 		}
 		$status = $this->db->insert("vehicles",$vehicle_data);
		return ($status)?1:0;
 	}

 	function getVehiclesData($vehicle_id = '',$vechile_type = ''){
 		$cond = (!empty($vehicle_id))?" AND vehicle_id = '$vehicle_id'":"";
 		$cond = (!empty($vechile_type))?" AND vehicle_type = '$vechile_type'":"";

		$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 changeVehicleStatus($vehicle_id = '', $status = ''){
 		if(empty($vehicle_id) || $status == ''){
 			return 0;
 		}
 		$status = $this->db->update('vehicles',array('status'=>$status), array('vehicle_id'=>$vehicle_id));
 		return ($status)?1:0;
 	}

 	function updateVehicle($vehicle_id = '', $vehicle_data = array()){
 		if(empty($vehicle_id) || empty($vehicle_data)){
 			return 0;
 		}
 		$status = $this->db->update('vehicles',$vehicle_data, array('vehicle_id'=>$vehicle_id));
 		return ($status)?1:0;
 	}
}
?>