<?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_region']) || empty($postData['user_region'])){
				$err = 1;
				$msg = 'Provide valid Region';
			}
			else if(!isset($postData['user_city']) || empty($postData['user_city'])){
				$err = 1;
				$msg = 'Provide valid City';
			}
			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';
			}

			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 user_forgetpsw(){
			
		}

	}
?>
