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

 	public function getRoad_assistData($road_assist_id='',$view=''){

 		
 		$sql = "SELECT *
			    FROM road_assist  
			  
			    WHERE road_assist_id='$road_assist_id' Order by road_assist_id DESC";

	    $Road_assistData = $this->db->query($sql);
	
	    
	    if(isset($Road_assistData)){

	    	return ($Road_assistData->row());
	    }
	    return 0;
 	}

 	public function addRoad_assist($Road_assist_data = array()){

	if(empty($Road_assist_data))
 			return 0;
          $userNameChk = $this->db->query("SELECT * FROM users 
 									     WHERE  user_type='2' AND username='".$Road_assist_data['email']."'");
 		if(!empty($userNameChk) && $userNameChk->num_rows() > 0) return 2; 

 		 // $userNameChk = $this->db->query("SELECT * FROM Road_assist AS CUST 
 			// 						   INNER JOIN users AS USR ON (USR.profile_id=CUST.id)
 			// 						   WHERE USR.status!='2' AND CUST.id!='".$road_assist_id."' AND 
				// 				   			 USR.username='".$Road_assist_data['email']."'");
 		 // if(!empty($userNameChk) && $userNameChk->num_rows() > 0) return 2;

 		$emailChk = $this->db->query("SELECT * FROM road_assist AS CUST 
 									  
								      WHERE  (CUST.email_id='".$Road_assist_data['email']."')");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) return 2;

 		$phoneChk = $this->db->query("SELECT * FROM road_assist AS CUST 
 									 
									  WHERE (CUST.phone_no='".$Road_assist_data['phone']."')");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) return 3;

    $status = $this->db->insert('road_assist', 
									 array('name'=>$Road_assist_data['name'],
										   'email_id'=>$Road_assist_data['email'],
										   'code'=>$Road_assist_data['phone_code'],
										   'phone_no'=>$Road_assist_data['phone'],
										   'altered_no'=>$Road_assist_data['altered_no'],
										   'road_assist_code'=>$Road_assist_data['road_assist_code'],
										   'address'=>$Road_assist_data['address'],
										    'working_from'=>$Road_assist_data['working_from'],
										  'working_to'=>$Road_assist_data['working_to'],
										   'profile_photo'=>$Road_assist_data['profile_image']));
		                       $road_assist_id = $this->db->insert_id();

if (isset($Road_assist_data['services']) && isset($Road_assist_data['amount']) && 
	!empty($Road_assist_data['services']) && !empty($Road_assist_data['amount'])) {

    foreach ($Road_assist_data['services'] as $key => $service_id) {
        $amount   = $Road_assist_data['amount'][$key];
        $services = $Road_assist_data['services'][$key];

        $this->db->query("INSERT INTO road_assist_services (subservice_id, cost, road_assist_id) VALUES ('$services', '$amount', '$road_assist_id')");
    } 
}
 		
 

		if(empty($road_assist_id) && !isset($road_assist_id)){
 			return 0;
 		}else{

         $status = $this->db->insert('users', array('user_type'=>'3',
										   'profile_id'=>$road_assist_id,
										   'username'=>$Road_assist_data['email'],
										   'phone_no'=>$Road_assist_data['phone'],
										   'password'=>$Road_assist_data['password']));

		$insertId = $this->db->insert_id();								  
        


        if(empty($insertId) && !isset($insertId)){
 			return 0;
 		}else{

   
    $data['status']=1;
 		}


       }
       return $data;	   
 		
 	}




 	function updateRoad_assist($road_assist_id = '', $Road_assist_data = array()){

 		
 		if(empty($road_assist_id) || empty($Road_assist_data))
 			return 0;
 		$userIdChk = $this->db->query("SELECT * FROM road_assist AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.road_assist_id)
 									   WHERE CUST.road_assist_id!='".$road_assist_id."' AND 
								   			 USR.username='".$Road_assist_data['email']."'");
 		if(!empty($userIdChk) && $userIdChk->num_rows() > 0) { return 4; }

 		$emailChk = $this->db->query("SELECT * FROM road_assist AS CUST 
 									  INNER JOIN users AS USR ON (USR.profile_id=CUST.road_assist_id)
 									  WHERE CUST.road_assist_id!='".$road_assist_id."' AND 
								   			CUST.email_id='".$Road_assist_data['email']."'");
 		if(!empty($emailChk) && $emailChk->num_rows() > 0) { return 2; }

 		$phoneChk = $this->db->query("SELECT * FROM road_assist AS CUST 
 									  INNER JOIN users AS USR ON (USR.profile_id=CUST.road_assist_id)
 									  WHERE CUST.road_assist_id!='".$road_assist_id."' AND 
								   			CUST.phone_no='".$Road_assist_data['phone']."'");
 		if(!empty($phoneChk) && $phoneChk->num_rows() > 0) { return 3; }

 		                 $upMecArr = array('name'=>$Road_assist_data['name'],
										   'email_id'=>$Road_assist_data['email'],
										   'code'=>$Road_assist_data['phone_code'],
										   'phone_no'=>$Road_assist_data['phone'],
										   'altered_no'=>$Road_assist_data['altered_no'],
										   'road_assist_code'=>$Road_assist_data['road_assist_code'],
										   'address'=>$Road_assist_data['address'],
										    'working_from'=>$Road_assist_data['working_from'],
										  'working_to'=>$Road_assist_data['working_to'],
										   'profile_photo'=>$Road_assist_data['profile_image']);
                               
 		$admUpdateArr = array('username'=>$Road_assist_data['email']);
							  

 		if(isset($Road_assist_data['profile_image']) && !empty($Road_assist_data['profile_image'])){
 			$upMecArr['profile_photo'] = $Road_assist_data['profile_image'];
 		
 		}

 		$status = $this->db->update('users',$admUpdateArr,array('profile_id'=>$road_assist_id,'user_type'=>'1'));
 		
 		if(!$status) { return 0; }
if (isset($Road_assist_data['services']) && isset($Road_assist_data['amount']) && 
	!empty($Road_assist_data['services']) && !empty($Road_assist_data['amount'])) {

      $this->db->where('road_assist_id',$road_assist_id);
     $dltstatus = $this->db->delete('road_assist_services'); 
if($dltstatus) { 

    foreach ($Road_assist_data['services'] as $key => $service_id) {
        $amount   = $Road_assist_data['amount'][$key];
        $services = $Road_assist_data['services'][$key];

        $this->db->query("INSERT INTO road_assist_services (subservice_id, cost, road_assist_id) VALUES ('$services', '$amount', '$road_assist_id')");
    } 

     }
}
		$status = $this->db->update('road_assist',$upMecArr,array('road_assist_id'=>$road_assist_id));

 		return $status;
 	}

 	function changeStatus($road_assist_id = '', $status = '0'){

 		if(empty($road_assist_id)){
 			return 0;
 		}

 		$status = $this->db->update('users',array('status'=>$status),array('profile_id'=>$road_assist_id,'user_type'=>'1'));
 		//echo $this->db->last_query();exit;
 		return $status;
 	}



function email_availability_check($email)
    {
if($email!=''){

        $query = "SELECT * FROM road_assist AS CUST 
 									   INNER JOIN users AS USR ON (USR.profile_id=CUST.road_assist_id)
 									   WHERE CUST.road_assist_id!='".$road_assist_id."' AND 
								   			 USR.username='".$email."'";
        $query=$this->db->query($query,array($email));
        if($query->num_rows()>0)
        {
            return $query->row_array();
        } else
        {
            return false;
        }

        }
    return false;
    }

public function get_all_Road_assist($filter=null) {
    	if($filter){
    		$this->db->order_by($filter['order_by'], $filter['order_type']);
    		if(!empty($filter['where'])){
    			$this->db->where($filter['where']);
    		}
    		if($filter['length']!=-1)
    			$this->db->limit($filter['length'],$filter['start']);
    	}
    	
    	$this->db->where('users.status!=','2');
    	$this->db->order_by('road_assist.road_assist_id','desc');
    	$this->db->select('road_assist.*,users.status');
    	$this->db->from('road_assist');
    	$this->db->join('users', 'users.profile_id = road_assist.road_assist_id');
        $this->db->group_by('road_assist.road_assist_code');
    	$res = $this->db->get();
    	
    	return $res->result();
    }

public function getRoad_assistServices(){

 		$sql = "SELECT *
			    FROM sub_services WHERE status!='2' Order by id DESC";

	    $GarageData = $this->db->query($sql);
	 
	    if(isset($GarageData)){

	    	return ($GarageData->result());
	    }
	    return 0;
 	}

public function getServiceData($Garage_id='',$view=''){

 		
 		$sql = "SELECT road_assist_services.*,services.service_name,sub_services.sub_services as subservice_name
			    FROM road_assist_services 
			    INNER JOIN sub_services ON (sub_services.id=road_assist_services.subservice_id)
			    INNER JOIN services ON (services.id=sub_services.id)
			    WHERE road_assist_services.road_assist_id='$Garage_id' AND road_assist_services.status='1' Order by road_assist_services.road_assist_id DESC";

	    $GarageData = $this->db->query($sql);

	    if(isset($GarageData)){

	    	return ($GarageData->result());
	    }
	    return 0;
 	}


 	public function getGarageServices(){

 		

 		$sql = "SELECT *
			    FROM sub_services WHERE status!='2' Order by id DESC";

	    $GarageData = $this->db->query($sql);
	 
	    if(isset($GarageData)){

	    	return ($GarageData->result());
	    }
	    return 0;
 	}
 	public function getAmenities(){

 		

 		$sql = "SELECT *
			    FROM amenities WHERE status!='3' Order by id DESC";

	    $amenitiesData = $this->db->query($sql);
	 
	    if(isset($amenitiesData)){

	    	return ($amenitiesData->result());
	    }
	    return 0;
 	}

}
?>