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

    require_once APPPATH . '/third_party/FCM/androidNoti.php';

class Webservice extends CI_Controller {

	/**
	 * Index Page for this controller.ci
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/indexa
	 *	- or -
	 * Since this controller is set as the default controller 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>f
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	 
	 
	  public function __construct(){
    parent:: __construct();
    $this->load->model('webservice_model');
    $this->load->library(['form_validation','email']);  
    define("SITE_URL",'https://techimmense.in/CineramaMaps/'); 
   define("SITE_EMAIL",'info@cineramamaps.com');  
   $this->load->helper('string');
   date_default_timezone_set('Asia/Calcutta');
   error_reporting(0);

  }
	public function index()
	{
		$this->load->view('welcome_message');
	}
	
				 /************* get_place_id_by_addresss_googlemap function *************/

		public function get_place_id_by_addresss_googlemap()
	{
	    $lang = $this->input->get_post('lang');
	    $db_place_id = $this->input->get_post('db_place_id');
	    $address =  $this->input->get_post('address');
	     $address = urlencode($address);
	     
	    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/place/textsearch/json?query='.$address.'&key=AIzaSyAGxLMNvIuc8XpG21cOF3VkxbK1EkTpuzQ&language='.$lang);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($res);
//print_r($response);die;

 $p_id= $response->results[0]->place_id;
//print_r($response);die;
                     $this->webservice_model->update_data('country_map_city_places',['placeid'=>$p_id],['id'=>$db_place_id]);

          $object = (object) ['place_id' =>$p_id];
          $ressult['result']=$object;
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;
           header('Content-type: application/json');
      echo json_encode($json);die;
//print_r($response);die;
	}
	
			 /************* get_details_by_place_id_googlemap function *************/

			public function get_details_by_place_id_googlemap()
	{
	    $placeid =  $this->input->get_post('place_id');
	    $user_id =  $this->input->get_post('user_id');
	    $lang = $this->input->get_post('lang');


  $country_map_city_places = $this->webservice_model->get_where('country_map_city_places',['placeid'=>$placeid]);

if($country_map_city_places){


      $lat = $this->input->get_post('lat');
      $lon = $this->input->get_post('lon');
if($lat){
    $lat = $this->input->get_post('lat');
      $lon = $this->input->get_post('lon');
}else{
    $lat = '22.56';
      $lon = '75.65';
}


               if($country_map_city_places[0]['lat']){
                   $lat1 = $country_map_city_places[0]['lat'];

                   $lon1 = $country_map_city_places[0]['lon'];
               }else{
                   $lat1 = '22.22';

                   $lon1 = '75.55';
                   
               }
                          $distance = $this->webservice_model->distance($lat, $lon, $lat1, $lon1, $miles = false);

               
               $country_map_city_places[0]['distance']=number_format($distance,2);
                 


          $place_id = $country_map_city_places[0]['id'];
            
            $country_map_city_places[0]['icon']=SITE_URL.'uploads/images/'.$country_map_city_places[0]['icon'];  
           
             if($user_id){
		     	    $rest_like_check = $this->webservice_model->get_where('unfav_place',['user_id'=>$user_id,'place_id'=>$place_id]);
                       if($rest_like_check){
                           
                           			    $country_map_city_places[0]['fav_status']=$rest_like_check[0]['status'];  
                
                       }else{
                           			    $country_map_city_places[0]['fav_status']='No';  
                
                       }
                       
        }else{
                           			    $country_map_city_places[0]['fav_status']='No';  
            
        }
            
                          $total_unfav_place = $this->db->query("select COUNT(id) AS total_unfav_place from unfav_place where place_id = $place_id AND status = 'Unlike'")->result_array();
                
                          $country_map_city_places[0]['total_unfav_place'] = $total_unfav_place[0]['total_unfav_place'];
                       
                       
                          $total_fav_place = $this->db->query("select COUNT(id) AS total_fav_place from unfav_place where place_id = $place_id AND status = 'Like'")->result_array();
                
                          $country_map_city_places[0]['total_fav_place'] = $total_fav_place[0]['total_fav_place'];
                         
              
            
            
               $country_id = $country_map_city_places[0]['country_id'];
               $fetch_country_map = $this->webservice_model->get_where('country_map',['id'=>$country_id]);
if($fetch_country_map){
    
                $fetch_country_map[0]['country_icon']=SITE_URL.'uploads/images/'.$fetch_country_map[0]['country_icon'];  
                $fetch_country_map[0]['image']=SITE_URL.'uploads/images/'.$fetch_country_map[0]['image'];  
                $country_map_city_places[0]['country_details'] = $fetch_country_map[0];
}else{
                $country_map_city_places[0]['country_details'] = (object)[];

    
}


 $city_id = $country_map_city_places[0]['city_id'];
               $country_map_city = $this->webservice_model->get_where('country_map_city',['id'=>$city_id]);
if($country_map_city){
                $country_map_city[0]['image']=SITE_URL.'uploads/images/'.$country_map_city[0]['image'];  

        $country_map_city_places[0]['city_details'] = $country_map_city[0];

}else{      
    $country_map_city_places[0]['city_details'] = (object)[];

    
}


    $country_map_city_places[0]['avg_rating'] = "5.0";
            
            
             $tag_id = $country_map_city_places[0]['tag_id'];
             
             if($tag_id){
           $exp = explode(',',$tag_id);

     
       foreach($exp as $val1)

        {
            $id1 = $val1;
         $arr_get1 = "id = '$id1'";
             
    	 $fetch1 = $this->webservice_model->get_where('tags', $arr_get1);

	     $country_map_city_places[0]['tag_details'][]=$fetch1[0];

	    

        }
             }else{
                  $country_map_city_places[0]['tag_details']=[];
             }
             
             
             
                 
        
        
        
             $arr_whr = ['place_id'=>$place_id];	           

                         $country_map_city_places_images = $this->webservice_model->get_where('country_map_city_places_images', $arr_whr);
                         if($country_map_city_places_images){
                             foreach($country_map_city_places_images as $val_img)
                               {
                                  $val_img['image']=SITE_URL.'uploads/images/'.$val_img['image'];  

                                 $country_map_city_places[0]['places_images'][]=$val_img;
                               }
                                }else{
                                 $country_map_city_places[0]['places_images']=[];
                                            
                               }
                               
                               

         
        



	$fetch_rating_review = $this->webservice_model->get_where('rating_review', ['place_id'=>$place_id,'type'=>'Place']);
	
    if ($fetch_rating_review)
      {
       foreach($fetch_rating_review as $val1)

        {

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['user_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      
         $data_review[] = $val1;
        }
         $country_map_city_places[0]['rating_review'] =$data_review;

      }else{
         $country_map_city_places[0]['rating_review'] =[];
          
      }
      
     
        $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/place/details/json?placeid='.$placeid.'&key=AIzaSyAGxLMNvIuc8XpG21cOF3VkxbK1EkTpuzQ&fields=formatted_phone_number,formatted_address,name,rating,photos,plus_code,reviews,url,website,address_components,adr_address,business_status,current_opening_hours,delivery,formatted_phone_number,geometry,icon,icon_background_color,icon_mask_base_uri,international_phone_number,opening_hours,place_id,editorial_summary&language='.$lang);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($res);

      

$country_map_city_places[0]['google_map'] = $response;
      
         $ressult['result'] = $country_map_city_places[0];
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
         
          header('Content-type: application/json');
          echo json_encode($json);die;
      }
      else
      {
         $ressult['result'] = (object)[];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);die;
      }
      
      
	  
	}
	
	
		 /************* get_photos_by_place_id_googlemap function *************/

		public function get_photos_by_place_id_googlemap()
	{
	    $place_id =  $this->input->get_post('place_id');
	    $lang = $this->input->get_post('lang');

	    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/place/details/json?placeid='.$place_id.'&key=AIzaSyAGxLMNvIuc8XpG21cOF3VkxbK1EkTpuzQ&fields=photos,place_id&language='.$lang);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);

curl_close($ch);
$response = json_decode($res);
//print_r($response);
if (isset($response->result)) {
    
  $photo= $response->result->photos; 
  
 
$de=[];
foreach($photo as $val_photo) {
     $photo_reference = $val_photo->photo_reference;
     $place_photo =  'https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photo_reference='.$photo_reference.'&key=AIzaSyAGxLMNvIuc8XpG21cOF3VkxbK1EkTpuzQ&fields=photos,place_id';

     $de[] = ['place_id' =>$place_id,'p_photo' =>$place_photo];
}

}
          $ressult['result']=$de;
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;
           header('Content-type: application/json');
           echo json_encode($json);die;
//print_r($response);die;
	}
	
	
	
	 /************* get_country_map_city_places_new function *************/


public

