<?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 Webservices_mechanic extends CI_Controller {
		
		public function __construct() {
			parent::__construct();
			$this->load->model('Webservice_mechanic_model');
			$this->load->library('form_validation');
			
			define("PAYSTACK_SECRET_KEY", "sk_live_ac1f5350a42852fd3439fdac8ba1d434f1c42360");
			date_default_timezone_set("Africa/Lagos");
		}
		
		// customer_login
		public function sign_in_mechanic(){
			header('Content-type: application/json');
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$respArr = array('status'=>'error','message'=>'Required Fields are empty.');
			if(!isset($postData['user_name']) || empty($postData['user_name']) || 
			!isset($postData['password']) || empty($postData['password'] = md5($postData['password'])) || !isset($postData['device_id']) || empty($postData['device_id'])){
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_mechanic_model->checkMechanicLogin($postData);
			echo json_encode($respArr); exit;
		}
		
	//Get Booked Services	
		public function get_todays_services(){
			header('Content-type: application/json');
			$headers = apache_request_headers();	
			if(!isset($headers['auth']) || empty($headers['auth'])){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Authtoken is Required';
				echo json_encode($respArr);exit;
			}
			$authRes = $this->Webservice_mechanic_model->get_mechanic_authtoken($headers['auth']); 
			if($authRes['status'] == 'error'){
				echo json_encode($authRes);exit;
			}
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$per_page = 10;
			$page = (isset($postData['page']) && $postData['page'] >= 1)?(int)$postData['page']:1; 
		    $start = ($page - 1) * $per_page; 
		    $postData['mechanic_id'] = $authRes['data']['mechanic_id'];
			$bookDataCount = $this->Webservice_mechanic_model->getBookedService($postData,0,0,1);
			$bookDataResult = $this->Webservice_mechanic_model->getBookedService($postData,$start,$per_page,1);
			$total = 0;
			if($bookDataCount['status'] == 'success'){
				$total = count($bookDataCount['data']);	
			}
			if($total >= $per_page){
				$totalPages = (int)($total % $per_page ==0 ? $total / $per_page :($total / $per_page)+1);
			}
			else{
				$totalPages = 1;
			}
			if($bookDataResult['status'] == 'success'){
				$respArr = array(
					'status' => 'success',
					'message'=>'success',
					'data' => $bookDataResult['data'],
					'meta' => array(
						'total_pages' => $totalPages,
						'total' => $total,
						'current_page' => ($page == 0)?1:$page,
						'per_page' => $per_page
					)
				);
			}else{
				$respArr = array(
					'status' => 'error',
					'message'=>'No data',
					'data' => [],
					'meta' => array(
						'total_pages' => $totalPages,
						'total' => $total,
						'current_page' => ($page == 0)?1:$page,
						'per_page' => $per_page
					)
				);
			}
			echo json_encode($respArr);exit;
		}

		public function services_details(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			if(!isset($headers['auth']) || empty($headers['auth'])){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Authtoken is Required';
				echo json_encode($respArr);exit;
			}
			$authRes = $this->Webservice_mechanic_model->get_mechanic_authtoken($headers['auth']); 
			if($authRes['status'] == 'error'){
				echo json_encode($authRes);exit;
			}
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$postData['mechanic_id'] = $authRes['data']['mechanic_id'];
			if(!isset($postData['service_id']) || empty($postData['service_id'])){
				$respArr['message'] = "Service Id is Required";
				return $respArr;
			}
			$respArr = $this->Webservice_mechanic_model->getBookedService($postData);
			echo json_encode($respArr);exit;
		}

		public function get_service_history(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			if(!isset($headers['auth']) || empty($headers['auth'])){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Authtoken is Required';
				echo json_encode($respArr);exit;
			}
			$authRes = $this->Webservice_mechanic_model->get_mechanic_authtoken($headers['auth']); 
			if($authRes['status'] == 'error'){
				echo json_encode($authRes);exit;
			}
			$per_page = 10;
			$page = (isset($postData['page']) && $postData['page'] >= 1)?(int)$postData['page']:1; 
		    $start = ($page - 1) * $per_page; 
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
		    $postData['mechanic_id'] = $authRes['data']['mechanic_id'];
			$bookDataCount = $this->Webservice_mechanic_model->getBookedService($postData,0,0,0,3);
			$bookDataResult=$this->Webservice_mechanic_model->getBookedService($postData,$start,$per_page,0,3);
			$total = 0;
			if($bookDataCount['status'] == 'success'){
				$total = count($bookDataCount['data']);	
			}
			if($total >= $per_page){
				$totalPages = (int)($total % $per_page ==0 ? $total / $per_page :($total / $per_page)+1);
			}
			else{
				$totalPages = 1;
			}
			if($bookDataResult['status'] == 'success'){
				$respArr = array(
					'status' => 'success',
					'message'=>'success',
					'data' => $bookDataResult['data'],
					'meta' => array(
						'total_pages' => $totalPages,
						'total' => $total,
						'current_page' => ($page == 0)?1:$page,
						'per_page' => $per_page
					)
				);
			}else{
				$respArr = array(
					'status' => 'error',
					'message'=>'No data',
					'data' => [],
					'meta' => array(
						'total_pages' => $totalPages,
						'total' => $total,
						'current_page' => ($page == 0)?1:$page,
						'per_page' => $per_page
					)
				);
			}
			echo json_encode($respArr);exit;
		}

		public function logout(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			if(!isset($headers['auth']) || empty($headers['auth'])){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Authtoken is Required';
				echo json_encode($respArr);exit;
			}
			$authRes = $this->Webservice_mechanic_model->get_mechanic_authtoken($headers['auth']); 
			if($authRes['status'] == 'error'){
				echo json_encode($authRes);exit;
			}
			$respArr=$this->Webservice_mechanic_model->mechanic_logout($authRes['data']['mechanic_id']);
			echo json_encode($respArr);exit;
		}

		public function forgot_password(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			if(!isset($postData['user_name']) || empty($postData['user_name'])){
				$respArr['message'] = "Username is Required";
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_mechanic_model->forgot_password($postData['user_name']);
			echo json_encode($respArr);exit;
		}

		public function change_password(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$respArr = array("status"=>"error");
			if(!isset($postData['user_name']) || empty($postData['user_name'])){
				$respArr['message'] = "Username is Required";
				echo json_encode($respArr);exit;
			}else if(!isset($postData['password']) || empty($postData['password'])){
				$respArr['message'] = "Password is Required";
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_mechanic_model->change_password($postData);
			echo json_encode($respArr);exit;
		}

		public function start_service(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			$respArr = array("status"=>"error");
			if(!isset($postData['service_id']) || empty($postData['service_id'])){
				$respArr['message'] = "Booking Id is Required";
				echo json_encode($respArr);exit;
			}
			if(!isset($postData['emergency'])){
				$respArr['message'] = "Emergency Field is Required";
				echo json_encode($respArr);exit;
			}
			$respArr = $this->Webservice_mechanic_model->start_service($postData);
			echo json_encode($respArr);exit;			
		}

		public function stop_service(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			$post = file_get_contents("php://input");
			$postData = json_decode($post, true);
			if(!isset($postData['service_id']) || empty($postData['service_id'])){
				$respArr['message'] = "Booking Id is Required";
				return $respArr;
			}else if(!isset($postData['emergency']) || empty($postData['emergency'])){
				$respArr['message'] = "Emergency Field is Required";
				return $respArr;
			}
			$respArr = $this->Webservice_mechanic_model->start_service($postData,1);
			echo json_encode($respArr);exit;			
		}

		public function update_profile_status(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			if(!isset($headers['auth']) || empty($headers['auth'])){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Authtoken is Required';
				echo json_encode($respArr);exit;
			}
			$authRes = $this->Webservice_mechanic_model->get_mechanic_authtoken($headers['auth']); 
			if($authRes['status'] == 'error'){
				echo json_encode($respArr);exit;
			}

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

			if(!isset($postData['status']) ){
				$respArr['message'] = "Online status is Required";
				echo json_encode($respArr) ;exit;
			}

			$postData['mechanic_id'] = $authRes['data']['mechanic_id'];
			$respArr = $this->Webservice_mechanic_model->update_profile_status($postData);
			echo json_encode($respArr);exit;		
		}

		public function add_location(){
			header('Content-type: application/json');
			$headers = apache_request_headers();
			if(!isset($headers['auth']) || empty($headers['auth'])){
				$respArr['status'] = 'error';
				$respArr['message'] = 'Authtoken is Required';
				echo json_encode($respArr);exit;
			}
			$authRes = $this->Webservice_mechanic_model->get_mechanic_authtoken($headers['auth']); 
			if($authRes['status'] == 'error'){
				echo json_encode($respArr);exit;
			}

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

			if(!isset($postData['lat']) && empty($postData['lat'])){
				$respArr['message'] = "Latitude is Required";
				echo json_encode($respArr) ;exit;
			}else if(!isset($postData['lng']) && empty($postData['lng'])){
				$respArr['message'] = "Longitude is Required";
				echo json_encode($respArr) ;exit;
			}

			$postData['mechanic_id'] = $authRes['data']['mechanic_id'];
			$respArr = $this->Webservice_mechanic_model->add_location($postData);
			echo json_encode($respArr);exit;
		}
	}
?>
