<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vehicle_Model extends CI_Model {
  
  private $tableName = 'vehicles';
  
  function __construct() 
  {
    /* Call the Model constructor */
    parent::__construct();
  }
  
  function get_all_vehicles() {
		$this->db->select($this->tableName.'.*');
		$this->db->order_by("vehicle_name", "desc");
		$this->db->where("vehicle_id IS NOT NULL");//("id !=",$id);
		$query = $this->db->get($this->tableName);
		return $query->result();
  }

	function save_vehicle($vehicle_data,$vehicle_id=false)
	{
		if (!$vehicle_id)
		{
			$this->db->insert($this->tableName,$vehicle_data);
		}else {
			$this->db->where('vehicle_id', $vehicle_id);
			return $this->db->update($this->tableName,$vehicle_data);
		}
	}	

		function get_all_vehicle_list() {
		$this->db->select($this->tableName.'.*');
		
		$this->db->order_by("vehicle_id", "desc");
		$this->db->where("vehicle_id IS NOT NULL");//("id !=",$id);
		$query = $this->db->get($this->tableName);
		return $query->result(); 
	}
	
		
	function get_all_vehicle() {
		$this->db->select('vehicles.*,warehouses.name');
		$this->db->from('warehouses');
		$this->db->join('vehicles', 'vehicles.warehouse_id = warehouses.id', 'left');
		$this->db->order_by('vehicle_id', 'desc');
		$this->db->where("vehicle_id IS NOT NULL");//("id !=",$id);
		$query = $this->db->get();
		
		
		//print_r($query);
		return $query->result_array();
	}
	
	public function get_all_vehicle_by_warehouse_id($warehouse_id)
	{
		$this->db->select('*');
		$this->db->from($this->tableName);
		$this->db->where("warehouse_id", $warehouse_id);
		$query = $this->db->get();
		$data=array();
 
//$data[0] = 'SELECT'; 
foreach ($query->result() as $row)
{
$data[$row->vehicle_id] = $row->vehicle_name;
}
return ($data);
}
		
	
	
	
	
	public function get_vehicle_info($id)
	 {
		$this->db->select('*');
		$this->db->from($this->tableName);
		$this->db->where("vehicle_id", $id);
		$this->db->order_by("vehicle_id", "desc");
		$query = $this->db->get();
		
		return $query->row_array(); 
	 }
	
	public function delete_vehicle($vehicle_id)
	{
		$this->db->where('vehicle_id', $vehicle_id);
		$this->db->delete($this->tableName);
	
	}

	public function disable_vehicle($vehicle_id)
	{
		$data = array(
			'vehicle_status' => 0
		);	
		$this->db->where('vehicle_id', $vehicle_id);
		$this->db->update($this->tableName, $data);
	}
	
	public function enable_vehicle($vehicle_id)
	{
		$data = array(
			'vehicle_status' => 1
		);	
		$this->db->where('vehicle_id', $vehicle_id);
		$this->db->update($this->tableName, $data);
	}
}