  function get_country_map_city_places_new()
    {
        
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;
	    $lang = $this->input->get_post('lang');
            $lat = $this->input->get_post('lat', TRUE) ;
            $lon = $this->input->get_post('lon', TRUE) ;


    $fetch_city = $this->webservice_model->get_where('country_map_city',['id'=>$city_id]);

if($fetch_city){

            
///////////// tag start here

    $fetch_city_tag = $this->webservice_model->get_where('tags',['city_id'=>$city_id]);
    
    if($fetch_city_tag){
        
         foreach($fetch_city_tag as $val_tag)
        {
            
            $val_tag['icon']=SITE_URL.'uploads/images/'.$val_tag['icon'];  
           
            $t_id = $val_tag['id'];

            $total_tag_place_count = $this->db->query("select COUNT(id) AS total_tag_place_count from country_map_city_places where city_id = $city_id AND FIND_IN_SET('$t_id', `tag_id`) ")->result_array();
                if($total_tag_place_count[0]['total_tag_place_count'] == 0){
                    
                   $val_tag['total_tag_place_count'] = $total_tag_place_count[0]['total_tag_place_count'];
                    //continue;
                }else{
            $val_tag['total_tag_place_count'] = $total_tag_place_count[0]['total_tag_place_count'];
                }
                       
            $fetch_city[0]['tags'][] = $val_tag;
        }
    }else{
            $fetch_city[0]['tags']=[];
        
    }

//////////// tag end here
    $fetch = $this->webservice_model->get_where('country_map_city_places',['city_id'=>$city_id]);
    if ($fetch)
      {
      foreach($fetch as $val)
        {
            
            
            ///////// distance start
            
             $lat = $this->input->get_post('lat');
             $lon = $this->input->get_post('lon');
if($lat){
    $lat = $this->input->get_post('lat');
      $lon = $this->input->get_post('lon');
}else{
    $lat = '22.56';
      $lon = '75.65';
}


               if($val['lat']){
                   $lat1 = $val['lat'];

                   $lon1 = $val['lon'];
               }else{
                   $lat1 = '22.22';

                   $lon1 = '75.55';
                   
               }
                          $distance = $this->webservice_model->distance($lat, $lon, $lat1, $lon1, $miles = false);

               
                           $val['distance']=number_format($distance,2);
                 


            ////////// distance end 

            ///////1111111
             $address =  $val['address'];
	     $address = urlencode($address);
	     
	    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/place/textsearch/json?query='.$address.'&key=AIzaSyAGxLMNvIuc8XpG21cOF3VkxbK1EkTpuzQ&language='.$lang);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);

curl_close($ch);
$response = json_decode($res);
//print_r($response);die;

$val['map_icon'] = $response->results[0]->icon;
$val['map_icon_background_color'] = $response->results[0]->icon_background_color;
            /////////11111111
            
            $place_id = $val['id'];
            
             if($user_id){
		     	    $rest_like_check = $this->webservice_model->get_where('unfav_place',['user_id'=>$user_id,'place_id'=>$place_id]);
                       if($rest_like_check){
                           
                           			    $val['fav_status']=$rest_like_check[0]['status'];  
                
                       }else{
                           			    $val['fav_status']='No';  
                
                       }
                       
        }else{
                           			    $val['fav_status']='No';  
            
        }
            
                          $total_unfav_place = $this->db->query("select COUNT(id) AS total_unfav_place from unfav_place where place_id = $place_id AND status = 'Unlike'")->result_array();
                
                          $val['total_unfav_place'] = $total_unfav_place[0]['total_unfav_place'];
                       
                       
                          $total_fav_place = $this->db->query("select COUNT(id) AS total_fav_place from unfav_place where place_id = $place_id AND status = 'Like'")->result_array();
                
                          $val['total_fav_place'] = $total_fav_place[0]['total_fav_place'];
                         
              
            
            
            $country_id = $val['country_id'];
               $fetch_country_map = $this->webservice_model->get_where('country_map',['id'=>$country_id]);
if($fetch_country_map){
    $val['country_name'] = $fetch_country_map[0]['name'];
    $val['country_name_ar'] = $fetch_country_map[0]['name_ar'];
}else{
    $val['country_name'] = '';
    $val['country_name_ar'] = '';
    
}


 $city_id = $val['city_id'];
               $country_map_city = $this->webservice_model->get_where('country_map_city',['id'=>$city_id]);
if($country_map_city){
    $val['city_name'] = $country_map_city[0]['name'];
    $val['city_name_ar'] = $country_map_city[0]['name_ar'];
}else{
    $val['city_name'] = '';
    $val['city_name_ar'] = '';
    
}


    $val['avg_rating'] = "5.0";
            
            
               $val['plan_purchase_status']='No';  
          $val['icon']=SITE_URL.'uploads/images/'.$val['icon'];
          
             $tag_id = $val['tag_id'];
             
             if($tag_id){
           $exp = explode(',',$tag_id);

     
       foreach($exp as $val1)

        {
            $id1 = $val1;
         $arr_get1 = "id = '$id1'";
             
    	 $fetch1 = $this->webservice_model->get_where('tags', $arr_get1);

	     $val['tag_details'][]=$fetch1[0];

	    

        }
             }else{
                  $val['tag_details']=[];
             }
             
             
             
                 
        
        
        
             $arr_whr = ['place_id'=>$place_id];	           

                         $country_map_city_places_images = $this->webservice_model->get_where('country_map_city_places_images', $arr_whr);
                         if($country_map_city_places_images){
                             foreach($country_map_city_places_images as $val_img)
                               {
                                  $val_img['image']=SITE_URL.'uploads/images/'.$val_img['image'];  

                                 $fetch_city[0]['places_images'][]=$val_img;
                               }
                                }else{
                                 $fetch_city[0]['places_images']=[];
                                            
                               }
                               
                               

          $data[] = $val;
        }



	$fetch_rating_review = $this->webservice_model->get_where('rating_review', ['city_id'=>$city_id]);
	
    if ($fetch_rating_review)
      {
       foreach($fetch_rating_review as $val1)

        {

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['user_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      
         $data_review[] = $val1;
        }
         $fetch_city[0]['rating_review'] =$data_review;

      }else{
         $fetch_city[0]['rating_review'] =[];
          
      }
      
      
         $fetch_city[0]['place_details']  = $data;
         $ressult['result'] = $fetch_city[0];
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
         
          header('Content-type: application/json');
          echo json_encode($json);die;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);die;
      }


}else{
    
    
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);die;
}
    header('Content-type: application/json');
    echo json_encode($json);
    }
    
	/************* get_profile function *************/

     public function get_profile(){

      $user_id = $this->input->get_post('user_id');
      $arr_get = ['id'=>$this->input->get_post('user_id')];

      $login = $this->webservice_model->get_where('users',$arr_get);

      if ($login) {  
     
     
       if($user_id){
             $c_date = date('Y-m-d H:i:s');
                       $rest_list = $this->db->query("SELECT * FROM plan_purchase WHERE status = 'Complete' AND user_id = '$user_id' AND exp_date >= '$c_date'")->result_array();
if($rest_list){
    $login[0]['subscription_status'] = 'Yes';
}else{
    $login[0]['subscription_status'] = 'No';
    
}
}else{
    $login[0]['subscription_status'] = 'No';
    
}


          /*           $c_date = date('Y-m-d H:i:s');
             
                

 $get = $this->db->select_avg("rating", "rating")->where(['rest_id'=>$user_id])->get('rating_review')->result_array();

         $rating = ($get[0]['rating']=='') ?  0 : $get[0]['rating'];   

         
         $login[0]['rating'] = "$rating";
*/

            $login[0]['image']=SITE_URL.'uploads/images/'.$login[0]['image'];
         //   $login[0]['provider_logo']=SITE_URL.'uploads/images/'.$login[0]['provider_logo'];
          //  $login[0]['store_logo']=SITE_URL.'uploads/images/'.$login[0]['store_logo'];
           // $login[0]['store_cover_image']=SITE_URL.'uploads/images/'.$login[0]['store_cover_image'];
           // $login[0]['licence_image']=SITE_URL.'uploads/images/'.$login[0]['licence_image'];
           // $login[0]['id_proof_image']=SITE_URL.'uploads/images/'.$login[0]['id_proof_image'];
           // $login[0]['vehicle_insura_image']=SITE_URL.'uploads/images/'.$login[0]['vehicle_insura_image'];
           // $login[0]['registration_image']=SITE_URL.'uploads/images/'.$login[0]['registration_image'];
           
         

  $login_token  = $this->webservice_model->get_where('tokens',['user_id'=>$user_id]); 
  
  //$login[0]['token'] = $login_token[0]['authorization_token'];
         
       
         
       $pro_noti = $this->db->query("select COUNT(id) AS total_cart from notification where user_id = '$user_id' AND seen_status = 'UNSEEN' ")->result_array();
       $login[0]['noti_count']=$pro_noti[0]['total_cart'];
     
   
         

 /*$arr_get1 = ['id'=>$login[0]['cat_id']];

      $login_cat = $this->webservice_model->get_where('category',$arr_get1);
if($login_cat){
    $login[0]['cat_name'] = $login_cat[0]['name'];
}else{
    $login[0]['cat_name'] ='';
    
}
*/

/*
 $login_image = $this->webservice_model->get_where('provider_images',['user_id'=>$login[0]['id']]);
                      if($login_image){
                                foreach($login_image as $val)
                               {
                                
                               
                                   $val['image']=SITE_URL.'uploads/images/'.$val['image'];  
                                   $login[0]['provider_images'][]=$val;
                                  }
                              
                                }else{
                                   $login[0]['provider_images']=[];
    
                                }
*/

          $ressult['result']=$login[0];
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;

      }else{

        $json = ['result'=>(object)[],'status'=>'0','message'=>'Data Not Found'];

      }

      header('Content-type: application/json');
      echo json_encode($json);
    }      




