<?php 

class User_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}
		
 	function getUserData($user_id = '', $user_type = ''){
 		$user_id = (empty($user_id))?$this->session->userdata('id'):$user_id;
 		$user_type = (empty($user_type))?$this->session->userdata('user_type'):$user_type;

		$result = $this->db->get_where('admin_users',array('status'=>'1','id'=>$user_id));
 		if(empty($result)){
 			return 0;
 		}
 		$result = $result->row();
 		$result->mechanic_data = '';
		$result->mechanic_shop_data = '';
		if($user_type == 2){
			$this->load->model('Mechanic_model');
			$result->mechanic_data = $this->Mechanic_model->getMechanic($user_id);

			if(!empty($result->mechanic_data->shop_id)){
				$this->load->model('Shop_model');
				$shop_data = $this->Shop_model->getShop($result->mechanic_data->shop_id);

				if(!empty($shop_data)){
					$result->mechanic_shop_data = $shop_data;
				}
			}
		}
		return $result;
 	}

 	function updateUser($user_id = '',$user_type = '',$user_data = array()){
 		if(empty($user_id) || empty($user_type) || empty($user_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!='".$user_id."' AND 
								   			 AU.username='".$mechanic_data['username']."'");
 		if(!empty($userIdChk) && $userIdChk->num_rows() > 0){
 			return 4;
 		}
 		if($user_type == 2){
	 		$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!='".$user_id."' AND 
									   		MECH.email_id='".$user_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!='".$user_id."' AND 
									   		MECH.phone='".$user_data['phone']."'");
	 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
	 			return 3;
	 		}
 		}
 		$admUpArr = array('username'=>$user_data['username'],'display_name'=>$user_data['display_name']);
 		if(!empty($user_data['profile_image'])){
 			$admUpArr['profile_image'] = $user_data['profile_image'];
 		}
 		if(!empty($user_data['password'])){
 			$admUpArr['password'] = $user_data['password'];
 		}
 		$status = $this->db->update('admin_users',$admUpArr,array('id'=>$user_id));
 		if(!$status){
 			return 0;
 		}

 		if($user_type == 2){
 			$insertArr = array('first_name'=>$user_data['first_name'],'email_id'=>$user_data['email_id'],
				 			   'address'=>$user_data['address'],'city'=>$user_data['city'],
				 			   'state'=>$user_data['state'],'shop_id'=>$user_data['store'],
				 			   'last_name'=>$user_data['last_name'],'phone'=>$user_data['phone'],
				 			   'licence_number'=>$user_data['licence_number'],
				 			   'licence_exp_date'=>$user_data['licence_exp_date']);
	 		if(!empty($user_data['licence'])){
	 			$insertArr['licence'] = $user_data['licence'];
	 		}
 			$status = $this->db->update('mechanic', $insertArr, array('mechanic_id'=>$user_id));
 		}
		$usrData = $this->getUserData($user_id,$user_type);
		if(!empty($usrData)){
			$this->session->set_userdata('user',$usrData);
			$this->session->set_userdata('mechanic_data',$usrData->mechanic_data);
			$this->session->set_userdata('mechanic_shop_data',$usrData->mechanic_shop_data);
		}
 		return $status;
 	}
}
?>