<?php
defined('BASEPATH') OR exit('No direct script access allowed');
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'); 
    }

  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 {
  var $auth_token;

  public function __construct() {
    parent::__construct();
    $this->load->model('Webservice_model');   
    $this->load->model('Validation_app_model');
    $method = $this->router->fetch_method();  

    $data = (array) json_decode(file_get_contents('php://input'));
    if($method == 'profile') {
      $data = $_POST;
    }
    if (isset(apache_request_headers()['Auth'])) {
            $this->auth_token = apache_request_headers()['Auth'];
            $data['auth_token'] = $this->auth_token;
        }
    $res = $this->Validation_app_model->validation_check($method, $data);
    if($res['state'] == 1) {
      $this->errorResponse($res['response']['code'], $res['response']['message']);
      die;
    }     
  }

  public function login() {
    $data = (array) json_decode(file_get_contents('php://input'));
    $res = $this->Webservice_model->login($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }


  public function check_email_availability() {
    $data = (array) json_decode(file_get_contents('php://input'));
    $res = $this->Webservice_model->availability($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }

  public function registration(){
    $data = (array) json_decode(file_get_contents('php://input'));
    $res = $this->Webservice_model->register($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }

  public function get_events_list() {
    $data = $_GET;
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->discover($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }

  public function events_details($event_id = null) {
    $data['event_id'] = $_GET['event_id'];
    $data['auth_token'] = $this->auth_token;
    if($data['event_id'] == null) {
      $this->errorResponse("ER16","Event id is null or empty");
      die;
    }
    
    
    $res = $this->Webservice_model->event($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }

  public function response($data) {
    $result =  array(
      'status' => 'success',
      'data' =>$data
    );
    print json_encode($result);
  }

  public function favResponse($data) {
    $result =  array(
      'status' => 'success',
      'data' => array(
      'favorite_events' =>$data)
    );
    print json_encode($result);
  }


    public function successResponse($data) {
    $result =  array(
      'status' => 'success',
    );
    print json_encode($result);
  }

  public function errorResponse($errorCode, $errorDesc) {
    $result =  array(
      'status' => 'error',
      'error'=> $errorCode,
      'message'=> $errorDesc
    );
    print json_encode($result);
  }


    public function get_category_list($query = null) {
    $data['query'] = $_GET['query'];
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->get_category_list($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }
  
    public function add_favorites(){
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->add_favorites($data);
    if($res['status']!=0){
    $this->successResponse($res);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }


    public function get_cities_list() {
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->get_cities_list($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }

  }


    public function update_city(){
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->update_city($data);
    if($res['status']!=0){
    $this->successResponse($res);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }


    public function booking_summary($booking_id = null) {
      $data['booking_id'] = $_GET['booking_id'];
      if($data['booking_id'] == null) {
        $this->errorResponse("ER34","Booking id is null or empty");
        die;
      }
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->booking_summary($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }

  }

    public function payment(){
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->payment($data);
    if($res['status']!=0){
    $this->successResponse($res);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }


    public function event_rating(){
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->event_rating($data);
    if($res['status']!=0){
    $this->successResponse($res);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  } 


    public function update_notification_email_status(){
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->update_notification_email_status($data);
    if($res['status']!=0){
    $this->successResponse($res);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }


    public function profile_details() {
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->profile_details($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }

  public function profile_edit() {
    $data = $_POST;
    if(isset($_FILES['profile_photo'])) {
      $data['file'] = $_FILES['profile_photo'];
    } 
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->update_profile($data);
    if($res['status']!=0){
      $this->successResponse($res);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }



  public function booking() {
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->booking($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }
  public function cancel_booking() {    
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->cancel($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }



  public function get_favorites_list() {
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->favouritelist($data);
    if($res['status']!=0){
      $this->favResponse($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }

  public function get_booking_list() {    
    $data = (array) json_decode(file_get_contents('php://input'));
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->bookedlist($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }


    public function settings() {
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->get_settings($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }

   public function forgot_password() {
    $data = (array) json_decode(file_get_contents('php://input'));
    $res = $this->Webservice_model->forgot_password($data);
    if($res['status']!=0){
      $this->successResponse($res);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }


    public function get_last_booking() {
      
      $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->get_last_booking($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }

  }

  public function filters() {
    $data['auth_token'] = $this->auth_token;
    $res = $this->Webservice_model->filters($data);
    if($res['status']!=0){
      $this->response($res['data']);
    }
    else{
      $this->errorResponse($res['code'],$res['message']);
    }
  }
}
?>