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

class vehicle_model extends CI_Model{
	
	public function add_vehicle($data) { 
		return	$this->db->insert('vehicles',$data);
	}  
    /* public function getall_vehicle() {
      $this->db->select("*");
	  $this->db->from('vehicles');
	  $this->db->join('vehicle_group', 'vehicle_group.gr_id=vehicles.v_group','LEFT');
	  $this->db->order_by('v_id','desc');
	  $query = $this->db->get();
	  return $query->result_array();
	} */
	public function getall_vehicle() { 
		$organization_id=$this->session->userdata['session_data']['organization_id'];
		if($organization_id == "0"){
		  $this->db->select("*");
		  $this->db->from('vehicles');
		  $this->db->join('vehicle_group', 'vehicle_group.gr_id=vehicles.v_group','LEFT');
		  $this->db->order_by('v_id','desc');
		  $query = $this->db->get();
		  return $query->result_array();
		}else{
			  $this->db->select("*");
			  $this->db->from('vehicles');
			  $this->db->join('drivers', 'drivers.d_id=vehicles.v_driver_id');
			  $this->db->join('vehicle_group', 'vehicle_group.gr_id=vehicles.v_group','LEFT');	
				$this->db->where('drivers.organization_id', $organization_id);			  
			  $this->db->order_by('v_id','desc');
			  $query = $this->db->get();
			  return $query->result_array();
		}
			
     
	}  
	public function get_vehicledetails($v_id) { 
		return $this->db->select('*')->from('vehicles')->where('v_id',$v_id)->get()->result_array();
	} 
	public function getall_organizations() {
      $data=$this->db->select('*')->from('drivers')->join('organization', 'organization.org_id = drivers.d_id')->get()->result_array();
       // $data=$this->db->select('*')->from('organization')->get()->result_array();
      return $data;

	}
	public function edit_vehicle() { 
		$this->db->where('v_id',$this->input->post('v_id'));
		return $this->db->update('vehicles',$this->input->post());
	}
      public function edit_vehicle_group() { 
		$this->db->where('gr_id',$this->input->post('gr_id'));
		return $this->db->update('vehicle_group',$this->input->post());
	}
      public function get_vehiclegroups($gr_id) { 
		return $this->db->select('*')->from('vehicle_group')->where('gr_id',$gr_id)->get()->result_array();
	} 
	public function getall_bookings($v_id) { 
		$bookings = $this->db->select('*')->from('trips')->where('t_vechicle',$v_id)->order_by('t_id','desc')->get()->result_array();
		if(!empty($bookings)) {
			foreach ($bookings as $key => $tripdataval) {
				$newtripdata[$key] = $tripdataval;
				$newtripdata[$key]['t_customer_details'] =  $this->db->select('*')->from('customers')->where('c_id',$tripdataval['t_customer_id'])->get()->row();
				$newtripdata[$key]['t_driver_details'] =   $this->db->select('*')->from('drivers')->where('d_id',$tripdataval['t_driver'])->get()->row();
			}
			return $newtripdata;
		} else {
			return array();
		}

	} 
	public function get_vehiclegroup() { 
		return $this->db->select('*')->from('vehicle_group')->get()->result_array();
	}
	public function vehiclegroup_delete($gr_id) { 
		$groupinfo = $this->db->select('*')->from('vehicles')->where('v_group',$gr_id)->get()->result_array();
		if(count($groupinfo)>0) {
			return false;
		} else {
			$this->db->where('gr_id', $gr_id);
    		$this->db->delete('vehicle_group');
    		return true;
		}
	}
	public function get_vehicletype() { 
		 $this->db->select("*");
		  $this->db->from('vehicle_group');
		  $this->db->join('vehicle_type', 'vehicle_type.vt_group=vehicle_group.gr_id');
		  $this->db->order_by('id','desc');
		  $query = $this->db->get();
		  return $query->result_array();
	}
	public function vehicletype_delete($id) { 
	
			$this->db->where('id', $id);
    		$this->db->delete('vehicle_type');
    		return true;
		
	}
	public function edit_vehicle_type() { 

		$this->db->where('id',$this->input->post('id'));
		 $s_em=$this->input->post("vt_group");
        $s_na=$this->input->post("v_type");
        $image_name=$this->input->post('image'); 
        $image1_name=$this->input->post('sideimage'); 
         

       if($image_name!="" && $image1_name!="")
            {

		        $config['upload_path'] = 'assets/uploads/';
		        $config['allowed_types'] =  'jpg|jpeg|png'; 
		        $this->load->library('upload', $config);
		        if (!$this->upload->do_upload('file')&&!$this->upload->do_upload('file2'))
		        {
		            $error = array('error' => $this->upload->display_errors());
		        }
		        else
		        {
		            $upload_data=$this->upload->data();

		            $image_name=$upload_data['file_name'];
		            $image1_name=$upload_data['file_name1'];
		   

		        }
		}
		else{
					$image_name=$this->input->post('old');
					$image1_name=$this->input->post('old1');
				
				}
				$data=array('v_type'=>$s_na,'vt_group'=>$s_em,'image'=>$image_name,'sideimage'=>$image1_name);
				return $this->db->update('vehicle_type',$data);



if($image1_name!="")
            {
            	

		        $config['upload_path'] = 'assets/uploads/';
		        $config['allowed_types'] =  'jpg|jpeg|png'; 
		        $this->load->library('upload', $config);
		        if (!$this->upload->do_upload('file'))
		        {
		            $error = array('error' => $this->upload->display_errors());
		        }
		        else
		        {
		            $upload_data=$this->upload->data();
		            $image1_name=$upload_data['file_name'];
		        }
		}
		else{
					$image1_name=$this->input->post('old1');
				}
				$data=array('v_type'=>$s_na,'vt_group'=>$s_em,'sideimage'=>$image1_name);
				return $this->db->update('vehicle_type',$data);
			  
	}
	public function get_vehicletypes($id) { 
		return $this->db->select('*')->from('vehicle_type')->where('id',$id)->get()->result_array();
	}
} 