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

 	public function addDriver($driver_data = array()){
 		if(empty($driver_data)){
 			return 0;
 		}
 		$emailChk = $this->db->get_where('drivers',array('email_id'=>$driver_data['email_id'],'status !='=>'2'));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$phoneChk = $this->db->get_where('drivers',array('phone'=>$driver_data['phone'],'status !='=>'2'));
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
 			return 3;
 		}
 		$status = $this->db->insert('drivers',$driver_data);
		return ($status)?1:0;
 	}

 	function getDriver($driver_id = '',$status = '1',$company_id = ''){
 		$cond = '';
 		$user_id = $this->session->userdata('id');
 		if($this->session->userdata('user_type') != 1){
 			$cond = " AND CMP.company_id = '$user_id'";
 		}
 		$cond .= (!empty($driver_id))?" AND DRV.driver_id = '$driver_id'":"";
 		$cond .= (!empty($company_id))?" AND DRV.company_id = '$company_id'":"";

 		$sql = "SELECT DRV.*, CMP.company_name, VH.vehicle_type, VHS.vehicle_model, VHS.vehicle_reg_no,
 					   VHS.vehicle_reg_image, VHS.model
 				FROM drivers AS DRV
 				INNER JOIN company AS CMP ON (CMP.company_id = DRV.company_id)
 				INNER JOIN admin_users AS AU ON (AU.id = CMP.company_id)
 				LEFT JOIN vehicles AS VHS ON (VHS.vehicle_id = DRV.vehicle)
 				LEFT JOIN vehicle_types AS VH ON (VH.vehicle_id = DRV.vehicle_id)
 				WHERE DRV.status IN ($status) AND AU.status = '1' $cond";
		$result = $this->db->query($sql);
		if(empty($result)){
			return;
		}
		return (empty($driver_id))?$result->result():$result->row();
 	}

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

 	function updateDriver($driver_id = '', $driver_data = array()){
 		if(empty($driver_id) || empty($driver_data)){
 			return 0;
 		}
 		$emailChk = $this->db->get_where('drivers',array('email_id'=>$driver_data['email_id'],'status !='=>'2','driver_id != '=>$driver_id));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$phoneChk = $this->db->get_where('drivers',array('phone'=>$driver_data['phone'],'status !='=>'2','driver_id != '=>$driver_id));
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
 			return 3;
 		}
 		if(empty($driver_data['licence'])){
 			unset($driver_data['licence']);
 		}
 		if(empty($driver_data['company_id'])){
 			unset($driver_data['company_id']);
 		}
 		if(empty($driver_data['vehicle_id'])){
 			unset($driver_data['vehicle_id']);
 		}
 		if(empty($driver_data['profile_image'])){
 			unset($driver_data['profile_image']);
 		}
 		
 		$status = $this->db->update('drivers', $driver_data, array('driver_id'=>$driver_id));
		return ($status)?1:0;
 	}

}
?>