<?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 = ''){
 		$cond = (!empty($driver_id))?" AND driver_id = '$driver_id'":"";

 		$sql = "SELECT DRV.*, CMP.company_name
 				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)
 				WHERE DRV.status IN (0,1) 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;
 		}
 		$upArr = array('company_id'=>$driver_data['company_id'],'first_name'=>$driver_data['first_name'],'last_name'=>$driver_data['last_name'],'email_id'=>$driver_data['email_id'],'phone'=>$driver_data['phone']);
 		if(!empty($driver_data['profile_image'])){
 			$upArr['profile_image'] = $driver_data['profile_image'];
 		}
 		if(!empty($driver_data['licence'])){
 			$upArr['licence'] = $driver_data['licence'];
 		}
 		$status = $this->db->update('drivers', $upArr, array('driver_id'=>$driver_id));
		return ($status)?1:0;
 	}

}
?>