<?php
        
        defined('BASEPATH')OR exit('No direct script access allowed');

        // Allow from any origin

        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

        }

        // Access-Control headers are received during OPTIONS requests

        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_driver extends CI_Controller {

        public function __construct() {

              parent::__construct();

              $this->load->model('Webservices_driver_model');

              $class = $this->router->fetch_class();

              $method = $this->router->fetch_method();

              if ($this->input->server('REQUEST_METHOD') == 'GET')

              $postdata = json_encode($_GET);

              else if ($this->input->server('REQUEST_METHOD') == 'POST')

              $postdata = file_get_contents("php://input");

              $auth = '';

              if (isset(apache_request_headers()['Auth'])) {

              $auth = apache_request_headers()['Auth'];

              }

              $this->last_id = set_log($class, $method, $postdata, $auth);

        }


        // *********************** NEMT DRIVER DOCUMENTATION ********************

        // *********************** DRIVER LOGIN ********************

        public function do_login() {

            $postdata = file_get_contents("php://input");

            $request = json_decode($postdata, true);
            
            $result = $this->Webservices_driver_model->login($request);

            header('Content-type: application/json');

            if ($result) {

              print json_encode(array('status' => 'success', 'data' => array('auth_token' => $result['auth_token'], 'user' => array('user_id' => $result['user_id'], 'name' => $result['name'], 'phone' => $result['phone'], 'first_time_user' => $result['first_time_user']))));

            } else {
                  print json_encode(array('status' => 'error', 'message' => 'Unknown Credential! Try Again', 'error' => '202'));
            }
        }

        // *********************** DOCUMENT UPLOAD ********************

        public function document_upload() {

           header('Content-type: application/json');

           if (isset(apache_request_headers()['Auth'])) {

                 $auth = apache_request_headers()['Auth'];
                 
                 if (is_uploaded_file($_FILES['image']['tmp_name'])) {

                       $uploads_dir = 'assets/uploads/profile_pic/';

                       $tmp_name = $_FILES['image']['tmp_name'];

                       $pic_name = $_FILES['image']['name'];

                       $pic_name = str_replace(' ', '_', mt_rand().$pic_name);

                       move_uploaded_file($tmp_name, $uploads_dir.$pic_name);

                       $request = $_POST;

                       $request['image'] = $uploads_dir.$pic_name;

                       $request['auth'] = $auth;

                       $result = $this->Webservices_driver_model->doc_upload($request);

                       header('Content-type: application/json');

                       if ($result) {

                             print json_encode(array('status' => 'success'));

                       } else {

                             print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '207'));

                       }

                 } else {

                       print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '207'));

                 }

           } else {

                 print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '207'));

           }

      }

      // *********************** GET DRIVER STATUS ********************

      public function get_driver_status() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->driver_status($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success', 'data' => $result));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

      }

     
      // *********************** UPDATE DRIVER STATUS ********************

      public function update_driver_status() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   if (isset($request['driver_status'])) {

                         $result = $this->Webservices_driver_model->status($request);

                         header('Content-type: application/json');

                         if ($result) {

                               print json_encode(array('status' => 'success'));

                               $this->Webservices_driver_model->driver_onstatus($request);

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

      }


      // *********************** ARRIVED AT CUSTOMER ********************

      public function arrived_at_customer() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   if (isset($request['trip_id'])) {

                         $result = $this->Webservices_driver_model->arrived_at_customer($request);

                         header('Content-type: application/json');

                         if ($result) {

                               print json_encode(array('status' => 'success'));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

      }

      // *********************** GET DRIVER PROFILE ********************

      public function get_profile() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request = $_GET;

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->profile($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success', 'data' =>$result));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '205', 'message' => 'Something Went wrong'));

                   }

            } else {

                   print json_encode(array('status' => 'error', 'code' => '205', 'message' => 'Something Went wrong'));

             }

       }

      // *********************** UPDATE DRIVER PASSWORD ********************

      public function update_password() {

            header('Content-type: application/json');
            if (isset(apache_request_headers()['Auth'])) {

                  $auth = apache_request_headers()['Auth'];

                  $postdata = file_get_contents("php://input");

                  $request = json_decode($postdata, true);

                  $request['auth'] = $auth;

                  $query = $this->db->where('unique_id', $auth)->get('driver_auth_table');

                  if ($query->num_rows() > 0) {

                      $rs = $query->row();

                      $driver_id = $rs->driver_id;

                      $this->db->where('driver_id=', $driver_id);

                      $query1 = $this->db->get('drivers');

                      $result = $query1->row();
                     
                              if ($result->password == md5($request['current_password'])) {

                                    $result = $this->Webservices_driver_model->update_password($request);

                                    if ($result) {

                                          print json_encode(array('status' => 'success'));

                                    } else {

                               print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '508'));
                              
                              }
                              } else {

                                    print json_encode(array('status' => 'error', 'message' => 'Invalid Old Password', 'error' => '508'));

                              }

                  } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '508'));

                  }

            }
      }

      // *********************** UPDATE DRIVER USER NAME ********************

      public function update_user_name() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   if(!isset($_POST) || empty($_POST) || !isset($_POST['name'])||empty($_POST['name'])){
                        print json_encode(array('status'=>'error',
                                                'message'=>'Something Went wrong','error'=>'208'));
                   }

                   $request['last_name'] = '';

                   $request['first_name'] = $_POST['name'];

                   if(isset($_FILES)&&!empty($_FILES)&&is_uploaded_file($_FILES['photo']['tmp_name'])){

                         $uploads_dir = './assets/uploads/profile_pic/';

                         $tmp_name = $_FILES['photo']['tmp_name'];

                         $pic_name = $_FILES['photo']['name'];

                         $pic_name = str_replace(' ', '_', mt_rand().$pic_name);

                         move_uploaded_file($tmp_name, $uploads_dir.$pic_name);

                         $request['profile_image'] = 'assets/uploads/profile_pic/'.$pic_name;
                   }

                   $result = $this->Webservices_driver_model->update_user_name($auth,$request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '203'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '204'));

             }

      }

      // *********************** UPDATE VEHICLE NUMBER ********************


      public function update_vehicle_number() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->update_vehicle_number($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '203'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '204'));

             }

       }

      // *********************** UPDATE DRIVER LOCATION ********************

      public function update_driver_location() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->driver_location($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

      }

      // *********************** TRIP ACCEPT ********************


      public function trip_accept() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   $data = $this->db->where('transport_id', $request['request_id'])->get('transport_details')->row();
                  
                   if($data->status==4 || $data->status==5 || $data->status==6 || $data->status==8) {
                         print json_encode(array('status' => 'error', 'message' => 'Trip Assigned '));
                   }
                   elseif($data->status == 2 || $data->status == 9) {
                         print json_encode(array('status' => 'error', 'message' => 'Trip Cancelled '));
                   }
                   else {

                         $result = $this->Webservices_driver_model->accept($request);

                         if (!empty($result)) {

                               print json_encode(array('status' => 'success', 'data' => $result));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '207', 'message' => 'Something Went wrong'));

                         }
                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

      }

      // *********************** PAYMENT STATUS ********************

      public function payment_status() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request = $_GET;

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->payment_status($request);

                   if ($result) {


                         print json_encode(array('status' => 'success', 'data' => $result));

                   } else {

                         print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '505'));

                   }
             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '534'));

             }

      }

      // *********************** APP STATUS ********************

      public function app_status() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->statusof_app($request);
                  
                   header('Content-type: application/json');

                   if ($result) {
                      
                         $result->app_status = '1';

                         $drvr_status = $this->Webservices_driver_model->status_driver($result->trip_id);

                         $result->driver_status = $drvr_status;

                         print json_encode(array('status' => 'success', 'data' => $result));

                   } else {

                         print json_encode(array('status' => 'success', 'data' => array('app_status' => '0')));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

      }

      // *********************** EMERGENCY RIDE HISTORY ********************

      public function emergency_ride_history() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request['auth'] = $auth;

                   $page = isset($_GET['page']) ? $_GET['page'] : 1;

                   if ($page == 0) {

                         $page = 1;

                   }

                   $result = $this->Webservices_driver_model->history_trips_emergency($request);

                   if ($result) {

                         $tmp = $this->db->query($result)->result();

                         $total = $this->db->query($result)->num_rows();

                         $per_page = 5;

                         $total_pages = ceil($total / $per_page);

                         $current_page = $page;

                         $query = $this->db->where('unique_id', $auth)->get('driver_auth_table');

                         $rs = $query->row();

                         $driv_id = $rs->driver_id;

                         if ($total > 0) {

                               $limit = ($per_page * ($current_page - 1));
                          
                               $limit_sql = " ORDER BY t.transport_id DESC LIMIT $limit,$per_page";

                               $result = $this->db->query($result.$limit_sql)->result();


                               $query = $this->db->where('unique_id', $auth)->get('driver_auth_table');

                               $rs = $query->row();

                               $driv_id = $rs->driver_id;

                               print json_encode(array('status' => 'success', 'data' => array('trips' => $result), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                         } else {

                               print json_encode(array('status' => 'success', 'data' => array('trips' => []), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '503'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '503'));

             }

       }

       // *********************** EMERGENCY RIDE HISTORY ********************

      public function scheduled_ride_history() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request['auth'] = $auth;

                   $page = isset($_GET['page']) ? $_GET['page'] : 1;

                   if ($page == 0) {

                         $page = 1;

                   }

                   $result = $this->Webservices_driver_model->history_trips_schedule($request);
                  
                   if (!empty($result)) {

                         $total = COUNT($result);

                         $per_page = 5;

                         $total_pages = ceil($total / $per_page);

                         $current_page = $page;

                         $query = $this->db->where('unique_id', $auth)->get('driver_auth_table');

                         $rs = $query->row();

                         $driv_id = $rs->driver_id;

                         if ($total > 0) {

                               print json_encode(array('status' => 'success', 'data' => array('trips' => $result), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                         } else {

                               print json_encode(array('status' => 'success', 'data' => array('trips' => []), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                         }

                   } else {

                         print json_encode(array('status' => 'success','data' => []));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '503'));

             }

       }

             


              

       



      


             







       


        

      // *********************** END NEMT DRIVER DOCUMENTATION ********************



       public function registration() {

             header('Content-type: application/json');

             $postdata = file_get_contents("php://input");

             $request = json_decode($postdata, true);

             $result = $this->Webservices_driver_model->driver_reg($request);

             header('Content-type: application/json');

             $otp = rand(1111, 9999); //print_r($otp);


             if ($result['status'] == 'success') {

                   $result = array('status' => "success", 'data' => array('auth_token' => $result['auth_token'], 'user' => array('user_id' => $result['user_id'], 'name' => $result['name'], 'phone' => $result['phone'], 'email' => $result['email'], 'city' => $result['city'], 'profile_photo' => $result['image'], 'is_phone_verified' => true)));

             } else {

                   //$result = array('status'=>"error",'message' =>'Mobile Number already Exists','error'=>'201');
                   $result = array('status' => 'error', 'message' => $result['message'], 'error' => '501');

             }

             $this->response($result);

       }

       function response($res) {

             header('Content-type: application/json');

             print json_encode($res);

       }


       public function get_booster_status() {

         header('Content-type: application/json');
         $request = $_GET;
         if (isset(apache_request_headers()['Auth'])) {
           $auth = apache_request_headers()['Auth'];
           $request['auth'] = $auth;
           $result = $this->Webservices_driver_model->get_booster_status($request);
           if ($result) {

             print json_encode(array('status' => 'success', 'data' => $result));
           } else {
             print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '505'));
           }
         } else {
           print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '508'));
         }

       }



        public function trip_cancel() {

           header('Content-type: application/json');
           if (isset(apache_request_headers()['Auth'])) {
                 $auth = apache_request_headers()['Auth'];
                 $postdata = file_get_contents("php://input");
                 $request = json_decode($postdata, true); ;
                 $request['auth'] = $auth;
                 if (isset($request['trip_id']) && isset($request['cancellation_reason'])) {

                       $result = $this->Webservices_driver_model->cancelReason($request);
                       if ($result) {
                             print json_encode(array('status' => 'success'));
                       } else {
                             print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '1602'));
                       }
                 } else {
                       print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '608'));

                 }
           } else {
                 print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '602'));
           }

     }


 public function booster_seat() {

           header('Content-type: application/json');
           if (isset(apache_request_headers()['Auth'])) {
                 $auth = apache_request_headers()['Auth'];
                 $postdata = file_get_contents("php://input");
                 $request = json_decode($postdata, true); ;
                 $request['auth'] = $auth;
                 if (isset($request['is_booster_seat'])) {
                  
                       $result = $this->Webservices_driver_model->booster_seat($request);
                       if ($result) {
                             print json_encode(array('status' => 'success'));
                       } else {
                             print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '1602'));
                       }
                 } else {
                       print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '608'));

                 }
           } else {
                 print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '602'));
           }

     }

      

       public function update_fcm_token() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   // print_r($request);


                   $result = $this->Webservices_driver_model->update_fcm($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '203'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '204'));

             }

       }

       public function mobile_number_availability() {

             header('Content-type: application/json');

             $postdata = file_get_contents("php://input");

             $request = json_decode($postdata, true);

             if (isset($request['phone'])) {

                   $result = $this->Webservices_driver_model->mobile_availability($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success', 'data' => array('phone' => $request['phone'], 'is_available' => filter_var($result['is_available'], FILTER_VALIDATE_BOOLEAN))));

                   } else {

                         print json_encode(array('status' => 'success', 'data' => array('phone' => $request['phone'], 'is_available' => filter_var(true, FILTER_VALIDATE_BOOLEAN))));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Mobile Number is missing. Please try again', 'error' => 'phone is missing'));

             }

       }

      




       public function update_profile() {

             header('Content-type: application/json');
             if (isset(apache_request_headers()['Auth'])) {
                   $auth = apache_request_headers()['Auth'];
            $query = $this->db->where('unique_id', $auth)->get('driver_auth_table');
                if ($query->num_rows() > 0) {
                    $rs = $query->row();
                    $cust_id = $rs->cust_id;
            }
  
                   $postdata = $_POST['profile_update'];

                   //$request = $_POST;
           $postdata = file_get_contents("php://input");
             $request = json_decode($postdata, true);
                   $request['auth'] = $auth;

                   $email = $request['email'];
                   $phone = $request['phone'];
           $this->db->where('id!=', $cust_id);
                   $this->db->where('email', $email);
                   $this->db->or_where("phone", $phone);

                    $count = $this->db->get('driver')->result();
                   //$count = $this->db->count_all_results();
                   //print_r($count);
                  if (count($count) == 0) {


                         $result = $this->Webservices_driver_model->prof_update($request);
                         if ($result) {
                               //print_r($result);
                               print json_encode(array('status' => 'success', 'data' => array('id' => $result->id, 'name' => $result->driver_name, 'phone' => $result->phone, 'email' => $result->email, 'address' => $result->address, 'city' => $result->city, 'state' => $result->state, 'postal_code' => $result->post_code, 'profile_photo' => $result->image, 'is_phone_verified' => false)));
                         } else {
                               print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '508'));
                         }
                   } else {
                         print json_encode(array('status' => 'error', 'message' => 'Email Already Exists', 'error' => '508'));

                   }

             } else {
                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '508'));
             }

       }

      

       public function document_status() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {
                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->doc_status($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success', 'data' => array('documents' => $result)));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '204', 'message' => 'Something Went wrong'));

             }

       }

       

       public function update_driver_type() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->type_driver($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

       }

       public function profile_photo_upload() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = $_POST['image'];

                   if (is_uploaded_file($_FILES['image']['tmp_name'])) {

                         $uploads_dir = './assets/uploads/profile_pic/';

                         $tmp_name = $_FILES['image']['tmp_name'];

                         $pic_name = $_FILES['image']['name'];

                         $pic_name = str_replace(' ', '_', mt_rand().$pic_name);

                         move_uploaded_file($tmp_name, $uploads_dir.$pic_name);

                         $request = $_POST;

                         $request['image'] = $uploads_dir.$pic_name;

                         $request['auth'] = $auth;

                         $result = $this->Webservices_driver_model->photo_upload($request);

                         header('Content-type: application/json');

                         if ($result) {

                               print json_encode(array('status' => 'success'));

                         } else {

                               print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '207'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '207'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '207'));

             }

       }

       

    


       

    


       public function trip_start() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   if (isset($request['trip_id'])) {

                         $result = $this->Webservices_driver_model->start_trip($request);

                         header('Content-type: application/json');

                         if ($result) {

                               print json_encode(array('status' => 'success'));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

                   //'a534457488937db4b21d0a043eb6581a'

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

       }

       public function help() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = $_GET;

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->help_pages($request);

                   header('Content-type: application/json');

                   if (isset($request['id'])) {

                         if ($result) {

                               $query = $this->db->where('unique_id', $request['auth'])->get('driver_auth_table');

                               $rs = $query->row();

                               $driv_id = $rs->driver_id;

                               $is_help = $this->Webservices_driver_model->is_help_status($driv_id, $result['id']);

                               $rs->is_helpful = $is_help;

                               print json_encode(array('status' => 'success', 'data' => array('id' => $result['id'], 'title' => $result['title'], 'icon' => $result['icon'], 'content' => $result['content'], 'is_helpful' => $is_help)));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

                   //'a534457488937db4b21d0a043eb6581a'

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

       }

       public function help_page_list() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   //$request = $_GET;

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->help_list($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success', 'data' => array('help' => $result)));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

                   //'a534457488937db4b21d0a043eb6581a'

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

       }

       public function help_page_review() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->help_review($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

       }

       public function update_vehicle_details() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->update_vehicle($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '210', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '210', 'message' => 'Something Went wrong'));

             }

       }

       public function update_accesibility_settings() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->update_settings($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success'));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

       }

       public function fetch_accesibility_settings() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request = $_GET;

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->fetch_settings($request);

                   header('Content-type: application/json');

                   if ($result) {

                         print json_encode(array('status' => 'success', 'data' => array('is_deaf' => filter_var($result['is_deaf'], FILTER_VALIDATE_BOOLEAN), 'is_flash_required_for_requests' => filter_var($result['is_flash_required_for_requests'], FILTER_VALIDATE_BOOLEAN))));

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

       }

       

       public function request_details($request_id = null) {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request = $_GET;

                   $request['auth'] = $auth;

                   if (isset($request['request_id']) && !empty($request['request_id'])) {

                         $result = $this->Webservices_driver_model->req_details($request);

                         if (!empty($result)) {

                               print json_encode(array('status' => 'success', 'data' => $result));

                         } else {

                               print json_encode(array('status' => 'error', 'error' => '210', 'message' => 'Invalid Request ID'));

                         }

                   } else {
                    
                         print json_encode(array('status' => 'error', 'error' => '209', 'message' => 'Required fields are empty'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'error' => '211', 'message' => 'Auth Token Missing'));

             }

       }

       public function trip_summary($id = null) {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $request = $_GET;
                   

                   if (isset($request['trip_id'])) {

                         $result = $this->Webservices_driver_model->summary_trip($request);

                         header('Content-type: application/json');

                         if (count($result) > 0) {

                      
                               print json_encode(array('status' => 'success', 'data' => $result));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '202', 'message' => 'Something Went wrong'));

                         } 

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '203', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

       }

       

       public function confirm_car_arrival() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   if (isset($request['trip_id'])) {

                         $result = $this->Webservices_driver_model->confirm_arrival($request);

                         header('Content-type: application/json');

                         if ($result) {

                               print json_encode(array('status' => 'success'));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

       }

       public function confirm_cash_collection() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   if (isset($request['trip_id'])) {

                         $result = $this->Webservices_driver_model->confirm_cash($request);

                         header('Content-type: application/json');

                         if ($result) {

                               print json_encode(array('status' => 'success'));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

       }

       public function rider_feedback_issues() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request['auth'] = $auth;

                   $page = $_GET['page'] >= 1 ? $_GET['page'] : 1;

                   $result = $this->Webservices_driver_model->ride_feedback($request);

                   header('Content-type: application/json');

                   if ($result) {

                         $total = count((array)$result);

                         $per_page = 20;

                         $total_pages = ceil($total / $per_page);

                         $current_page = $page;

                         print json_encode(array('status' => 'success', 'data' => array('issues' => $result), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                   } else {

                         print json_encode(array('status' => 'success', 'data' => []));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '503'));

             }

       }

       public function rider_feedback_comments() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request['auth'] = $auth;

                   $page = $_GET['page'] >= 1 ? $_GET['page'] : 1;

                   $result = $this->Webservices_driver_model->feedback_comments($request);

                   header('Content-type: application/json');

                   if ($result) {

                         //print_r($result);

                         //$start_time =$result['trip_id'];

                         //print_r($start_time);


                         // $total = $this->db->query($result)->num_rows();


                         $total = count((array)$result);
                         //   print_r($total);

                         $per_page = 20;

                         $total_pages = ceil($total / $per_page);

                         $current_page = $page;

                         print json_encode(array('status' => 'success', 'data' => array('comments' => $result), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                   } else {

                         print json_encode(array('status' => 'success', 'data' => []));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '503'));

             }

       }

       public function rating_details() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   //$postdata = file_get_contents("php://input");

                   $request = $_GET;

                   $request['auth'] = $auth;

                   $result = $this->Webservices_driver_model->rating($request);

                   header('Content-type: application/json');

                   if ($result) {

                         //$str =(int)$result->average_rating;

                         //$str = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $str);

                         //print_r($str);

                         //echo trim($str, '"');

                         $query = $this->db->where('unique_id', $request['auth'])->get('driver_auth_table');

                         $rs = $query->row();

                         $driv_id = $rs->driver_id;

                         $average_rating = $this->Webservices_driver_model->avg_rating($driv_id);

                         //print_r($average_rating);

                         if ($average_rating == '') {

                               $avg = '0';

                         } else {

                               $avg = $average_rating;

                         }

                         $result->average_rating = $avg;

                         $tot_requests = $this->Webservices_driver_model->num_rides($driv_id);

                         //print_r($tot_requests);

                         $result->total_requests = $tot_requests;

                         $req_accepted = $this->Webservices_driver_model->num_requests($driv_id);

                         $result->requests_accepted = $req_accepted;

                         $tot_trips = $this->Webservices_driver_model->num_trips($driv_id);

                         $result->total_trips = $tot_trips;

                         $trips_cancelled = $this->Webservices_driver_model->num_cancelled($driv_id);

                         //print_r($trips_cancelled);

                         $result->trips_cancelled = $trips_cancelled;

                         //$test = json_encode(array('status' => 'success','data' =>$result));


                         //$con = str_replace("\"", "", $test);

                         //print $con;


                         print json_encode(array('status' => 'success', 'data' => $result), JSON_NUMERIC_CHECK);

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

                   //'a534457488937db4b21d0a043eb6581a'

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

       }

       public function trip_details($trip_id = null) {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $request = $_GET;

                   if (isset($request['trip_id'])) {

                         $result = $this->Webservices_driver_model->tripdetails($request);

                         header('Content-type: application/json');

                         if (count($result) > 0) {

                               //print_r($result);

                               $rate = $this->Webservices_driver_model->driver_rate($request['trip_id']);

                               $result->rating = $rate;

                               print json_encode(array('status' => 'success', 'data' => $result));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '209', 'message' => 'Something Went wrong'));

             }

       }



       public function trip_list_for_today() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $request['auth'] = $auth;

                   $page = isset($_GET['page']) ? $_GET['page'] : 1;

                   if ($page == 0) {

                         $page = 1;

                   }

                   // $start_time = strtotime(date('Y-m-d 00:00:00'));

                   // $end_time = strtotime(date('Y-m-d 23:59:59'));


                   $start_time = strtotime(date('Y-m-d 00:00:00'));

                   $end_time = strtotime(date('Y-m-d 23:59:59'));

         


                   $result = $this->Webservices_driver_model->today_trips($request, $start_time, $end_time);

                   if ($result) {

                         //   print_r($result);

                         $total = $this->db->query($result)->num_rows();

                         $per_page = 2;

                         $total_pages = ceil($total / $per_page);

                         $current_page = $page;


                         if ($total > 0) {

                               $limit = ($per_page * ($current_page - 1));

                               $limit_sql = " LIMIT $limit,$per_page";

                               $result = $this->db->query($result.$limit_sql)->result();

                               $id = $result->driver_id;

                               $start_time = strtotime(date('Y-m-d 00:00:00'));

                               $end_time = strtotime(date('Y-m-d 23:59:59'));

                               $id = $result->driver_id;

                               $query = $this->db->where('unique_id', $auth)->get('driver_auth_table');

                               $rs = $query->row();

                               $driv_id = $rs->driver_id;

                               $fare = $this->Webservices_driver_model->totalfare_today($start_time, $end_time, $driv_id);

                               $rs->total_fare = $fare;

                               $rides = $this->Webservices_driver_model->total_rides($start_time, $end_time, $driv_id);

                               $rs->total_rides_taken = $rides;

                               $tot_onlinetime = $this->Webservices_driver_model->total_online_time($driv_id);

                               $rs->total_online_time = $tot_onlinetime;

                               //print_r($result);

                               print json_encode(array('status' => 'success', 'data' => array('total_fare' => $fare, 'total_online_time' => $tot_onlinetime, 'total_rides_taken' => $rides, 'trips' => $result), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                         } else {

                               print json_encode(array('status' => 'success', 'data' => array('total_fare' => 0, 'total_rides_taken' => 0, 'total_online_time' => 0, 'trips' => []), 'meta' => array('total' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, 'current_page' => $current_page)));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '503'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'message' => 'Something Went wrong', 'error' => '503'));

             }

       }

       function fcm_usermessage($request) {

             /*$postdata = file_get_contents("php://input");

             $request = json_decode($postdata,true);*/

             $this->Webservices_driver_model->message($request);

             /*

             if($result){

             print json_encode(array('status'=>'success','data'=>array('id'=>$result['trip_id'])));



             }else{

             print json_encode(array('status'=>'error','message'=>'Something Went wrong','error'=>'605'));

             }

              */

       }

       public function updatedriver_status() {

             header('Content-type: application/json');

             if (isset(apache_request_headers()['Auth'])) {

                   $auth = apache_request_headers()['Auth'];

                   $postdata = file_get_contents("php://input");

                   $request = json_decode($postdata, true);

                   $request['auth'] = $auth;

                   if (isset($request['driver_status'])) {

                         $result = $this->Webservices_driver_model->driver_onstatus($request);

                         header('Content-type: application/json');

                         if ($result) {

                               return 0;

                               //print json_encode(array('status' => 'success'));

                         } else {

                               print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                         }

                   } else {

                         print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

                   }

             } else {

                   print json_encode(array('status' => 'error', 'code' => '208', 'message' => 'Something Went wrong'));

             }

       }

      public function update_ride_start() {
        header('Content-type: application/json');

        if(isset(apache_request_headers()['Auth'])){
          $auth = apache_request_headers()['Auth'];
          $postdata = file_get_contents("php://input");
          $request = json_decode($postdata, true);

          if(!empty($auth) && isset($request['trip_id']) && !empty($request['trip_id'])) {
            $request['auth'] = $auth;
            $status = $this->Webservices_driver_model->update_ride_start($request);
            if ($status) {
              $resp = array('status'=>'success');
            } else {
              $resp = array('status'=>'error','code'=>'208','message'=>'Something Went wrong');
            }
          } else {
            $resp = array('status'=>'error','code'=>'209','message'=>'Required Fields are missing');
          }
        } else {
          $resp = array('status'=>'error','code'=>'210','message'=>'Auth Tokon Missing');
        }
        print_r(json_encode($resp));exit;
      }
        
        public function arrived_on_hospital() {

          header('Content-type: application/json');

          if (isset(apache_request_headers()['Auth'])) {

              $auth = apache_request_headers()['Auth'];

              $postdata = file_get_contents("php://input");

              $request = json_decode($postdata, true);

              $request['auth'] = $auth;

              $trip_end_time = time();

              $this->db->where('transport_id', $request['trip_id'])->update('transport_details', array('trip_end_time' => $trip_end_time));

              $res = $this->db->where('transport_id', $request['trip_id'])->get('transport_details')->row();

              $trip_start_time = (isset($res->trip_start_time) && !empty($res->trip_start_time))?$res->trip_start_time:'00:00';

              $trip_end_time = (isset($res->trip_end_time) && !empty($res->trip_end_time))?$res->trip_end_time:'00:00';

              $time = (($trip_end_time - $trip_start_time) / 60);

              $total_time = $time;

              $hours = floor($time / 60);

              $minutes = ($time % 60);

              $time = $hours.':'.$minutes;

              $minutes_travel = intval($total_time);

              if ($minutes_travel == '0') {

                  $hours = '0 : 01';

              } else {

                  $hours = date('H', mktime(0,$minutes));

              }
             
              $array = array('start_time'=>$request['start_time'],
                             'end_time'=>$request['end_time'],'total_time'=>$hours,'status'=>6);

              $this->db->where('transport_id', $request['trip_id'])->update('transport_details', $array);

              $data = $this->db->select('vehicle_type,transport_id,trip_distance,total_time,pickup_lat AS source_latitude,pickup_lng AS source_longitude, drop_lat AS destination_latitude,drop_lng AS destination_longitude')->where('transport_id', $request['trip_id'])->get('transport_details')->row_array();


              $trip_id = $request['trip_id'];

              $data['time'] = $total_time;


              $fare = $this->Webservices_driver_model->fare_calculate($data, $trip_id);
             
              $result = $this->Webservices_driver_model->get_trip_info($request['trip_id']);

              $this->Webservices_driver_model->message($request['trip_id']);

              if ($result) {

                  print json_encode(array('status' => 'success', 'data' => $result));

              } else {

                  print json_encode(array('status' => 'error', 'error' => '211', 'message' => 'Invalid booking details'));

              }

          } else {

              print json_encode(array('status' => 'error', 'error' => '210', 'message' => 'Something Went wrong'));

          }

      }


       public function distance($origins, $destination) {

             $data1 = "SELECT * FROM settings WHERE id = '1' ";

             $query1 = $this->db->query($data1);

             $rs = $query1->row();

             $key = $rs->key;

             $url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$origins&destinations=$destination&mode=driving&key=$key";

             $ch = curl_init();

             curl_setopt($ch, CURLOPT_URL, $url);

             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

             curl_setopt($ch, CURLOPT_PROXYPORT, 3128);

             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

             $response = curl_exec($ch);

             curl_close($ch);

             $response_a = json_decode($response, true);

             return $dist = $response_a['rows'][0]['elements'][0]['distance']['value'];

       }

       function distance_calculate($lat1, $lon1, $lat2, $lon2, $unit) {

             $theta = $lon1 - $lon2;

             $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));

             $dist = acos($dist);

             $dist = rad2deg($dist);

             $miles = $dist * 60 * 1.1515;

             $unit = strtoupper($unit);

             return ($miles * 1.609344);

       }

       function get_cal() {

                $num = 5;
                
                $sum = 0;

                for($count = 1; $count <= $num; $count++)
                {   

                    $sum += $count;

                }

                print_r($sum);exit();

                return $sum;
       }
 }
 ?>