<?php
	defined('BASEPATH')OR exit('No direct script access allowed');
	header('Content-Type: text/html; charset=utf-8');
	
	if(isset($_SERVER['HTTP_ORIGIN'])) {
		header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
		header('Access-Control-Allow-Credentials: true');
		header('Access-Control-Max-Age: 86400'); // cache for 1 day
	}
	
	if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
		if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
		header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
		if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
		header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
		exit(0);
	}
	
	class Webservice extends CI_Controller {
		public function __construct() {
			parent::__construct();
			$this->load->model('Webservice_model');
		}
		//User Signup
		public function user_signup(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			$err = 0;
			$msg = '';
			if(!isset($postData['user_name']) || empty($postData['user_name'])){
				$err = 1;
				$msg = 'Provide a valid Username';
			} 
			else if(!isset($postData['user_password']) || empty($postData['user_password'] = md5($postData['user_password']))){
				$err = 1;
				$msg = 'Provide a Password';
			}
			else if(!isset($postData['user_email']) || empty($postData['user_email'])){
				$err = 1;
				$msg = 'Provide valid Email';
			}
			else if(!isset($postData['user_phonecode']) || empty($postData['user_phonecode'])){
				$err = 1;
				$msg = 'Provide Phone Code';
			}
			else if(!isset($postData['user_phonenumber']) || empty($postData['user_phonenumber'])){
				$err = 1;
				$msg = 'Provide valid Phone Number';
			}
			else if(!isset($postData['user_country']) || empty($postData['user_country'])){
				$err = 1;
				$msg = 'Provide valid Country';
			}
			else if(!isset($postData['user_area']) || empty($postData['user_area'])){
				$err = 1;
				$msg = 'Provide valid Area';
			}
			else if(!isset($postData['user_address']) || empty($postData['user_address'])){
				$err = 1;
				$msg = 'Provide an Address';
			}
			else if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
				$err = 1;
				$msg = 'Provide User Location';
			}
			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
				$err = 1;
				$msg = 'Provide User Location';
			}
			else if(!isset($postData['device_id']) || empty($postData['device_id'])){
				$err = 1;
				$msg = 'Device Id is required';
			}
			if($err == 1){
				$respArr['message'] = $msg;
				echo json_encode($respArr);exit;
			}
			$custResp = $this->Webservice_model->createCustomer($postData);
			if(empty($custResp)){
				echo json_encode($respArr);exit;
			}
			if($custResp == '1'){
				$custResp = $this->Webservice_model->checkCustomerLogin($postData);
				if(empty($custResp) || !isset($custResp['status']) || empty($custResp['status'])){
					echo json_encode($respArr);exit;
				}
				if($custResp['status'] == '1'){
					$respArr['data'] = $custResp['data'];
					$respArr['status'] = 'success';
					$respArr['message'] = 'Success';
					echo json_encode($respArr);exit;
				}
			} else if($custResp == '2'){
				$respArr['status'] = '2';
				$respArr['message'] = 'Email Address already in use';
				echo json_encode($respArr);exit;
			} else if($custResp == '3'){
				$respArr['status'] = '2';
				$respArr['message'] = 'Phone already in use';
				echo json_encode($respArr);exit;
			}
			echo json_encode($respArr); exit;
		}
		//User Login
		public function user_login(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			$err = 0;
			$msg = '';

			if(!isset($postData['user_email']) || empty($postData['user_email'])){
				$err = 1;
				$msg = 'Provide a valid Email';
			} 
			else if(!isset($postData['user_password']) || empty($postData['user_password'] = md5($postData['user_password']))){
				$err = 1;
				$msg = 'Provide a Password';
			}
			else if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
				$err = 1;
				$msg = 'Provide User Location';
			}
			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
				$err = 1;
				$msg = 'Provide User Location';
			}
			else if(!isset($postData['device_id']) || empty($postData['device_id'])){
				$err = 1;
				$msg = 'Device Id is Required';
			}
			else if(!isset($postData['user_type']) || empty($postData['user_type'])){
				$err = 1;
				$msg = 'User Type is Required';
			}

			if($err == 1){
				$respArr['message'] = $msg;
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->userLogin($postData);
			echo json_encode($respArr);exit;
		}
		//Forgot Password
		public function update_profile(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			$err = 0;
			$msg = '';
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$err = 1;
				$msg = 'User Id is Required';
			} 
			else if(!isset($postData['user_name']) || empty($postData['user_name'])){
				$err = 1;
				$msg = 'Provide a valid Username';
			} 
			else if(!isset($postData['user_password']) || empty($postData['user_password'] = md5($postData['user_password']))){
				$err = 1;
				$msg = 'Provide a Password';
			}
			else if(!isset($postData['user_email']) || empty($postData['user_email'])){
				$err = 1;
				$msg = 'Provide valid Email';
			}
			else if(!isset($postData['user_phonecode']) || empty($postData['user_phonecode'])){
				$err = 1;
				$msg = 'Provide Phone Code';
			}
			else if(!isset($postData['user_phonenumber']) || empty($postData['user_phonenumber'])){
				$err = 1;
				$msg = 'Provide valid Phone Number';
			}
			else if(!isset($postData['user_country']) || empty($postData['user_country'])){
				$err = 1;
				$msg = 'Provide valid Country';
			}
			else if(!isset($postData['user_area']) || empty($postData['user_area'])){
				$err = 1;
				$msg = 'Provide valid Area';
			}
			else if(!isset($postData['user_address']) || empty($postData['user_address'])){
				$err = 1;
				$msg = 'Provide an Address';
			}
			else if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
				$err = 1;
				$msg = 'Provide User Location';
			}
			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
				$err = 1;
				$msg = 'Provide User Location';
			}
			if($err == 1){
				$respArr['message'] = $msg;
				echo json_encode($respArr);exit;
			}
			$custResp = $this->Webservice_model->updateCustomer($postData);
			if(empty($custResp)){
				echo json_encode($respArr);exit;
			}
			if($custResp == '1'){
				$respArr['status'] = 'success';
				$respArr['message'] = 'Success';
				echo json_encode($respArr);exit;
			} else if($custResp == '2'){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Email Address already in use';
				echo json_encode($respArr);exit;
			} else if($custResp == '3'){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Phone already in use';
				echo json_encode($respArr);exit;
			}else{
				$respArr['status'] = 'error';
				$respArr['message'] = 'Something went wrong..Try Again Later';
				echo json_encode($respArr);exit;
			}
			echo json_encode($respArr); exit;
		}

		public function upload_profilePic(){
			header('Content-type: application/json');
			$postData = $_POST;

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$respArr['message'] = 'User Id is Required';
				echo json_encode($respArr);exit;
			}
			if(isset($_FILES) && !empty($_FILES) && isset($_FILES['user_profilepic']) && !empty($_FILES['user_profilepic'])){
				$config = set_upload_service("assets/uploads/customer");
				$this->load->library('upload');
				$config['file_name'] = time()."_".$_FILES['user_profilepic']['name'];
				$this->upload->initialize($config);
				if($this->upload->do_upload('user_profilepic')){
					$upload_data = $this->upload->data();
					$postData['profile_photo'] = $config['upload_path']."/".$upload_data['file_name'];
				}
			}
			$custResp = $this->Webservice_model->upload_profilePic($postData);
			if($custResp == '1'){
				$respArr = array('status'=>'success','message'=>'Profile Pic Updated Successfully');
			}
			echo json_encode($respArr); exit;
		}

		public function user_forgetpsw(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$respArr['message'] = 'User Id is Required';
				echo json_encode($respArr);exit;
			}else if(!isset($postData['user_phonenumber']) || empty($postData['user_phonenumber'])){
				$respArr['message'] = 'Phone Number is Required';
				echo json_encode($respArr);exit;
			}else if(!isset($postData['user_countrycode']) || empty($postData['user_countrycode'])){
				$respArr['message'] = 'Country Code is Required';
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->user_forgetpsw($postData);
			echo json_encode($respArr); exit;
		}

		public function user_otp(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$respArr['message'] = 'User Id is Required';
				echo json_encode($respArr);exit;
			}else if(!isset($postData['otp']) || empty($postData['otp'])){
				$respArr['message'] = 'OTP is Required';
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->user_otp($postData);
			echo json_encode($respArr); exit;
		}

		public function user_otp_resend(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$respArr['message'] = 'User Id is Required';
				echo json_encode($respArr);exit;
			}else if(!isset($postData['user_phonenumber']) || empty($postData['user_phonenumber'])){
				$respArr['message'] = 'Phone Number is Required';
				echo json_encode($respArr);exit;
			}else if(!isset($postData['user_countrycode']) || empty($postData['user_countrycode'])){
				$respArr['message'] = 'Country Code is Required';
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->user_forgetpsw($postData);
			echo json_encode($respArr); exit;
		}

		public function get_carVin(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'0','message'=>'Something went wrong.');
			if(empty($postData) || !isset($postData['user_id']) || empty($postData['user_id']) || !isset($postData['car_vinNumber']) || empty($postData['car_vinNumber'])){
				echo json_encode($respArr);exit;
			}
			$settings = getSettings();
			$url = "https://specifications.vinaudit.com/v3/specifications?format=json&key=".urlencode($settings['vin_audit_api'])."&vin=".urlencode($postData['car_vinNumber']);
			if(!empty($url)){
				$vehData=file_get_contents($url);
				if(empty($vehData) || empty($vehData = json_decode($vehData,true))){
					echo json_encode($return_arr);exit;
				}
				if(!isset($vehData['success']) || empty($vehData['success']) || $vehData['success'] == false || ((!isset($vehData['attributes']) || empty($vehData['attributes'])) && 
                	(!isset($vehData['selections']) || empty($vehData['selections'])))){
					$return_arr['status'] = 2;
					$return_arr['message'] = 'No Data Found.';
					echo json_encode($return_arr);exit;
				}
				$vehDetails['vehicle'] = $vehData['attributes']['year'].' '.
                                         	 $vehData['attributes']['make'].' '.
                                         	 $vehData['attributes']['model'];
                $vehDetails['year']    = $vehData['attributes']['year'];
                $vehDetails['make']    = $vehData['attributes']['make'];
                $vehDetails['model']   = $vehData['attributes']['model'];
				$vehDetails['mileage'] = (isset($postData['mileage']) && !empty($postData['mileage']))?$postData['mileage']:'';
				$car_id = $this->Webservice_model->addVehicle($vehDetails,$postData);
				if($car_id){
					$return_arr['status'] = '1';
					$return_arr['data'] = $vehDetails;
				}
			}
			echo json_encode($return_arr);exit;
		}

		public function people_most_purchased(){
			header('Content-type: application/json');
			$respArr = $this->Webservice_model->people_most_purchased();
			echo json_encode($respArr); exit;
		}

		public function products_for_user(){
			header('Content-type: application/json');
			$postData = $_GET;
			$respArr = $this->Webservice_model->products_for_user($postData['user_id']);
			echo json_encode($respArr); exit;
		}

		public function user_bookedService(){
			header('Content-type: application/json');
			$postData = $_GET;
			$respArr = $this->Webservice_model->user_bookedService($postData['user_id']);
			echo json_encode($respArr); exit;
		}

		public function add_UserCar(){
			$postData = $_POST;
			$respArr = array('status'=>'error','message'=>'Something went wrong1.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			$err = 0;
			$msg = '';
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$err = 1;
				$msg = 'User Id is Required';
			} 
			else if(!isset($postData['car_VinNumber']) || empty($postData['car_VinNumber'])){
				$err = 1;
				$msg = 'Provide a car Vin Number';
			} 
			else if(!isset($postData['car_brand']) || empty($postData['car_brand'])){
				$err = 1;
				$msg = 'Provide a Car Brand';
			}
			else if(!isset($postData['car_model']) || empty($postData['car_model'])){
				$err = 1;
				$msg = 'Provide a Car Model';
			}
			else if(!isset($postData['car_year']) || empty($postData['car_year'])){
				$err = 1;
				$msg = 'Provide s Car Year';
			}
			else if(!isset($postData['car_milege']) || empty($postData['car_milege'])){
				$err = 1;
				$msg = 'Provide Car Mileage';
			}
			if($err == 1){
				$respArr['message'] = $msg;
				echo json_encode($respArr);exit;
			}
			$postData['vehicle_image'] = '';
			if(isset($_FILES)&& !empty($_FILES)&& isset($_FILES['car_image'])&& !empty($_FILES['car_image'])){
				$config = set_upload_service("assets/uploads/vehicle");
				$this->load->library('upload');
				$config['file_name'] = time()."_".$_FILES['car_image']['name'];
				$this->upload->initialize($config);
				if($this->upload->do_upload('car_image')){
					$upload_data = $this->upload->data();
					$postData['vehicle_image'] = $config['upload_path']."/".$upload_data['file_name'];
				}
			}
			$respArr = $this->Webservice_model->add_UserCar($postData);
			echo json_encode($respArr);exit;
		}

		public function get_mainservicelist(){
			header('Content-type: application/json');
			$respArr = $this->Webservice_model->get_MainServices();
			echo json_encode($respArr); exit;
		}

		public function get_subservicelist(){
			header('Content-type: application/json');
			$postData = $_GET;
			if(!isset($postData['service_id']) || empty($postData['service_id'])){
				$respArr['status'] = "error";
				$respArr['message'] = 'Provide Car Mileage';
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->get_subServices($postData['service_id']);
			echo json_encode($respArr); exit;
		}

		public function get_carDetails(){
			header('Content-type: application/json');
			$postData = $_GET;
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$respArr['status'] = "error";
				$respArr['message'] = 'User Id is Required';
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->get_carDetails($postData['user_id']);
			echo json_encode($respArr); exit;
		}

		public function get_userProfile(){
			header('Content-type: application/json');
			$postData = $_GET;
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$respArr['status'] = "error";
				$respArr['message'] = 'User Id is Required';
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->get_userProfile($postData['user_id']);
			echo json_encode($respArr); exit;
		}

		public function road_assistanceList(){
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
				$respArr['message'] = 'User Latitude is Required';
				echo json_encode($respArr); exit;
			} 
			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
				$respArr['message'] = 'User Longitude is Required';
				echo json_encode($respArr); exit;
			} 
			$respArr = $this->Webservice_model->road_assistanceList($postData);
			echo json_encode($respArr); exit;
		}

		public function road_assistanceDetail(){
			$postData = $_GET;

			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			if(!isset($postData['roadassistant_id']) || empty($postData['roadassistant_id'])){
				$respArr['message'] = 'Road Assistant Id is Required';
				echo json_encode($respArr); exit;
			}
			$respArr = $this->Webservice_model->road_assistanceDetail($postData['roadassistant_id']);
			echo json_encode($respArr); exit;
		}

		public function book_roadassistance(){
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			$err = 0;
			$msg = '';
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$err = 1;
				$msg = 'User Id is Required';
			} 
			else if(!isset($postData['user_lat']) || empty($postData['user_lat'])){
				$err = 1;
				$msg = 'Provide User Longitude';
			} 
			else if(!isset($postData['user_lng']) || empty($postData['user_lng'])){
				$err = 1;
				$msg = 'Provide User Latitude';
			}
			else if(!isset($postData['roadassistance_id']) || empty($postData['roadassistance_id'])){
				$err = 1;
				$msg = 'Provide Road Assistant Id';
			}
			else if(!isset($postData['user_carId']) || empty($postData['user_carId'])){
				$err = 1;
				$msg = 'Provide User Car Id';
			}
			else if(!isset($postData['date']) || empty($postData['date'])){
				$err = 1;
				$msg = 'Provide Booking Date';
			}
			if($err == 1){
				$respArr['message'] = $msg;
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->book_roadassistance($postData);
			echo json_encode($respArr); exit;
		}

		public function road_assistanceDetail_history(){
			$postData = $_GET;
			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(!isset($postData['roadassistant_id']) || empty($postData['roadassistant_id'])){
				$respArr['message'] = 'Road Assistant Id is Required';
				echo json_encode($respArr); exit;
			}
			$respArr = $this->Webservice_model->road_assistanceDetail_history($postData['roadassistant_id']);
			echo json_encode($respArr); exit;
		}

		public function status_roadassistancerequest(){
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			if(!isset($postData['roadservice_requestId']) || empty($postData['roadservice_requestId'])){
				$respArr['message'] = 'Road Assistant Id is Required';
				echo json_encode($respArr); exit;
			}else if(!isset($postData['status'])){
				$respArr['message'] = 'Status is Required';
				echo json_encode($respArr); exit;
			}
			$respArr = $this->Webservice_model->status_roadassistancerequest($postData);
			echo json_encode($respArr); exit;
		}

		public function road_assistancerequestlist(){
			$postData = $_GET;
			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$respArr['message'] = 'User Id is Required';
				echo json_encode($respArr); exit;
			}
			$respArr = $this->Webservice_model->road_assistancerequestlist($postData['user_id']);
			echo json_encode($respArr); exit;
		}

		public function write_review(){
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			$err = 0;
			$msg = '';
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$err = 1;
				$msg = 'User Id is Required';
			}
			else if(!isset($postData['review_rate']) || empty($postData['review_rate'])){
				$err = 1;
				$msg = 'Rating is Required';
			}
			else if(!isset($postData['reviewing_type']) || empty($postData['reviewing_type'])){
				$err = 1;
				$msg = 'Reviewing Type is Required';
			}
			else if(!isset($postData['review_caption']) || empty($postData['review_caption'])){
				$err = 1;
				$msg = 'Review Caption is Required';
			}
			else if(!isset($postData['review_description']) || empty($postData['review_description'])){
				$err = 1;
				$msg = 'Review Description is Required';
			}
			else if(!isset($postData['booking_id']) || empty($postData['booking_id'])){
				$err = 1;
				$msg = 'Booking Id is Required';
			}
			if($err == 1){
				$respArr['message'] = $msg;
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->write_review($postData);
			echo json_encode($respArr); exit;
		}

		public function user_requestService(){
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$respArr = array('status'=>'error','message'=>'Something went wrong.');
			if(empty($postData)){
				echo json_encode($respArr);exit;
			}
			$err = 0;
			$msg = '';
			if(!isset($postData['user_id']) || empty($postData['user_id'])){
				$err = 1;
				$msg = 'User Id is Required';
			}
			else if(!isset($postData['subservice_id']) || empty($postData['subservice_id'])){
				$err = 1;
				$msg = 'Sub Service Id is Required';
			}
			else if(!isset($postData['booking_date']) || empty($postData['booking_date'])){
				$err = 1;
				$msg = 'Booking Date is Required';
			}
			else if(!isset($postData['car_id']) || empty($postData['car_id'])){
				$err = 1;
				$msg = 'Car Id is Required';
			}
			if($err == 1){
				$respArr['message'] = $msg;
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_model->user_requestService($postData);
			echo json_encode($respArr); exit;
		}

	}
?>
