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

 	public function addMechanic($mechanic_data = array()){
 		if(empty($mechanic_data))
 			return 0;
 		$userIdChk = $this->db->query("SELECT * FROM mechanic AS MECH 
 									   INNER JOIN admin_users AS AU ON (AU.id = MECH.mechanic_id)
 									   WHERE AU.status!='2' AND 
								   			 AU.username='".$mechanic_data['username']."'");
 		if(!empty($userIdChk) && $userIdChk->num_rows() > 0) return 4;

 		$emailChk = $this->db->query("SELECT * FROM mechanic AS MECH 
 									   INNER JOIN admin_users AS AU ON (AU.id = MECH.mechanic_id)
 									   WHERE AU.status!='2' AND 
								   			 MECH.email_id='".$mechanic_data['email_id']."'");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) return 2;

 		$phoneChk = $this->db->query("SELECT * FROM mechanic AS MECH 
 									   INNER JOIN admin_users AS AU ON (AU.id = MECH.mechanic_id)
 									   WHERE AU.status!='2' AND 
								   			 MECH.phone='".$mechanic_data['phone']."'");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) return 3;

 		$status = $this->db->insert('admin_users',
										array('username'=>$mechanic_data['username'],
											  'password'=>$mechanic_data['password'],
											  'display_name'=>$mechanic_data['display_name'],
											  'profile_image'=>$mechanic_data['profile_image'],
											  'user_type'=>'2','status'=>'1'));
 		if(!$status){
 			return 0;
 		}
 		$mechamic_id = $this->db->insert_id();
		$status = $this->db->insert('mechanic', 
										array('mechanic_id'=>$mechamic_id,
											  'city'=>$mechanic_data['city'],
											  'phone'=>$mechanic_data['phone'],
											  'state'=>$mechanic_data['state'],
											  'shop_id'=>$mechanic_data['shop_id'],
											  'address'=>$mechanic_data['address'],
											  'licence'=>$mechanic_data['licence'],
											  'email_id'=>$mechanic_data['email_id'],
											  'last_name'=>$mechanic_data['last_name'],
											  'first_name'=>$mechanic_data['first_name'],
											  'licence_number'=>$mechanic_data['licence_number'],
											  'licence_exp_date'=>$mechanic_data['licence_exp_date']));
 		return $status;
 	}

 	function getMechanic($mechanic_id = '', $view_all = 0){
 		$cond = (!empty($mechanic_id))?" MECH.mechanic_id = '".$mechanic_id."' ":"";
 		$cond .= (!empty($cond))?" AND ":$cond;
 		$cond .= (!empty($view_all))?" ADMN.status IN (0,1) ":" ADMN.status IN (1) ";

		$sql = "SELECT ADMN.username,ADMN.user_type,ADMN.display_name,ADMN.profile_image,ADMN.status, 
				       MSH.shop_name, MSH.address AS shop_address, MSH.phone AS shop_phone, 
				       MSH.email_id AS shop_email, MECH.* 
		        FROM mechanic AS MECH 
			    INNER JOIN admin_users AS ADMN ON (ADMN.id = MECH.mechanic_id)
			    LEFT JOIN mechanic_shop AS MSH ON (MSH.shop_id = MECH.shop_id AND MSH.status = '1')
			    WHERE ".$cond;

		$result = $this->db->query($sql);
		if(empty($result)){
			return;
		}
		return (empty($mechanic_id))?$result->result():$result->row();
 	}

 	function updateMechanic($mechanic_id = '', $mechanic_data = array()){
 		if(empty($mechanic_id) || empty($mechanic_data))
 			return 0;
 		$userIdChk = $this->db->query("SELECT * FROM mechanic AS MECH 
 									   INNER JOIN admin_users AS AU ON (AU.id = MECH.mechanic_id)
 									   WHERE AU.status!='2' AND AU.id!='".$mechanic_id."' AND 
								   			 AU.username='".$mechanic_data['username']."'");
 		if(!empty($userIdChk) && $userIdChk->num_rows() > 0) { return 4; }

 		$emailChk = $this->db->query("SELECT * FROM mechanic AS MECH 
 									  INNER JOIN admin_users AS AU ON (AU.id = MECH.mechanic_id)
 									  WHERE AU.status!='2' AND AU.id!='".$mechanic_id."' AND 
								   			MECH.email_id='".$mechanic_data['email_id']."'");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) { return 2; }

 		$phoneChk = $this->db->query("SELECT * FROM mechanic AS MECH 
 									  INNER JOIN admin_users AS AU ON (AU.id = MECH.mechanic_id)
 									  WHERE AU.status!='2' AND AU.id!='".$mechanic_id."' AND 
								   			MECH.phone='".$mechanic_data['phone']."'");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) { return 3; }

 		$admUpdateArr = array('username'=>$mechanic_data['username'],
							  'display_name'=>$mechanic_data['display_name']);
 		if(isset($mechanic_data['profile_image']) && !empty($mechanic_data['profile_image']))
 			$admUpdateArr['profile_image'] = $mechanic_data['profile_image'];

 		$status = $this->db->update('admin_users',$admUpdateArr,array('id'=>$mechanic_id));
 		if(!$status) { return 0; }

 		$upMecArr = array('city'=>$mechanic_data['city'],'first_name'=>$mechanic_data['first_name'],
 						  'state'=>$mechanic_data['state'],'shop_id'=>$mechanic_data['shop_id'],
 						  'address'=>$mechanic_data['address'],'email_id'=>$mechanic_data['email_id'],
 						  'last_name'=>$mechanic_data['last_name'],'phone'=>$mechanic_data['phone'],
 						  'licence_number'=>$mechanic_data['licence_number'],
 						  'licence_exp_date'=>$mechanic_data['licence_exp_date']);

 		if(isset($mechanic_data['licence']) && !empty($mechanic_data['licence']))
 			$upMecArr['licence'] = $mechanic_data['licence'];
 		
		$status = $this->db->update('mechanic',$upMecArr,array('mechanic_id'=>$mechanic_id));
 		return $status;
 	}

 	function changeStatus($mechanic_id = '', $status = '0'){
 		if(empty($mechanic_id)){
 			return 0;
 		}
 		$status = $this->db->update('admin_users',array('status'=>$status),array('id'=>$mechanic_id));
 		return $status;
 	}
}
?>