/************* send_otp function *************/

    public function send_otp(){

$instance_id = "66D6EF1460B0B";
$access_token = "969058519vqbQ9";
$code =rand(1111,9999);
$msg = "Your CineramaMaps app otp is: ".$code;
       $msg = urlencode($msg);

$mobile =$this->input->get_post('mobile');
$type =$this->input->get_post('type');
$mobile_with_code =$this->input->get_post('mobile_with_code');

                $register_id = $this->input->get_post('register_id');
                $ios_register_id= $this->input->get_post('ios_register_id');
                
                $lat = $this->input->get_post('lat', TRUE);
                $lon = $this->input->get_post('lon', TRUE);


 $arr_get = "mobile = '$mobile'";


      $login = $this->webservice_model->get_where('users',$arr_get);
      if ($login) {
          $signup_statu = 'Yes';
          
          if($register_id){


                     $this->webservice_model->update_data('users',['register_id'=>$register_id],$arr_get);

          }
          
           if($ios_register_id){


                     $this->webservice_model->update_data('users',['ios_register_id'=>$ios_register_id],$arr_get);

          }
              
                    $login = $this->webservice_model->get_where('users',$arr_get);
               $user_details = $login[0];

                   
      }else{
                    $user_details = (object)[];

          $signup_statu = 'No';
          
      }

         $url = "https://wh.line.sa/api/send?instance_id=6703F8C121330&access_token=969058519vqbQ9&message=$msg&number=$mobile_with_code&type=text";

$ch = curl_init();    
curl_setopt($ch, CURLOPT_URL,$url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable  
curl_setopt($ch, CURLOPT_TIMEOUT, 10);  
$result = curl_exec($ch);  

   
$response = json_decode($result);


//print_r($response);die;
//curl_close($curl);

       if (isset($response->error)) {
          $ressult['result']=$response;
          $ressult['signup_statu']=$signup_statu;
          $ressult['user_details']=$user_details;
          $ressult['message']='unsuccessful';
          $ressult['status']='0';
          $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);
          die;
          
       }else{

     //  curl_close ($ch);  
                          $object = (object) ['code' =>$code];
                          
                          $ressult['result']=$object;
                        //  $ressult['result']=$response;
                          $ressult['message']='successful';
                          $ressult['signup_statu']=$signup_statu;
                          $ressult['user_details']=$user_details;
                          $ressult['status']='1';
                          $json = $ressult;   
                          
          header('Content-type: application/json');
          echo json_encode($json);die;
       }

         
    }
   
   


 /************* signup function *************/

    public function signup(){

  $first_name = $this->input->get_post('first_name');

  
  
             $password = $this->input->get_post('password', TRUE);
             $password = base64_encode($password);

      $mobile = $this->input->get_post('mobile');
      if($first_name != '' && $mobile !=''){
          
                  $first_name4digit =  substr($first_name, 0, 4);
                  $mobile_last3digit =  substr( $mobile, -3);
                  $referral_code = $first_name4digit.$mobile_last3digit;
      }else{
                  $referral_code =  $this->webservice_model->generateRandomString(5);

      }
       date_default_timezone_set('Asia/Calcutta');
        $date_time =  date('Y-m-d H:i:s');
                                   
                                      $signup_referral_code =$this->input->get_post('signup_referral_code');
if($signup_referral_code){
                                          $signup_referral_code =$this->input->get_post('signup_referral_code');

}else{
    $signup_referral_code = '';
}

        date_default_timezone_set('Asia/Calcutta');
        $date_time =  date('Y-m-d H:i:s');
       
       
      
         $arr_data = [
            'first_name'=>$this->input->get_post('first_name'),
            'last_name'=>$this->input->get_post('last_name'),
            'email'=>$this->input->get_post('email'),
            'mobile'=>$this->input->get_post('mobile'),
            'mobile_with_code'=>$this->input->get_post('mobile_with_code'),
            'address'=>$this->input->get_post('address'),
            'lat'=>$this->input->get_post('lat'),
            'lon'=>$this->input->get_post('lon'),
            'gender'=>$this->input->get_post('gender'),
            'dob'=>$this->input->get_post('dob'),
            'register_id'=>$this->input->get_post('register_id'),
            'date_time'=>$date_time,
            'ios_register_id'=>$this->input->get_post('ios_register_id') 
            ];
      
    


     

                        if (isset($_FILES['image']))
      {
               $n = rand(0, 100000);
               $path_info = pathinfo($_FILES['image']['name']);
               $img = "USER_IMG_" . $n .'.'.$path_info['extension'];
               move_uploaded_file($_FILES['image']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image'] = $img;        
      }



      $id = $this->webservice_model->insert_data('users',$arr_data);

      if ($id=="") {
        $json = ['result'=>(object)[],'status'=>'0','message'=>'data not found'];
      }else{
                             
        
        
         
        $arr_gets = ['id'=>$id];
        $arr_gets_token = ['user_id'=>$id];
        $userId = $id;
        $login = $this->webservice_model->get_where('users',$arr_gets); 
        
        
        $login[0]['image']=SITE_URL.'uploads/images/'.$login[0]['image'];
        $ressult['result']=$login[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }

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

    }


/***************get_user_page *****************/
 public
     function get_user_page()
        {  
           $category_id = $this->input->get_post('category_id', TRUE); 
           $user_id = $this->input->get_post('user_id', TRUE); 
           
           $fetch = $this->db->query("SELECT * FROM settings WHERE type = 'USER' ")->result_array();


          if ($fetch){
              
               
                                $data['result']=$fetch[0];
                                $data['message']='successfull';
                                $data['status']='1';
                                $json = $data;
            }
              else
              {
                                        $data['result']=(object)[];
                                        $data['message']='No Request Found';
                                        $data['status']='0';
                                        $json = $data; 
              }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
     
    
/************* get_plans function *************/
   
   
public

  function get_plans()
    {
        
           
    $fetch = $this->webservice_model->get_all('plans');
    if ($fetch)
      {
      foreach($fetch as $val)
        {
          $val['is_select'] = 'No';
          $val['offer_image']=SITE_URL.'uploads/images/'.$val['offer_image'];
          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    


                /************* delete_account *************/
    public

    function delete_account()
    {
                  $user_id = $this->input->get_post('user_id');
                  $list = $this->webservice_model->get_where('users',['id'=>$user_id]);

      if ($list)
      {
                   $this->webservice_model->delete_data('users',['id'=>$user_id]);

                          $ressult['result']="delete successfull";
                          $ressult['message']='successful';
                          $ressult['status']='1';
                          $json = $ressult;
      }
        else
      {
                          $ressult['result']='unsuccessful';
                          $ressult['message']='unsuccessful';
                          $ressult['status']='0';
                          $json = $ressult;                              
        
      }

        
                     
      header('Content-type: application/json');
      echo json_encode($json);
    }



/************* update_profile function *************/

    public function update_profile(){


/*
$token = $this->input->get_post('token', TRUE);
               if($token){
                   
                    $where_token = "authorization_token = '$token'";
    

     

      $login_token = $this->webservice_model->get_where('tokens',$where_token);
      if ($login_token) {



               }else{
                   
                                $ressult['result']=(object)[];
                                $ressult['message']='Wrong authorization';
                                $ressult['status']='0';
                                $json = $ressult;  
                                
                                  header('Content-type:application/json');
                                  echo json_encode($json);die;
               } 
               
               }else{
                   
                                $ressult['result']=(object)[];
                                $ressult['message']='Wrong authorization';
                                $ressult['status']='0';
                                $json = $ressult;  
                                
                                  header('Content-type:application/json');
                                  echo json_encode($json);die;
               }
               */
               
      $arr_get = ['id'=>$this->input->get_post('user_id')];

      $login = $this->webservice_model->get_where('users',$arr_get);
      if ($login[0]['id'] == "")
      {
                          $ressult['result']=(object)[];
                          $ressult['message']='unsuccessfull';
                          $ressult['status']='0';
                          $json = $ressult;

                                header('Content-type:application/json');
                                echo json_encode($json);
                                die;
      }



       $arr_data = [
           'first_name'=>$this->input->get_post('first_name'),
           'last_name'=>$this->input->get_post('last_name'),
            'email'=>$this->input->get_post('email'),
            'address'=>$this->input->get_post('address'),
            'lat'=>$this->input->get_post('lat'),
            'lon'=>$this->input->get_post('lon'),
            'gender'=>$this->input->get_post('gender'),
            'dob'=>$this->input->get_post('dob'),
            
            ];

                    


 
                        if (isset($_FILES['image']))
      {
               $n = rand(0, 100000);
                $path_info = pathinfo($_FILES['image']['name']);
               $img = "USER_IMG_" . $n .'.'.$path_info['extension'];
               move_uploaded_file($_FILES['image']['tmp_name'], "uploads/images/" . $img);
               $arr_data['image'] = $img;        
      }
        

      $res = $this->webservice_model->update_data('users',$arr_data,$arr_get);
      if ($res)
      {
        $data = $this->webservice_model->get_where('users',$arr_get);
        $data[0]['image']=SITE_URL.'uploads/images/'.$data[0]['image'];
        
        $data[0]['provider_logo']=SITE_URL.'uploads/images/'.$data[0]['provider_logo'];
              $data[0]['token'] = '';

        $ressult['result']=$data[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }
      else
      {
                          $ressult['result']=(object)[];
                          $ressult['message']='unsuccessfull';
                          $ressult['status']='0';
                          $json = $ressult;
      }

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

                          

    }

 	
 	
/************* get_country_map function *************/
   
   
public

  function get_country_map()
    {
        
            $user_id = $this->input->get_post('user_id', TRUE) ;
            $type = $this->input->get_post('type', TRUE) ;
              $c_date = date('Y-m-d H:i:s');
if($type =='All'){

    $fetch = $this->webservice_model->get_all('country_map');
}else{
    
    $fetch = $this->db->query("SELECT * FROM country_map WHERE remove_status = 'No' AND  id IN(select map_country_id from plan_purchase where status = 'Complete' AND user_id = '$user_id' AND exp_date >= '$c_date') order by id desc")->result_array();

}
    if ($fetch)
      {
      foreach($fetch as $val)
        {
            
            if($user_id){
             $c_date = date('Y-m-d H:i:s');
$map_country_id = $val['id'];
                       $rest_list = $this->db->query("SELECT * FROM plan_purchase WHERE status = 'Complete' AND map_country_id = '$map_country_id' AND user_id = '$user_id' AND exp_date >= '$c_date'")->result_array();
if($rest_list){
    $val['subscription_status'] = 'Yes';
}else{
    $val['subscription_status'] = 'No';
    
}
}else{
    $val['subscription_status'] = 'No';
    
}
            
                                   $val['country_icon']=SITE_URL.'uploads/images/'.$val['country_icon'];  
                                   $val['image']=SITE_URL.'uploads/images/'.$val['image'];  

          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
    /************* get_country_map_city function *************/


public

  function get_country_map_city()
    {
        
            $country_id = $this->input->get_post('country_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;


    $fetch = $this->webservice_model->get_where('country_map_city',['country_id'=>$country_id]);
    if ($fetch)
      {
      foreach($fetch as $val)
        {
            $city_id = $val['id'];
            $city_address = $val['address'];
            $address = $val['name'];
          if($city_address ==''){
              $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/place/textsearch/json?query='.$address.'&key=AIzaSyAGxLMNvIuc8XpG21cOF3VkxbK1EkTpuzQ');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($res);
//print_r($response);die;

 $p_address= $response->results[0]->formatted_address;

  $p_lat= $response->results[0]->geometry->location->lat;
  $p_lon= $response->results[0]->geometry->location->lng;
//print_r($response);die;
                     $this->webservice_model->update_data('country_map_city',['address'=>$p_address,'lat'=>$p_lat,'lon'=>$p_lon],['id'=>$city_id]);

}

            
                $fetch_country_map = $this->webservice_model->get_where('country_map',['id'=>$country_id]);
if($fetch_country_map){
    $val['country_name'] = $fetch_country_map[0]['name'];
    $val['country_name_ar'] = $fetch_country_map[0]['name_ar'];
}else{
    $val['country_name'] = '';
    $val['country_name_ar'] = '';
    
}
			 $city_id =  $val['id'];     
         $get = $this->db->select_avg("rating", "rating")->where(['city_id'=>$city_id])->get('rating_review')->result_array();

         $rating = ($get[0]['rating']=='') ?  0 : $get[0]['rating'];   

         
          $val['avg_rating'] = number_format($rating,2);



          $val['image']=SITE_URL.'uploads/images/'.$val['image'];
          
          
             $tag_id = $val['tag_id'];
             
             if($tag_id){
           $exp = explode(',',$tag_id);

     
       foreach($exp as $val1)

        {
            $id1 = $val1;
         $arr_get1 = "id = '$id1'";
             
    	 $fetch1 = $this->webservice_model->get_where('tags', $arr_get1);

	     $val['tag_details'][]=$fetch1[0];

	    

        }
             }else{
                  $val['tag_details']=[];
             }
        
        
        if($user_id){
                    $city_id = $val['id'];
		     	    $rest_like_check = $this->webservice_model->get_where('unfav_citymap',['user_id'=>$user_id,'city_id'=>$city_id]);
                       if($rest_like_check){
                           			    $val['fav_status']='Yes';  
                
                       }else{
                           			    $val['fav_status']='No';  
                
                       }
                       
        }else{
                           			    $val['fav_status']='No';  
            
        }
        
        
          if($user_id){
             $c_date = date('Y-m-d H:i:s');
$map_country_id = $val['id'];
                       $rest_list = $this->db->query("SELECT * FROM plan_purchase WHERE status = 'Complete' AND map_city_id = '$city_id' AND user_id = '$user_id' AND exp_date >= '$c_date'")->result_array();
if($rest_list){
    $val['subscription_status'] = 'Yes';
}else{
    $val['subscription_status'] = 'No';
    
}
}else{
    $val['subscription_status'] = 'No';
    
}

          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
 	
 	
 	
    
    /************* get_country_map_city_places function *************/


public

  function get_country_map_city_places()
    {
        
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;


    $fetch_city = $this->webservice_model->get_where('country_map_city',['id'=>$city_id]);

if($fetch_city){

            
///////////// tag start here

    $fetch_city_tag = $this->webservice_model->get_where('tags',['city_id'=>$city_id]);
    
    if($fetch_city_tag){
        
         foreach($fetch_city_tag as $val_tag)
        {
            
            $val_tag['icon']=SITE_URL.'uploads/images/'.$val_tag['icon'];  
           
            $t_id = $val_tag['id'];

            $total_tag_place_count = $this->db->query("select COUNT(id) AS total_tag_place_count from country_map_city_places where city_id = $city_id AND tag_id = $t_id")->result_array();
                if($total_tag_place_count[0]['total_tag_place_count'] == 0){
                    
                    continue;
                }else{
            $val_tag['total_tag_place_count'] = $total_tag_place_count[0]['total_tag_place_count'];
                }
                       
            $fetch_city[0]['tags'][] = $val_tag;
        }
    }else{
            $fetch_city[0]['tags']=[];
        
    }

//////////// tag end here
    $fetch = $this->webservice_model->get_where('country_map_city_places',['city_id'=>$city_id]);
    if ($fetch)
      {
      foreach($fetch as $val)
        {

            
            
            $place_id = $val['id'];
            
             if($user_id){
		     	    $rest_like_check = $this->webservice_model->get_where('unfav_place',['user_id'=>$user_id,'place_id'=>$place_id]);
                       if($rest_like_check){
                           
                           			    $val['fav_status']=$rest_like_check[0]['status'];  
                
                       }else{
                           			    $val['fav_status']='No';  
                
                       }
                       
        }else{
                           			    $val['fav_status']='No';  
            
        }
            
                          $total_unfav_place = $this->db->query("select COUNT(id) AS total_unfav_place from unfav_place where place_id = $place_id AND status = 'Unlike'")->result_array();
                
                          $val['total_unfav_place'] = $total_unfav_place[0]['total_unfav_place'];
                       
                       
                          $total_fav_place = $this->db->query("select COUNT(id) AS total_fav_place from unfav_place where place_id = $place_id AND status = 'Like'")->result_array();
                
                          $val['total_fav_place'] = $total_fav_place[0]['total_fav_place'];
                         
              
            
            
            $country_id = $val['country_id'];
               $fetch_country_map = $this->webservice_model->get_where('country_map',['id'=>$country_id]);
if($fetch_country_map){
    $val['country_name'] = $fetch_country_map[0]['name'];
    $val['country_name_ar'] = $fetch_country_map[0]['name_ar'];
}else{
    $val['country_name'] = '';
    $val['country_name_ar'] = '';
    
}


 $city_id = $val['city_id'];
               $country_map_city = $this->webservice_model->get_where('country_map_city',['id'=>$city_id]);
if($country_map_city){
    $val['city_name'] = $country_map_city[0]['name'];
    $val['city_name_ar'] = $country_map_city[0]['name_ar'];
}else{
    $val['city_name'] = '';
    $val['city_name_ar'] = '';
    
}


    $val['avg_rating'] = "5.0";
            
            
               $val['plan_purchase_status']='No';  
          $val['icon']=SITE_URL.'uploads/images/'.$val['icon'];
          
             $tag_id = $val['tag_id'];
             
             if($tag_id){
           $exp = explode(',',$tag_id);

     
       foreach($exp as $val1)

        {
            $id1 = $val1;
         $arr_get1 = "id = '$id1'";
             
    	 $fetch1 = $this->webservice_model->get_where('tags', $arr_get1);

	     $val['tag_details'][]=$fetch1[0];

	    

        }
             }else{
                  $val['tag_details']=[];
             }
             
             
             
                 
        
        
        
             $arr_whr = ['place_id'=>$place_id];	           

                         $country_map_city_places_images = $this->webservice_model->get_where('country_map_city_places_images', $arr_whr);
                         if($country_map_city_places_images){
                             foreach($country_map_city_places_images as $val_img)
                               {
                                  $val_img['image']=SITE_URL.'uploads/images/'.$val_img['image'];  

                                 $fetch_city[0]['places_images'][]=$val_img;
                               }
                                }else{
                                 $fetch_city[0]['places_images']=[];
                                            
                               }
                               
                               

          $data[] = $val;
        }



	$fetch_rating_review = $this->webservice_model->get_where('rating_review', ['city_id'=>$city_id]);
	
    if ($fetch_rating_review)
      {
       foreach($fetch_rating_review as $val1)

        {

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['user_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      
         $data_review[] = $val1;
        }
         $fetch_city[0]['rating_review'] =$data_review;

      }else{
         $fetch_city[0]['rating_review'] =[];
          
      }
      
      
         $fetch_city[0]['place_details']  = $data;
         $ressult['result'] = $fetch_city[0];
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
         
          header('Content-type: application/json');
          echo json_encode($json);die;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);die;
      }


}else{
    
    
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);die;
}
    header('Content-type: application/json');
    echo json_encode($json);
    }
    



 
    /************* get_place_details function *************/


public

  function get_place_details()
    {
        
            $place_id = $this->input->get_post('place_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;


    $country_map_city_places = $this->webservice_model->get_where('country_map_city_places',['id'=>$place_id]);

if($country_map_city_places){

          
            
            $country_map_city_places[0]['icon']=SITE_URL.'uploads/images/'.$country_map_city_places[0]['icon'];  
           
             if($user_id){
		     	    $rest_like_check = $this->webservice_model->get_where('unfav_place',['user_id'=>$user_id,'place_id'=>$place_id]);
                       if($rest_like_check){
                           
                           			    $country_map_city_places[0]['fav_status']=$rest_like_check[0]['status'];  
                
                       }else{
                           			    $country_map_city_places[0]['fav_status']='No';  
                
                       }
                       
        }else{
                           			    $country_map_city_places[0]['fav_status']='No';  
            
        }
            
                          $total_unfav_place = $this->db->query("select COUNT(id) AS total_unfav_place from unfav_place where place_id = $place_id AND status = 'Unlike'")->result_array();
                
                          $country_map_city_places[0]['total_unfav_place'] = $total_unfav_place[0]['total_unfav_place'];
                       
                       
                          $total_fav_place = $this->db->query("select COUNT(id) AS total_fav_place from unfav_place where place_id = $place_id AND status = 'Like'")->result_array();
                
                          $country_map_city_places[0]['total_fav_place'] = $total_fav_place[0]['total_fav_place'];
                         
              
            
            
               $country_id = $country_map_city_places[0]['country_id'];
               $fetch_country_map = $this->webservice_model->get_where('country_map',['id'=>$country_id]);
if($fetch_country_map){
    
                $fetch_country_map[0]['country_icon']=SITE_URL.'uploads/images/'.$fetch_country_map[0]['country_icon'];  
                $fetch_country_map[0]['image']=SITE_URL.'uploads/images/'.$fetch_country_map[0]['image'];  
                $country_map_city_places[0]['country_details'] = $fetch_country_map[0];
}else{
                $country_map_city_places[0]['country_details'] = (object)[];

    
}


 $city_id = $country_map_city_places[0]['city_id'];
               $country_map_city = $this->webservice_model->get_where('country_map_city',['id'=>$city_id]);
if($country_map_city){
                $country_map_city[0]['image']=SITE_URL.'uploads/images/'.$country_map_city[0]['image'];  

        $country_map_city_places[0]['city_details'] = $country_map_city[0];

}else{      
    $country_map_city_places[0]['city_details'] = (object)[];

    
}


    $country_map_city_places[0]['avg_rating'] = "5.0";
            
            
             $tag_id = $country_map_city_places[0]['tag_id'];
             
             if($tag_id){
           $exp = explode(',',$tag_id);

     
       foreach($exp as $val1)

        {
            $id1 = $val1;
         $arr_get1 = "id = '$id1'";
             
    	 $fetch1 = $this->webservice_model->get_where('tags', $arr_get1);

	     $country_map_city_places[0]['tag_details'][]=$fetch1[0];

	    

        }
             }else{
                  $country_map_city_places[0]['tag_details']=[];
             }
             
             
             
                 
        
        
        
             $arr_whr = ['place_id'=>$place_id];	           

                         $country_map_city_places_images = $this->webservice_model->get_where('country_map_city_places_images', $arr_whr);
                         if($country_map_city_places_images){
                             foreach($country_map_city_places_images as $val_img)
                               {
                                  $val_img['image']=SITE_URL.'uploads/images/'.$val_img['image'];  

                                 $country_map_city_places[0]['places_images'][]=$val_img;
                               }
                                }else{
                                 $country_map_city_places[0]['places_images']=[];
                                            
                               }
                               
                               

         
        



	$fetch_rating_review = $this->webservice_model->get_where('rating_review', ['place_id'=>$place_id,'type'=>'Place']);
	
    if ($fetch_rating_review)
      {
       foreach($fetch_rating_review as $val1)

        {

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['user_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      
         $data_review[] = $val1;
        }
         $country_map_city_places[0]['rating_review'] =$data_review;

      }else{
         $country_map_city_places[0]['rating_review'] =[];
          
      }
      
      
         $ressult['result'] = $country_map_city_places[0];
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
         
          header('Content-type: application/json');
          echo json_encode($json);die;
      }
      else
      {
         $ressult['result'] = (object)[];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);die;
      }



    header('Content-type: application/json');
    echo json_encode($json);
    }
    
  
    /************* get_guidelines_tips function *************/


public

  function get_guidelines_tips()
    {
        
            $country_id = $this->input->get_post('country_id', TRUE) ;
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;

if($country_id){
    $fetch = $this->webservice_model->get_where('guidelines_tips',['country_id'=>$country_id]);
}else{
    $fetch = $this->webservice_model->get_all('guidelines_tips');
    
}
    if ($fetch)
      {
      foreach($fetch as $val)
        {
            
             $user_id = $this->input->get_post('user_id', TRUE) ;
            $map_city_id = $val['city_id'];
            $c_date = date('Y-m-d H:i:s');
     
            $fetch = $this->db->query("SELECT * FROM plan_purchase WHERE status = 'Complete' AND map_city_id = '$map_city_id' AND user_id = '$user_id' AND exp_date >= '$c_date'")->result_array();
if($fetch){
   
}else{
    continue;
    
}


          $val['image']=SITE_URL.'uploads/images/'.$val['image'];
          $ressult[] = $val;
        }


  if(isset($ressult)){
    $data['result'] = $ressult;
    $data['message'] = 'successfull';
    $data['status'] = '1';
    $json = $data;
  header('Content-type: application/json');
  echo json_encode($json);die;
  }else {
    $data['result'] = [];
    $data['message'] = 'data not found';
    $data['status'] = '0';
    $json = $data;
  header('Content-type: application/json');
  echo json_encode($json);die;
  }
  
       
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
 	
 	
 	
 	
    /************* get_company_offer function *************/


public

  function get_company_offer()
    {
        
            $country_id = $this->input->get_post('country_id', TRUE) ;
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;
            
            if($country_id){
                
                  $c_date = date('Y-m-d'); 


$fetch = $this->db->query("SELECT * FROM `company_offer` where  country_id = '$country_id' AND ( start_date <= '$c_date' AND end_date >='$c_date')")->result_array(); 


                       //$fetch = $this->db->query("SELECT * FROM company_offer WHERE country_id = '$country_id'")->result_array();
}else{
                       //$fetch = $this->db->query("SELECT * FROM company_offer  group by category_id")->result_array();
$fetch = $this->db->query("SELECT * FROM `company_offer` where  ( start_date <= '$c_date' AND end_date >='$c_date')  group by category_id ")->result_array(); 
    
}

    if ($fetch)
      {
      foreach($fetch as $val)
        {
            
            $cat_id = $val['category_id'];

    $fetch1 = $this->webservice_model->get_where('company_offer_category',['id'=>$cat_id]);
    
                $fetch2 = $this->webservice_model->get_where('company_offer',['category_id'=>$cat_id]);
                $fet=[];
 foreach($fetch2 as $val2)
        {
              $val2['image']=SITE_URL.'uploads/images/'.$val2['image'];
              $fet[] = $val2;
        }
         $fetch1[0]['company_offer'] = $fet;
         $data[] = $fetch1[0];
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
 	
 	
 	
 	
 	  /************* get_services function *************/


public

  function get_services()
    {
        
            $country_id = $this->input->get_post('country_id', TRUE) ;
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;
                  $c_date = date('Y-m-d'); 

if($country_id){
    $fetch = $this->db->query("SELECT * FROM `services` where  country_id = '$country_id' AND ( start_date <= '$c_date' AND end_date >='$c_date')")->result_array(); 

    //$fetch = $this->webservice_model->get_where('services',['country_id'=>$country_id]);
}else{
   // $fetch = $this->webservice_model->get_all('services');
        $fetch = $this->db->query("SELECT * FROM `services` where ( start_date <= '$c_date' AND end_date >='$c_date')")->result_array(); 

}
    if ($fetch)
      {
      foreach($fetch as $val)
        {
          $val['image1']=SITE_URL.'uploads/images/'.$val['image1'];
          $val['image2']=SITE_URL.'uploads/images/'.$val['image2'];
          $val['image3']=SITE_URL.'uploads/images/'.$val['image3'];
          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
    	
	/************* get_service_details function *************/

     public function get_service_details(){

      $user_id = $this->input->get_post('user_id');
      $service_id = $this->input->get_post('service_id');
      $arr_get = ['id'=>$this->input->get_post('service_id')];

      $login = $this->webservice_model->get_where('services',$arr_get);

      if ($login) {  
     
          $login[0]['image1']=SITE_URL.'uploads/images/'.$login[0]['image1'];
          $login[0]['image2']=SITE_URL.'uploads/images/'.$login[0]['image2'];
          $login[0]['image3']=SITE_URL.'uploads/images/'.$login[0]['image3'];
         
         $fetch_rating_review = $this->webservice_model->get_where('service_rating_review',['service_id'=>$service_id]);
	
    if ($fetch_rating_review)
      {$data_review = [];
       foreach($fetch_rating_review as $val1)

        {

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['user_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      
         $data_review[] = $val1;
        }
         $login[0]['rating_review'] =$data_review;

      }else{
         $login[0]['rating_review'] =[];
          
      }
         
          $ressult['result']=$login[0];
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;

      }else{

        $json = ['result'=>(object)[],'status'=>'0','message'=>'Data Not Found'];

      }

      header('Content-type: application/json');
      echo json_encode($json);
    }      



 	
 	  /*************  get_notification_list *************/
    public

    function get_notification_list()
    {                     
                   $user_id = $this->input->get_post('user_id', TRUE); 

                    $this->db->order_by("id", "desc");
     
      $arr_get = ['type'=>'USER'];
                           // $login = $this->db->query("SELECT * FROM notification where type = 'USER' AND (user_id = '$user_id' OR user_id = '') order by id desc ")->result_array();
           $login = $this->db->query("SELECT * FROM notification WHERE FIND_IN_SET('$user_id', `user_id`) ORDER BY id desc")->result_array();

      $login = $this->webservice_model->get_where('notification',$arr_get);

               
      if ($login) {
          
          
        
  $this->db->query("UPDATE `notification` SET `seen_status`='SEEN' WHERE `user_id` = '$user_id' ");
                

                         
                         foreach($login as  $val)
       {   
                              
                                  
                           $data[] = $val;

                          }
        

                          $ressult['result']=$data;
                          $ressult['message']='successful';
                          $ressult['status']='1';
                          $json = $ressult;                      
                         

      }
      else {

                          $ressult['result']=[];
                          $ressult['message']='unsuccessful';
                          $ressult['status']='0';
                          $json = $ressult;
      }

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

    }
    
 	
 	
     /***************add_user_trip_schedule*****************/
public

function add_user_trip_schedule()
	{
	    
	    
        

 $date_time = date('Y-m-d H:i:s');     

	 $arr_product = array(
		'user_id' => $this->input->get_post('user_id', TRUE) ,
		'day_id' => $this->input->get_post('day_id', TRUE) ,
		'table_map_name' => $this->input->get_post('table_map_name', TRUE) ,
		'map_name' => $this->input->get_post('map_name', TRUE) ,
		'day_name' => $this->input->get_post('day_name', TRUE) ,
		'day_name_ar' => $this->input->get_post('day_name_ar', TRUE) ,
		'country_id' => $this->input->get_post('country_id', TRUE) ,
		'country_name' => $this->input->get_post('country_name', TRUE) ,
		'country_name_ar' => $this->input->get_post('country_name_ar', TRUE) ,
		'place_id' => $this->input->get_post('place_id', TRUE) ,
		'trip_name' => $this->input->get_post('trip_name', TRUE) ,
		'trip_place_id' => $this->input->get_post('trip_place_id', TRUE) ,
		'map_place_id' => $this->input->get_post('map_place_id', TRUE) ,
		'trip_name_ar' => $this->input->get_post('trip_name_ar', TRUE) ,
		'map_type' => $this->input->get_post('map_type', TRUE) ,
		'map_type_ar' => $this->input->get_post('map_type_ar', TRUE) ,
		'address' => $this->input->get_post('address', TRUE) ,
		'lat' => $this->input->get_post('lat', TRUE) ,
		'lon' => $this->input->get_post('lon', TRUE) ,
		'how_much_day' => $this->input->get_post('how_much_day', TRUE) ,
		'trip_by_cineramap' => $this->input->get_post('trip_by_cineramap', TRUE) ,
	);

	
    
                $id = $this->webservice_model->insert_data('user_trip_schedule',$arr_product);

                if($id=="") {
                    $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];
                }else{
                     
                     
                           $login = $this->webservice_model->get_where('user_trip_schedule',['id'=>$id]);

  
                $ressult['result']=$login[0];
                $ressult['message']='successfull';
                $ressult['status']='1';
                $json = $ressult;
                header('Content-type: application/json');
	        echo json_encode($json);
	        die;

           

               }           
	 

	        header('Content-type: application/json');
	        echo json_encode($json);
	        die;
   }



/************* update_user_trip_schedule function *************/

    public function update_user_trip_schedule(){

               
               
      $arr_get = ['id'=>$this->input->get_post('trip_id')];

      $login = $this->webservice_model->get_where('user_trip_schedule',$arr_get);
      if ($login[0]['id'] == "")
      {
                          $ressult['result']=(object)[];
                          $ressult['message']='unsuccessfull';
                          $ressult['status']='0';
                          $json = $ressult;

                                header('Content-type:application/json');
                                echo json_encode($json);
                                die;
      }



 $date_time = date('Y-m-d H:i:s');     

     $arr_product = array(
	
		'date_time' => $date_time,
		'day_id' => $this->input->get_post('day_id', TRUE) ,
		'day_name' => $this->input->get_post('day_name', TRUE) ,
		'day_name_ar' => $this->input->get_post('day_name_ar', TRUE) ,
		'table_map_name' => $this->input->get_post('table_map_name', TRUE) ,
		'country_id' => $this->input->get_post('country_id', TRUE) ,
		'country_name' => $this->input->get_post('country_name', TRUE) ,
		'country_name_ar' => $this->input->get_post('country_name_ar', TRUE) ,
		'place_id' => $this->input->get_post('place_id', TRUE) ,
		'trip_name' => $this->input->get_post('trip_name', TRUE) ,
		'trip_place_id' => $this->input->get_post('trip_place_id', TRUE) ,
		'map_place_id' => $this->input->get_post('map_place_id', TRUE) ,
		'trip_name_ar' => $this->input->get_post('trip_name_ar', TRUE) ,
		'map_type' => $this->input->get_post('map_type', TRUE) ,
		'map_type_ar' => $this->input->get_post('map_type_ar', TRUE) ,
		'address' => $this->input->get_post('address', TRUE) ,
		'lat' => $this->input->get_post('lat', TRUE) ,
		'lon' => $this->input->get_post('lon', TRUE) ,
		'how_much_day' => $this->input->get_post('how_much_day', TRUE) ,
		'trip_by_cineramap' => $this->input->get_post('trip_by_cineramap', TRUE) ,
	);

                    

        

      $res = $this->webservice_model->update_data('user_trip_schedule',$arr_product,$arr_get);
      if ($res)
      {
        $data = $this->webservice_model->get_where('user_trip_schedule',$arr_get);


        $ressult['result']=$data[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }
      else
      {
                          $ressult['result']=(object)[];
                          $ressult['message']='unsuccessfull';
                          $ressult['status']='0';
                          $json = $ressult;
      }

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

                          

    }

 	
/************* get_user_trip_schedule_details function *************/

    public function get_user_trip_schedule_details(){

               
               
      $arr_get = ['id'=>$this->input->get_post('trip_id')];

      $login = $this->webservice_model->get_where('user_trip_schedule',$arr_get);
      if ($login[0]['id'] == "")
      {
                          $ressult['result']=(object)[];
                          $ressult['message']='unsuccessfull';
                          $ressult['status']='0';
                          $json = $ressult;

                                header('Content-type:application/json');
                                echo json_encode($json);
                                die;
      }




        $ressult['result']=$login[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
   

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

                          

    }

 	
 	
 	
 	  /************* get_user_trip_schedule_map_name function *************/


public

  function get_user_trip_schedule_map_name()
    {
        
            $country_id = $this->input->get_post('country_id', TRUE) ;
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;

            $fetch = $this->db->query("SELECT * FROM user_trip_schedule where user_id = '$user_id' AND place_id = '$city_id' AND map_name !='' ")->result_array();

  //  $fetch = $this->webservice_model->get_where('user_trip_schedule',['user_id'=>$user_id]);

    if ($fetch)
      {
      foreach($fetch as $val)
        {
             $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
 	  /************* get_user_trip_schedule function *************/


public

  function get_user_trip_schedule()
    {
        
            $country_id = $this->input->get_post('country_id', TRUE) ;
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;

            $fetch = $this->db->query("SELECT * FROM user_trip_schedule where user_id = '$user_id'  AND map_name !='' ")->result_array();

  //  $fetch = $this->webservice_model->get_where('user_trip_schedule',['user_id'=>$user_id]);

    if ($fetch)
      {
      foreach($fetch as $val)
        {
             $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
    
      /************* get_user_trip_schedule_by_day function *************/


public

  function get_user_trip_schedule_by_day()
    {
        
            $lat = $this->input->get_post('lat', TRUE) ;
            $lon = $this->input->get_post('lon', TRUE) ;
            $city_id = $this->input->get_post('city_id', TRUE) ;
            $user_id = $this->input->get_post('user_id', TRUE) ;
            $table_map_name = $this->input->get_post('table_map_name', TRUE) ;


    $fetch = $this->webservice_model->get_all('days');

    if ($fetch)
      {
      foreach($fetch as $val)
        {
             $day_id = $val['id'];
             $fetch_user_trip_schedule = $this->db->query("SELECT * FROM user_trip_schedule where user_id = '$user_id' AND place_id = '$city_id' AND  day_id = '$day_id' AND  table_map_name = '$table_map_name'")->result_array();
            if($fetch_user_trip_schedule){
             foreach($fetch_user_trip_schedule as $val_p)
                    {
                        if($val_p['trip_name'] == '' && $val_p['trip_name_ar'] ==''){
                             continue;
                        }
                         if($val_p['lat']){
                   $lat1 = $val_p['lat'];

                   $lon1 = $val_p['lon'];
               }else{
                   $lat1 = '22.22';

                   $lon1 = '75.55';
                   
               }
                          $distance = $this->webservice_model->distance($lat, $lon, $lat1, $lon1, $miles = false);

               
                          $val_p['distance']=number_format($distance,2);
                          $val_p['time']=number_format($distance * 1.5,2);
                              $rating = number_format($rating, 1, '.', '');

                          $minutes =number_format($distance * 1.5, 1, '.', '');

        
$zero    = new DateTime('@0');
$offset  = new DateTime('@' . $minutes * 60);
$diff    = $zero->diff($offset);

$val_p['time'] = $diff->format('%ad,%hh,%im');

                        $val['day_wise_trip'][]=$val_p;
                    }
            }else{
                
                continue;
                        $val['day_wise_trip']=[];
                
            }
            
             $ressult[] = $val;
        }

     if(isset($ressult)){
    $data['result'] = $ressult;
    $data['message'] = 'successfull';
    $data['status'] = '1';
    $json = $data;
  header('Content-type: application/json');
  echo json_encode($json);die;
  }else {
    $data['result'] = [];
    $data['message'] = 'data not found';
    $data['status'] = '0';
    $json = $data;
  header('Content-type: application/json');
  echo json_encode($json);die;
  }
        
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
                 /************* delete_user_trip_schedule *************/
    public

    function delete_user_trip_schedule()
    {
               
                           $trip_id = $this->input->get_post('trip_id', TRUE) ;


      $arr_get = ['id'=>$this->input->get_post('trip_id')];

      $login = $this->webservice_model->get_where('user_trip_schedule',$arr_get);

      if ($login)
      {
                   $this->webservice_model->delete_data('user_trip_schedule',['id'=>$trip_id]);

                          $ressult['result']="delete successfull";
                          $ressult['message']='delete successfull';
                          $ressult['status']='1';
                          $json = $ressult;
      }
        else
      {
                          $ressult['result']='unsuccessful';
                          $ressult['message']='unsuccessful';
                          $ressult['status']='0';
                          $json = $ressult;                              
        
      }

        
                     
      header('Content-type: application/json');
      echo json_encode($json);
    }

      /************* delete_user_trip_schedule_by_city *************/
    public

    function delete_user_trip_schedule_by_city()
    {
               
                           $user_id = $this->input->get_post('user_id', TRUE) ;
                           $city_id = $this->input->get_post('city_id', TRUE) ;


      $arr_get = ['user_id'=>$this->input->get_post('user_id'),'place_id'=>$this->input->get_post('city_id')];

      $login = $this->webservice_model->get_where('user_trip_schedule',$arr_get);

      if ($login)
      {
                   $this->webservice_model->delete_data('user_trip_schedule',$arr_get);

                          $ressult['result']="delete successfull";
                          $ressult['message']='delete successfull';
                          $ressult['status']='1';
                          $json = $ressult;
      }
        else
      {
                          $ressult['result']='unsuccessful';
                          $ressult['message']='unsuccessful';
                          $ressult['status']='0';
                          $json = $ressult;                              
        
      }

        
                     
      header('Content-type: application/json');
      echo json_encode($json);
    }
    
/************* get_country_map function *************/
   
   
public

  function get_country_map_by_user()
    {
        
            $user_id = $this->input->get_post('user_id', TRUE) ;


    $fetch = $this->webservice_model->get_all('country_map');
    if ($fetch)
      {
      foreach($fetch as $val)
        {
                                   $val['country_icon']=SITE_URL.'uploads/images/'.$val['country_icon'];  
                                   $val['image']=SITE_URL.'uploads/images/'.$val['image'];  

          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
 /************* fav_unfav_citymap function *************/

    public function fav_unfav_citymap(){

         $arr_data = [
            'user_id'=>$this->input->get_post('user_id'),
            'city_id'=>$this->input->get_post('city_id')
            ];
      
    
      $arr_get = ['user_id' => $arr_data['user_id'],'city_id' => $arr_data['city_id']];

      $login = $this->webservice_model->get_where('unfav_citymap',$arr_get);
      if ($login) {
          
            $id = $this->webservice_model->delete_data('unfav_citymap',$arr_get);

        
        $ressult['result']='Remove to favourite list successfull';
        $ressult['message']='Remove to favourite list successfull';
        $ressult['status']='1';
        $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
      }     


     

      $id = $this->webservice_model->insert_data('unfav_citymap',$arr_data);

      if ($id=="") {
        $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];
      }else{
                              
        $arr_gets = ['id'=>$id];
        $login = $this->webservice_model->get_where('unfav_citymap',$arr_gets); 
        
        $ressult['result']='Add to favourite list successfull';
        $ressult['message']='Add to favourite list successfull';
        $ressult['status']='1';
        $json = $ressult;
      }

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

    }


	/*************  my_fav_citymap function *************/
 public function my_fav_citymap()
	{
	    
	     $user_id  = $this->input->get_post('user_id');  
	  

	        	    $rest_list = $this->webservice_model->get_where('unfav_citymap',['user_id'=>$user_id]);

		if ($rest_list)
		{
			foreach($rest_list as $val)
			{             
			    
			      
       
			                        $product_id = $val['city_id'];
               
           
               
                                    $like_check = $this->webservice_model->get_where('country_map_city',['id'=>$product_id]);
                                   
                                     $like_check[0]['fav_status'] = "Yes";
                                     	 $city_id =  $val['city_id'];     
         $get = $this->db->select_avg("rating", "rating")->where(['city_id'=>$city_id])->get('rating_review')->result_array();

         $rating = ($get[0]['rating']=='') ?  0 : $get[0]['rating'];   

         
          $like_check[0]['avg_rating'] = number_format($rating,2);


                                     $like_check[0]['image']=SITE_URL.'uploads/images/'.$like_check[0]['image'];

  
  
  
       $user_id = $this->input->get_post('user_id', TRUE) ;
            $map_city_id = $val['city_id'];
            $c_date = date('Y-m-d H:i:s');
     
            $fetch = $this->db->query("SELECT * FROM plan_purchase WHERE status = 'Complete' AND map_city_id = '$map_city_id' AND user_id = '$user_id' AND exp_date >= '$c_date'")->result_array();
if($fetch){
    $like_check[0]['subscription_status'] = 'Yes';
}else{
    $like_check[0]['subscription_status'] = 'No';
    
}

			                         $data[] = $like_check[0];
			    }
		       
		$json = ['result'=>$data,'message'=>'successfull','status'=>'1']; 
		}
		else{
        			$json = ['result'=>[],'message'=>'unsuccessfull','status'=>'0']; 
        		}
	  
		header('Content-type: application/json');
		echo json_encode($json);
	}
	
	
	    
    /************* add_city_rating_review function *************/

    public function add_city_rating_review(){

$date_time = date('Y-m-d H:i:s');
         $arr_data = [
            'user_id'=>$this->input->get_post('user_id'),
            'city_id'=>$this->input->get_post('city_id'),
            'rating'=>$this->input->get_post('rating'),
            'place_id'=>$this->input->get_post('place_id'),
            'type'=>$this->input->get_post('type'),
            'date_time'=>$date_time,
            'review'=>$this->input->get_post('review')
            ];
      
        $arr_gets = ['user_id'=>$this->input->get_post('user_id'),'city_id'=>$this->input->get_post('city_id')];
        $login = $this->webservice_model->get_where('rating_review',$arr_gets); 
        
      
      /*if ($login) {
        
        $ressult['result']=(object)[];
        $ressult['message']='already provided rating review';
        $ressult['status']='0';
        $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
      }     */


      $id = $this->webservice_model->insert_data('rating_review',$arr_data);

      if ($id=="") {
        $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];
      }else{
                              
        $arr_gets = ['id'=>$id];
        $login = $this->webservice_model->get_where('rating_review',$arr_gets); 
        
        $product_id = $login[0]['id'];
        
        
      
        $ressult['result']=$login[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }

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

    }
 
 
   
     	
 /*************  addPayment_moyasar *************/
    public

    function addPayment_moyasar()
    {           
              

      $user_id = $this->input->get_post('user_id');
      
      $total_amount = $this->input->get_post('total_amount');
      $total_amount = $total_amount*100;
      $callback_url = $this->input->get_post('callback_url');
      $currency = 'SAR';
      $type = $this->input->get_post('type');
      $name = $this->input->get_post('name');
      $number = $this->input->get_post('number');
      $year = $this->input->get_post('year');
      $month = $this->input->get_post('month');
      $cvc = $this->input->get_post('cvc');



$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.moyasar.com/v1/payments');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n    \"amount\": $total_amount,\n    \"currency\": \"SAR\",\n    \"description\": \"Payment for order #\",\n    \"callback_url\": \"https://example.com/thankyou\",\n    \"source\": {\n        \"type\": \"creditcard\",\n        \"name\": \"$name\",\n        \"number\": \"$number\",\n        \"cvc\": \"$cvc\",\n        \"month\": \"$month\",\n        \"year\": \"$year\"\n    }\n}");
curl_setopt($ch, CURLOPT_USERPWD, 'pk_live_3X8YbEs6HVuDixtcsxE73G22u6UkMpR9ziAR8qnD' . ':' . '');
//curl_setopt($ch, CURLOPT_USERPWD, 'pk_test_XEheLuyaUAgt35ZQ84B6ufL79fJpa5hMNcrCzWak' . ':' . '');

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


      
       $response = curl_exec($ch);  
       $response = json_decode($response);
      // print_r($response);
      
      $status_payment = $response->status;
       if($status_payment == 'failed'){
                 $message_payment = $response->source->message;

          $ressult['result']=$response;
          $ressult['message']=$response;
          $ressult['status']='0';
          $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);
          die;
    }
    
    
       if (isset($response->type)) {
                            $message_payment = $response->message;

          $ressult['result']=$response;
          $ressult['message']=$response;
          $ressult['status']='0';
          $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);
          die;
       }
       
        
       curl_close ($ch);
                              $ressult['result']=$response;
                               $ressult['message']=$response;
                               $ressult['status']='0';
                               $json = $ressult;
                               header('Content-type:application/json');
                               echo json_encode($json);die;
 
      header('Content-type: application/json');
      echo json_encode($json);die;
      
    
    }
    
    
    
	  /************* plan_purchase  function *************/

    public function plan_purchase(){

 $plan_id = $this->input->get_post('plan_id', TRUE);
                $login_plan = $this->webservice_model->get_where('country_map_city',['id'=>$plan_id]);   

  $c_date = date('Y-m-d H:i:s');
  
  
  $month = $login_plan[0]['city_map_month'];
  
  
 $exp_date = date('Y-m-d H:i:s', strtotime("+$month months"));

 
       $arr_data = [
            'user_id'=>$this->input->get_post('user_id'),
            'map_country_id'=>$this->input->get_post('map_country_id'),
            'map_city_id'=>$this->input->get_post('map_city_id'),
            'plan_id'=>$this->input->get_post('plan_id'),
            'transaction_id'=>$this->input->get_post('transaction_id'),
            'exp_date'=>$exp_date,
            'amount'=>$this->input->get_post('amount')
       ];
       


                $id = $this->webservice_model->insert_data('plan_purchase',$arr_data);


      if ($id=="") {

                $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];

      }else{


                $arr_gets = ['id' => $id];
                     
        
                $login = $this->webservice_model->get_where('plan_purchase',$arr_gets);   
       
                
                $ressult['result']=$login[0];
                $ressult['message']='successfull';
                $ressult['status']='1';
                $json = $ressult;
      }

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

    }
    
   	/*************  get_my_purchase_plan function *************/
 public function get_my_purchase_plan()
	{
	    $c_date = date('Y-m-d H:i:s');
	     $user_id  = $this->input->get_post('user_id');  
	  
           $rest_list = $this->db->query("SELECT * FROM plan_purchase WHERE user_id = '$user_id' AND exp_date >= '$c_date'")->result_array();


		if ($rest_list)
		{
			foreach($rest_list as $val)
			{             
			    
			      
       
			                        $map_country_id = $val['map_country_id'];
               
                                    $like_check = $this->webservice_model->get_where('country_map',['id'=>$map_country_id]);
                                   
                                 if($like_check){

                                     $like_check[0]['image']=SITE_URL.'uploads/images/'.$like_check[0]['image'];
                                     $like_check[0]['country_icon']=SITE_URL.'uploads/images/'.$like_check[0]['country_icon'];
                                         $val['country_map'] = $like_check[0];
                                     }else{
                                         $val['country_map'] = (object)[];
                                     }
  
			                         $data[] = $val;
			    }
		       
		$json = ['result'=>$data,'message'=>'successfull','status'=>'1']; 
		}
		else{
        			$json = ['result'=>[],'message'=>'unsuccessfull','status'=>'0']; 
        		}
	  
		header('Content-type: application/json');
		echo json_encode($json);
	}
	
   
   
    	
/************* check_plan_purchase_city_wise function *************/
   
   
public

  function check_plan_purchase_city_wise()
    {
        
            $user_id = $this->input->get_post('user_id', TRUE) ;
            $map_city_id = $this->input->get_post('map_city_id', TRUE) ;
            $c_date = date('Y-m-d H:i:s');
     
            $fetch = $this->db->query("SELECT * FROM plan_purchase WHERE status = 'Complete' AND map_city_id = '$map_city_id' AND user_id = '$user_id' AND exp_date >= '$c_date'")->result_array();

    if ($fetch)
      {
                                         $like_check = $this->webservice_model->get_where('country_map_city',['id'=>$fetch[0]['plan_id']]);


         $ressult['result'] = $like_check[0];
         $ressult['message'] = 'successful';
         $ressult['plan_purcahse_status'] = 'Yes';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = (object)[];
         $ressult['message'] = 'successful';
         $ressult['plan_purcahse_status'] = 'No';
         $ressult['status'] = '1';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
     /***************add_country_map_suggestion*****************/
public

function add_country_map_suggestion()
	{
	    
	    
        

 $date_time = date('Y-m-d H:i:s');     

	 $arr_product = array(
		'user_id' => $this->input->get_post('user_id', TRUE) ,
		'country_name' => $this->input->get_post('country_name', TRUE) ,
		'description' => $this->input->get_post('description', TRUE) ,
	);

	
    
                $id = $this->webservice_model->insert_data('country_map_suggestion',$arr_product);

                if($id=="") {
                    $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];
                }else{
                     
                     
                           $login = $this->webservice_model->get_where('country_map_suggestion',['id'=>$id]);

  
                $ressult['result']=$login[0];
                $ressult['message']='successfull';
                $ressult['status']='1';
                $json = $ressult;
                header('Content-type: application/json');
	        echo json_encode($json);
	        die;

           

               }           
	 

	        header('Content-type: application/json');
	        echo json_encode($json);
	        die;
   }
   
      
 /************* fav_unfav_place function *************/

    public function fav_unfav_place(){

         $arr_data = [
            'user_id'=>$this->input->get_post('user_id'),
            'place_id'=>$this->input->get_post('place_id'),
            'status'=>$this->input->get_post('status')
            ];
      
    $status = $this->input->get_post('status', TRUE);
      $arr_get = ['user_id' => $arr_data['user_id'],'place_id' => $arr_data['place_id']];

      $login = $this->webservice_model->get_where('unfav_place',$arr_get);
      if ($login) {
         
      $id = $this->webservice_model->delete_data('unfav_place',$arr_get);

      $id = $this->webservice_model->insert_data('unfav_place',$arr_data);

 if($status == ''){
                          $id = $this->webservice_model->delete_data('unfav_place',$arr_get);

          }
          
        
        $ressult['result']='Remove to favourite list successfull';
        $ressult['message']='Remove to favourite list successfull';
        $ressult['status']='1';
        $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
      }     


     

      $id = $this->webservice_model->insert_data('unfav_place',$arr_data);

      if ($id=="") {
        $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];
      }else{
                              
        $arr_gets = ['id'=>$id];
        $login = $this->webservice_model->get_where('unfav_place',$arr_gets); 
        
        $ressult['result']='Add to favourite list successfull';
        $ressult['message']='Add to favourite list successfull';
        $ressult['status']='1';
        $json = $ressult;
      }

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

    }

	
/************* get_purcahse_city_map_list function *************/
   
   
public

  function get_purcahse_city_map_list()
    {
        
            $user_id = $this->input->get_post('user_id', TRUE) ;
            $type = $this->input->get_post('type', TRUE) ;
              $c_date = date('Y-m-d H:i:s');

    
    $fetch = $this->db->query("SELECT * FROM country_map_city WHERE remove_status = 'No' AND  id IN(select map_city_id from plan_purchase where status = 'Complete' AND  user_id = '$user_id' AND exp_date >= '$c_date') order by id desc")->result_array();


    if ($fetch)
      {
      foreach($fetch as $val)
        {
            
              $city_id = $val['id'];
              $country_id = $val['country_id'];
               $fetch_country_map = $this->webservice_model->get_where('country_map',['id'=>$country_id]);
if($fetch_country_map){
    
                $fetch_country_map[0]['country_icon']=SITE_URL.'uploads/images/'.$fetch_country_map[0]['country_icon'];  
                $fetch_country_map[0]['image']=SITE_URL.'uploads/images/'.$fetch_country_map[0]['image'];  
                $val['country_details'] = $fetch_country_map[0];
}else{
                $val['country_details'] = (object)[];

    
}

  if($user_id){
                    $city_id = $val['id'];
		     	    $rest_like_check = $this->webservice_model->get_where('unfav_citymap',['user_id'=>$user_id,'city_id'=>$city_id]);
                       if($rest_like_check){
                           			    $val['fav_status']='Yes';  
                
                       }else{
                           			    $val['fav_status']='No';  
                
                       }
                       
        }else{
                           			    $val['fav_status']='No';  
            
        }
        
            $val['avg_rating'] = "5.0";
            
                            $val['image']=SITE_URL.'uploads/images/'.$val['image'];  


          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
     
    /************* cancel_plan_purchase function *************/
    public function cancel_plan_purchase()
    {
      $user_id = $this->input->get_post('user_id', TRUE);
      $map_country_id = $this->input->get_post('map_country_id', TRUE);
      $map_city_id = $this->input->get_post('map_city_id', TRUE);


      $arr_login = ['user_id' => $user_id ,'map_country_id' => $map_country_id,'map_city_id' => $map_city_id];
      $login = $this->webservice_model->get_where('plan_purchase', $arr_login);
 
      if ($login)
      {     
          $this->webservice_model->update_data('plan_purchase',['status'=>'Cancel'],$arr_login);
            
          $ressult['result']='successfull';
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;

        
      }
      else
      {
          $ressult['result']='unsuccessfull';
          $ressult['message']='unsuccessfull';
          $ressult['status']='0';
          $json = $ressult;
      }

      header('Content-type: application/json');
      echo json_encode($json);
    }
    
    
      
         
/************* get_banner function *************/
   
   
public

  function get_banner()
    {
       
        $c_date = date('Y-m-d'); 


$fetch = $this->db->query("SELECT * FROM `banner` where ( start_date <= '$c_date' AND end_date >='$c_date')")->result_array(); 


    //$fetch = $this->webservice_model->get_all('banner');
    if ($fetch)
      {
      foreach($fetch as $val)
        {
            
          $val['image']=SITE_URL.'uploads/images/'.$val['image'];
          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
      
    /************* add_service_rating_review function *************/

    public function add_service_rating_review(){

$date_time = date('Y-m-d H:i:s');
         $arr_data = [
            'user_id'=>$this->input->get_post('user_id'),
            'service_id'=>$this->input->get_post('service_id'),
            'rating'=>$this->input->get_post('rating'),
            'date_time'=>$date_time,
            'review'=>$this->input->get_post('review')
            ];
      
        $arr_gets = ['user_id'=>$this->input->get_post('user_id'),'service_id'=>$this->input->get_post('service_id')];
        $login = $this->webservice_model->get_where('service_rating_review',$arr_gets); 
        
      
      if ($login) {
        
        $ressult['result']=(object)[];
        $ressult['message']='already provided rating review';
        $ressult['status']='0';
        $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
      }     


      $id = $this->webservice_model->insert_data('service_rating_review',$arr_data);

      if ($id=="") {
        $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];
      }else{
                              
        $arr_gets = ['id'=>$id];
        $login = $this->webservice_model->get_where('service_rating_review',$arr_gets); 
        
        $product_id = $login[0]['id'];
        
        $ressult['result']=$login[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }

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

    }
 
 
 
    

     /************* add_product function *************/

    public function add_place_by_user(){

 

         $arr_data = [
            'user_id'=>$this->input->get_post('user_id'),
            'country_id'=>$this->input->get_post('country_id'),
            'city_id'=>$this->input->get_post('city_id'),
            'tag_id'=>$this->input->get_post('tag_id'),
            'place_name'=>$this->input->get_post('place_name'),
            'place_name_ar'=>$this->input->get_post('place_name_ar'),
            'description'=>$this->input->get_post('description'),
            'description_ar'=>$this->input->get_post('description_ar'),
            'address'=>$this->input->get_post('address'),
            'lat'=>$this->input->get_post('lat'),
            'lon'=>$this->input->get_post('lon'),
            'icon_background_color'=>$this->input->get_post('icon_background_color'),
            'promo_code_and_discount'=>$this->input->get_post('promo_code_and_discount'),
            'mobile'=>$this->input->get_post('mobile'),
            'email'=>$this->input->get_post('email'),
            'site_url'=>$this->input->get_post('site_url'),
            'video_link_en	'=>$this->input->get_post('video_link_en	'),
            'video_link_ar'=>$this->input->get_post('video_link_ar'),
            'google_map_link'=>$this->input->get_post('google_map_link'),
            ];
      
 


      $id = $this->webservice_model->insert_data('country_map_city_places',$arr_data);

      if ($id=="") {
        $json = ['result'=>(object)[],'status'=>0,'message'=>'data not found'];
      }else{
                              
        $arr_gets = ['id'=>$id];
        $login = $this->webservice_model->get_where('country_map_city_places',$arr_gets); 
        
        $product_id = $login[0]['id'];
        
        
          if (isset($_FILES['places_images']))
        {


               $product_images = $_FILES['places_images']['name'];
 
               $i=0;
               foreach($product_images AS $name){
               
                 $n = rand(0, 100000);
                 $ext = "png";
                 $img = "PRODUCT_images_" . date('Ymdhis') . '_' . $n . '.'.$ext;
                 move_uploaded_file($_FILES['places_images']['tmp_name'][$i], "uploads/images/" . $img);
               
                 $img_data = ['place_id'=>$product_id,'image'=>$img];
                 $this->webservice_model->insert_data('country_map_city_places_images',$img_data);
                 $i++;

               }
        
        }
                       
      
        $ressult['result']=$login[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }

      header('Content-type:application/json');
      echo json_encode($json);die;

    }
   
   
/************* get_days function *************/
   
   
public

  function get_days()
    {
        
           
    $fetch = $this->webservice_model->get_all('days');
    if ($fetch)
      {
      foreach($fetch as $val)
        {
              $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
   
         
/************* get_coupons function *************/
   
   
public

  function get_coupons()
    {
        
            $provider_id = $this->input->get_post('provider_id', TRUE) ;


    $fetch = $this->webservice_model->get_all('coupons',['status'=>'Active']);
    if ($fetch)
      {
      foreach($fetch as $val)
        {
           $val['image']=SITE_URL.'uploads/images/'.$val['image'];
           $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
      /************* apply_offer function *************/

    public function apply_offer(){

       $offer_code = $this->input->get_post('offer_code');
       $user_id = $this->input->get_post('user_id');
       $amount = $this->input->get_post('amount');
       $cu_date = date("Y-m-d");
      $arr_get = ['code' => $offer_code];
                    $this->db->order_by("id", "desc");

      $login = $this->webservice_model->get_where('coupons',$arr_get);
      if ($login) {
               
          
          $end_date =$login[0]['end_date'];
          $percentage =$login[0]['percentage'];
        if($cu_date <= $end_date){
            
            $discount = ($amount * $percentage)/100;
            $after_discount = $amount - $discount;
            $discount_amount = $discount;
            
            $ressult['total_amount']=$amount;
            $ressult['discount_amount']=$discount_amount;
            $ressult['after_discount']=$after_discount;
            $ressult['offer_id']=$login[0]['id'];
            $ressult['result']='applied successfull';
            $ressult['message']='successfull';
            $ressult['status']='1';
            $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
            
        }else{
            $ressult['result']='Code is expired ';
            $ressult['message']='unsuccessfull';
            $ressult['status']='0';
            $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
        }
        
      }else{
          $ressult['result']='Entered wrong code';
        $ressult['message']='unsuccessfull';
        $ressult['status']='0';
        $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
          
      }     


    }



/////////////////////////// cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

  



     /************* login function *************/


    public function login(){

                 /* $token = $this->input->get_post('token', TRUE);
               if($token){
                   
                    $where_token = "token = '$token'";
    

     

      $login_token = $this->webservice_model->get_where('tokens',$where_token);
      if ($login_token) {



               }else{
                   
                                $ressult['result']=(object)[];
                                $ressult['message']='Wrong authorization';
                                $ressult['status']='0';
                                $json = $ressult;  
                                
                                  header('Content-type:application/json');
                                  echo json_encode($json);die;
               } 
               
               }else{
                   
                                $ressult['result']=(object)[];
                                $ressult['message']='Wrong authorization';
                                $ressult['status']='0';
                                $json = $ressult;  
                                
                                  header('Content-type:application/json');
                                  echo json_encode($json);die;
               } */
                
                
                
                $email = $this->input->get_post('email', TRUE);


                $email = $this->input->get_post('email', TRUE);
                $mobile = $this->input->get_post('mobile', TRUE);
                $password = $this->input->get_post('password', TRUE);
                $register_id = $this->input->get_post('register_id');
                $type = $this->input->get_post('type');
                $ios_register_id= $this->input->get_post('ios_register_id');
                
                $lat = $this->input->get_post('lat', TRUE);
                $lon = $this->input->get_post('lon', TRUE);

              $password = $this->input->get_post('password', TRUE);
              $password = base64_encode($password);


    $where = "email = '$email' AND password = '$password' ";
    

     

      $login = $this->webservice_model->get_where('users',$where);
      if ($login) {



 $wherech_r_id = "register_id = '$register_id' AND type = 'USER'";
		
      $login_ch_r_id = $this->webservice_model->get_where('users',$wherech_r_id);
                   if($login_ch_r_id){
                       
                     $this->webservice_model->update_data('users',['register_id'=>''],$wherech_r_id);

                   }
                   
                   /*
                     $wherech_i_id = "ios_register_id = '$ios_register_id' AND type = 'USER'";
		
      $login_ch_i_id = $this->webservice_model->get_where('users',$wherech_i_id);
                   if($login_ch_i_id){
                       
                     $this->webservice_model->update_data('users',['ios_register_id'=>''],$wherech_i_id);

                   }*/
                   
                   

                      if($register_id) { 
    
                       $arrr_get =   array(
                                     'register_id'=>$register_id,
                                     'lat'=>$lat,
                                     'lon'=>$lon
                                );
                  

                               $this->webservice_model->update_data('users',$arrr_get,$where);
                            }

                       if($ios_register_id) {  

                       $arrr_get1 =   array(
                                     'ios_register_id'=>$ios_register_id,
                                     'lat'=>$lat,
                                     'lon'=>$lon
                                );
                  
                               $this->webservice_model->update_data('users',$arrr_get1,$where);
                            }



                               $login3 = $this->webservice_model->get_where('users',$where);
                               $login3[0]['image']=SITE_URL.'uploads/images/'.$login3[0]['image'];
                               $login3[0]['provider_logo']=SITE_URL.'uploads/images/'.$login3[0]['provider_logo'];
                               
  $userId = $login3[0]['id'];
  $tktToken = $this->generateToken($userId);

  $login_token  = $this->webservice_model->get_where('tokens',['user_id'=>$userId]); 
  
  $login3[0]['token'] = $login_token[0]['authorization_token'];
  
                               $ressult['result']=$login3[0];
                               $ressult['message']='successfull';
                               $ressult['status']='1';
                               $json = $ressult;
                               header('Content-type:application/json');
                               echo json_encode($json);die;
                   

                 }else{
                                $ressult['result']=(object)[];
                                $ressult['message']='You have entered wrong email';
                                $ressult['status']='0';
                                $json = $ressult;       
      }

      header('Content-type:application/json');
      echo json_encode($json);
    }




   
   
   
    
    /************* change_password function *************/
    public function change_password()
    {
     $password = $this->input->get_post('old_password', TRUE);
      $new_password = $this->input->get_post('password', TRUE);
      $id = $this->input->get_post('user_id', TRUE);

                $password = base64_encode($password);
                $new_password = base64_encode($new_password);
                
      $arr_login = ['id' => $id ,'password' => $password];
      $login = $this->webservice_model->get_where('users', $arr_login);

      $arr_data = ['password'=>$new_password];
      
      
      if ($login)
      {     
          $this->webservice_model->update_data('users',$arr_data,$arr_login);
            $arr_login = ['id' => $id];

            $login = $this->webservice_model->get_where('users', $arr_login);

          $ressult['result']=$login[0];
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;

        
      }
      else
      {
          $ressult['result']=(object)[];
          $ressult['message']='unsuccessfull';
          $ressult['status']='0';
          $json = $ressult;
      }

      header('Content-type: application/json');
      echo json_encode($json);
    }


	      /************* social_login function *************/

     public function social_login(){

      
       


 $arr_data = [
            'first_name'=>$this->input->get_post('first_name'),
            'last_name'=>$this->input->get_post('last_name'),
            'email'=>$this->input->get_post('email'),
            'type'=>'USER',
            'status'=>'Active',
            'social_id'=>$this->input->get_post('social_id'),
            'register_id'=>$this->input->get_post('register_id'),
            'ios_register_id'=>$this->input->get_post('ios_register_id') 
            ];
      $image = $this->input->get_post('image');

      if($image!=""){
         $img = "USER_IMG_" . rand(1, 10000) . ".png";
         @file_put_contents('uploads/images/'.$img, file_get_contents($image));
         $arr_data['image'] = $img;

      }

      $arr_get = ['social_id'=>$arr_data['social_id']];

      $login = $this->webservice_model->get_where('users',$arr_get);

      if ($login) {  
     
     
     $arr_data1 = [
           
            'register_id'=>$this->input->get_post('register_id')          
            ];
        $this->webservice_model->update_data('users',$arr_data1,$arr_get);
        $data = $this->webservice_model->get_where('users',$arr_get);
        $data[0]['image']=SITE_URL.'uploads/images/'.$data[0]['image'];
       
         $id = $data[0]['id'];
  $tktToken = $this->generateToken($id);

  $login_token  = $this->webservice_model->get_where('tokens',['user_id'=>$id]); 
  
  $data[0]['token'] = $login_token[0]['authorization_token'];
  
       
       
          $ressult['result']=$data[0];
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;

      }else{



        //$arr_data['user_code'] = $this->webservice_model->generateRandomString(4);

        $id = $this->webservice_model->insert_data('users',$arr_data);
        
    
        $data = $this->webservice_model->get_where('users',['id'=>$id]);    
        
           
  $tktToken = $this->generateToken($id);

  $login_token  = $this->webservice_model->get_where('tokens',['user_id'=>$id]); 
  
  $data[0]['token'] = $login_token[0]['authorization_token'];
  
  
  
        $data[0]['image']=SITE_URL.'uploads/images/'.$data[0]['image'];
         $data[0]['provider_logo']=SITE_URL.'uploads/images/'.$data[0]['provider_logo'];

          $ressult['result']=$data[0];
          $ressult['message']='successfull';
          $ressult['status']='1';
          $json = $ressult;

      }

      header('Content-type: application/json');
      echo json_encode($json);
    }

 	

    
    
    
/************* get_category function *************/
   
   
public

  function get_category()
    {
        
            $provider_id = $this->input->get_post('provider_id', TRUE) ;


    $fetch = $this->webservice_model->get_where('category',['status'=>'Active']);
    if ($fetch)
      {
      foreach($fetch as $val)
        {
            if($provider_id){
             $cat_id = $val['id'];
                $checking = $this->webservice_model->get_where('provider_category',['cat_id'=>$cat_id,'provider_id'=>$provider_id]);
if($checking){
     $val['category_added'] = 'Yes';
}else{
     $val['category_added'] = 'No';
    
}
}
          $val['image']=SITE_URL.'uploads/images/'.$val['image'];
           $val['status'] = 'No';
          $data[] = $val;
        }

         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
         $ressult['result'] = [];
         $ressult['message'] = 'unsuccessful';
         $ressult['status'] = '0';
         $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    	
    	

 public function generateToken($userId,$tktReason='register'){
     
      $where = "user_id = '$userId'";
    

     

      $login = $this->webservice_model->get_where('tokens',$where);
      if ($login) {
     
        $static_str='AL';
        $currenttimeseconds = date("mdY_His");
        $token_id=$static_str.$userId.$currenttimeseconds;
        $data = array(
                 'authorization_token' => md5($token_id),
                 'user_id' => $userId,
                 );
         $this->webservice_model->update_data('tokens',$data,$where);

       }else{
           
            $static_str='AL';
        $currenttimeseconds = date("mdY_His");
        $token_id=$static_str.$userId.$currenttimeseconds;
        $data = array(
                 'authorization_token' => md5($token_id),
                 'user_id' => $userId,
                 );
         $this->webservice_model->insert_data('tokens',$data);
           
       }

     }

	 
    
      
    /************* add_rating_review function *************/

    public function add_rating_review(){

$date_time = date('Y-m-d H:i:s');
         $arr_data = [
            'user_id'=>$this->input->get_post('user_id'),
            'rest_id'=>$this->input->get_post('rest_id'),
            'rating'=>$this->input->get_post('rating'),
            'date_time'=>$date_time,
            'review'=>$this->input->get_post('review')
            ];
      
        $arr_gets = ['user_id'=>$this->input->get_post('user_id'),'rest_id'=>$this->input->get_post('rest_id')];
        $login = $this->webservice_model->get_where('rating_review',$arr_gets); 
        
      
      if ($login) {
        
        $ressult['result']=(object)[];
        $ressult['message']='already provided rating review';
        $ressult['status']='0';
        $json = $ressult;
                               
        header('Content-type:application/json');
        echo json_encode($json);
        die;
      }     


      $id = $this->webservice_model->insert_data('rating_review',$arr_data);

      if ($id=="") {
        $json = ['result'=>'unsuccessfull','status'=>0,'message'=>'data not found'];
      }else{
                              
        $arr_gets = ['id'=>$id];
        $login = $this->webservice_model->get_where('rating_review',$arr_gets); 
        
        $product_id = $login[0]['id'];
        
        
      
        $ressult['result']=$login[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }

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

    }
 
 
 
 
 
  /*************  get_user_review_rating *************/
  public

  function get_user_review_rating()
    {
        
        	$user_id =  $this->input->get_post('user_id', TRUE);
        
     	    
     	  
     	         	          $arr_get = "user_id = '$user_id'";

	
                 	$fetch = $this->webservice_model->get_where('rating_review_request', $arr_get);
	
	
    if ($fetch)
      {
       foreach($fetch as $val1)

        {
            
             

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['provider_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      
         $data[] = $val1;
        }
         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
        $ressult['result'] = [];
        $ressult['message'] = 'unsuccessful';
        $ressult['status'] = '0';
        $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
    
  /*************  get_provider_review_rating1 *************/
  public

  function get_provider_review_rating1($provider_id)
    {
        
        	$provider_id =  $this->input->get_post('provider_id', TRUE);
        
     	    
     	  
     	         	          $arr_get = "provider_id = '$provider_id'";

	
                 	$fetch = $this->webservice_model->get_where('rating_review_request', $arr_get);
	
	
    if ($fetch)
      {
          
           $get = $this->db->select_avg("rating", "rating")->where(['provider_id'=>$provider_id])->get('rating_review_request')->result_array();

                        $rating = ($get[0]['rating']=='') ?  "0" : $get[0]['rating'];   

                     


       foreach($fetch as $val1)

        {
            
             

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['user_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      $val1['avg_rating']=$rating; 
         $data[] = $val1;
        }
        
        
        //////////////
        
        
        ///////////////
        return $ressult['result'] = $data;
      }
      else
      {
      return  $ressult['result'] = [];
       
      }

    }
    
    
    
  /*************  get_provider_review_rating *************/
  public

  function get_provider_review_rating()
    {
        
        	$provider_id =  $this->input->get_post('provider_id', TRUE);
        
     	    
     	  
     	         	          $arr_get = "provider_id = '$provider_id'";

	
                 	$fetch = $this->webservice_model->get_where('rating_review_request', $arr_get);
	
	
    if ($fetch)
      {
       foreach($fetch as $val1)

        {
            
             

	     $user_details= $this->webservice_model->get_where('users',['id'=>$val1['user_id']]);
       
         
         $val1['user_name']= $user_details[0]['first_name'];
         $val1['image']=SITE_URL.'uploads/images/'.$user_details[0]['image'];
        
      
      
         $data[] = $val1;
        }
        
        
        //////////////
        
        
        ///////////////
         $ressult['result'] = $data;
         $ressult['message'] = 'successful';
         $ressult['status'] = '1';
         $json = $ressult;
      }
      else
      {
        $ressult['result'] = [];
        $ressult['message'] = 'unsuccessful';
        $ressult['status'] = '0';
        $json = $ressult;
      }

    header('Content-type: application/json');
    echo json_encode($json);
    }
    
    
    
    


/************* get_provider_details function *************/
public function get_provider_details(){

  $arr_get = ['id'=>$this->input->get_post('provider_id')];


$provider_id = $this->input->get_post('provider_id');
$user_id = $this->input->get_post('user_id');

  $login = $this->webservice_model->get_where('users',$arr_get);

  if($login) {
  
   $login[0]['image']=SITE_URL.'uploads/images/'.$login[0]['image'];
  // $login[0]['store_logo']=SITE_URL.'uploads/images/'.$login[0]['store_logo'];
  // $login[0]['store_cover_image']=SITE_URL.'uploads/images/'.$login[0]['store_cover_image'];
  // $login[0]['licence_image']=SITE_URL.'uploads/images/'.$login[0]['licence_image'];
   //$login[0]['pass_image']=SITE_URL.'uploads/images/'.$login[0]['pass_image'];
    
   
              
                        $get = $this->db->select_avg("rating", "rating")->where(['to_id'=>$provider_id])->get('rating_review')->result_array();

                        $rating = ($get[0]['rating']=='') ?  "0" : $get[0]['rating'];   

                        $login[0]['rating'] = $rating;

              
              
              
    
           		       $rating_count = $this->webservice_model->get_where('rating_review',['to_id'=>$provider_id]);
                     if($rating_count){
           		       $aa = count($rating_count);
                          $login[0]['rating_count'] = $aa;
                     }else{
           		       $aa =0;
                          $login[0]['rating_count'] = $aa;
                         
                     }
                /*     
                        $follow_detail = $this->Webservice_model->get_where('follow_detail',['request_get_id'=>$login[0]['id']]);
                     if($follow_detail){
           		       $follow_detail = count($follow_detail);
                     }else{
           		       $follow_detail = 0;
                         
                     }
                     
                        $follow_detail1 = $this->Webservice_model->get_where('follow_detail',['request_id'=>$login[0]['id']]);
                     if($follow_detail1){
           		       $follow_detail1 = count($follow_detail1);
                     }else{
           		       $follow_detail1 = 0;
                         
                     }
                     
    $rating = number_format($rating, 1, '.', '');
    $login[0]['rating']=$rating;

    $login[0]['total_posts']=$aa;
    $login[0]['total_followers']=$follow_detail;
    $login[0]['total_following']=$follow_detail1;*/
    
    
   

           /*   $login_image = $this->webservice_model->get_where('provider_images',['user_id'=>$login[0]['id']]);
                      if($login_image){
                                foreach($login_image as $val)
                               {
                                
                               
                                   $val['image']=SITE_URL.'uploads/images/'.$val['image'];  
                                   $login[0]['provider_images'][]=$val;
                                  }
                              
                                }else{
                                   $login[0]['provider_images']=[];
    
                                }
*/

  $arr_provider = ['user_id'=>$user_id,'provider_id'=>$login[0]['id']];
                               	$fav_provider = $this->webservice_model->get_where('fav_provider', $arr_provider);
                                 if($fav_provider){
                                   $login[0]['fav_provider']='YES';

                                 }else{
                                   $login[0]['fav_provider']='NO';
                                     
                                 }
                        
                              
                               
  $ressult['result']=$login[0];
  $ressult['message']='successfull';
  $ressult['status']='1';
  $json = $ressult;

}else{

  $json = ['result'=>'unsuccessfull','status'=>'0','message'=>'Data Not Found'];

}

header('Content-type: application/json');
echo json_encode($json);
}




      /*************  add_wallet_amount *************/
    public

    function add_wallet_amount()
    {                     
          
      $arr_data = array(
        'user_id' => $this->input->get_post('user_id'),
        'payment_method' => $this->input->get_post('payment_method'), 
        'transaction_type' => 'Top Up', 
        'total_amount' => $this->input->get_post('total_amount')                           
      );

            
 $login_nwe = $this->webservice_model->get_all('key_setting');

$stripe_status=$login_nwe[0]['stripe_status'];
if($stripe_status == 'Test'){
    $key_new= $login_nwe[0]['stripe_secret_key_test'];
}else{
    $key_new= $login_nwe[0]['stripe_secret_key_live'];
    
}


      $user_id = $this->input->get_post('user_id');
      $total_amount = $this->input->get_post('total_amount');
      $token = $this->input->get_post('token');
      $currency = $this->input->get_post('currency');
     
	$url = 'https://api.stripe.com/v1/charges';
        $fields = [
            'amount' => ($arr_data['total_amount']*100),
            'currency' => $currency,
            'source' => $token,
            'metadata' => ['order_id'=>$arr_data['user_id']]
        ];

        $fields_string = http_build_query($fields);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/charges");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_USERPWD, $key_new . ":" . "");

       $headers = array();
       $headers[] = "Content-Type: application/x-www-form-urlencoded";
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

       $response = curl_exec($ch);  
       $response = json_decode($response);
       
       if (isset($response->error)) {
          $ressult['result']=$response;
          $ressult['message']='unsuccessful';
          $ressult['status']='0';
          $json = $ressult;
          header('Content-type: application/json');
          echo json_encode($json);
          die;
       }

       curl_close ($ch);
      
 

      $pay = $this->webservice_model->insert_data('payment', $arr_data);

     
      if ($pay != "") {

                          $single_data = ['id' => $pay];

                          $fetch_order = $this->webservice_model->get_where('payment',$single_data); 
                          

                         $fetch_user = $this->webservice_model->get_where('users',['id'=>$user_id]); 
                         $u_wallet = $fetch_user[0]['wallet'];
                         $new_wallet = $u_wallet + $total_amount ; 
                         $this->webservice_model->update_data('users',['wallet'=>$new_wallet],['id'=>$user_id]);


                          $ressult['result']=$fetch_order[0];
                          $ressult['message']='successful';
                          $ressult['status']='1';
                          $json = $ressult;                         
                          
      }
      else {
                          $ressult['result']='Data Not Found';
                          $ressult['message']='unsuccessful';
                          $ressult['status']='0';
                          $json = $ressult;
      }

                         header('Content-type: application/json');
                         echo json_encode($json);
    }
    
  
   
	
  
/************* update_location function *************/

    public function update_location(){

      $arr_get = ['id'=>$this->input->get_post('user_id')];

      $login = $this->webservice_model->get_where('users',$arr_get);
      if ($login[0]['id'] == "")
      {
                          $ressult['result']=(object)[];
                          $ressult['message']='unsuccessfull';
                          $ressult['status']='0';
                          $json = $ressult;

                                header('Content-type:application/json');
                                echo json_encode($json);
                                die;
      }



       $arr_data = [
           'address'=>$this->input->get_post('address'),
           'lat'=>$this->input->get_post('lat'),
           'lon'=>$this->input->get_post('lon'),
           'provider_lat'=>$this->input->get_post('lat'),
           'provider_lon'=>$this->input->get_post('lon'),
            ];

                    


 

      $res = $this->webservice_model->update_data('users',$arr_data,$arr_get);
      if ($res)
      {
        $data = $this->webservice_model->get_where('users',$arr_get);

        $ressult['result']=$data[0];
        $ressult['message']='successfull';
        $ressult['status']='1';
        $json = $ressult;
      }
      else
      {
                          $ressult['result']=(object)[];
                          $ressult['message']='unsuccessfull';
                          $ressult['status']='0';
                          $json = $ressult;
      }

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

                          

    }

    
 
  
  
  
  // end class

  }

?>
