<?php 

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Response;
use App\Ad;
use App\User;
use App\Category;
use App\Contact_query;
use App\Country;
use App\Post;
use App\Slider;
use App\City;
use App\Brand;
// use App\Agent;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;

use Validator;

class ApiController extends Controller
{
    /**
     * Display a listing of the resource.
     * 
     * @return \Illuminate\Http\Response
     */
     
       public function index()
    {
        $limit_premium_ads = get_option('number_of_premium_ads_in_home');
        $limit_regular_ads = get_option('number_of_free_ads_in_home');
        $limit_urgent_ads = get_option('number_of_urgent_ads_in_home');

        $premium_ads = Ad::activePremium()->with('city')->limit($limit_premium_ads)->orderBy('id', 'desc')->get();
        $regular_ads = Ad::activeRegular()->with('city')->limit($limit_regular_ads)->orderBy('id', 'desc')->get();
        $urgent_ads = Ad::activeUrgent()->with('city')->limit($limit_urgent_ads)->orderBy('id', 'desc')->get();
        
        $outputpremium = array();
        $outputregular = array();
        $outputurgent = array();

       
        
        foreach($premium_ads as $ad){
            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'city' => $ad->city->city_name,
                    'priceplan'=>$ad->price_plan,

                    
                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                foreach($urgent_ads as $ad){
            array_push($outputurgent ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'agent' => $ad ->agent,

                    'address' => $ad ->address,
                    'city' => $ad->city->city_name,
                    'route' => route('single_ad', $ad->slug),
                    'priceplan'=>$ad->price_plan,

                         'price' =>   $ad->price,
                         ));
        };
        
          foreach($regular_ads as $ad){
            array_push($outputregular ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'agent' => $ad ->agent,
                    'address' => $ad ->address,
                    'city' => $ad->city->city_name,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>   $ad->price,
                    'priceplan'=>$ad->price_plan,
                         ));
        };
        
        
        if($regular_ads || $urgent_ads|| $premium_ads )
        {
            return Response::json(
                array(
                    'status' => true,
                    'urgent' =>  $outputurgent,
                    'regular' =>  $outputregular,
                    'premium' =>  $outputpremium,

                    
                    ), 200
            );
            //return response()->json($ads);
        } else
        {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with your Information"
                        ]
                    ), 404
            );
        }
        
    }
    public function mapindex()
    {
        $limit_premium_ads = 10;
       

        $premium_ads = Ad::all();
     
        
        $outputpremium = array();
     

       
        
        foreach($premium_ads as $ad){
            
            $outputgallery= array();
           $premium_img  = $ad->media_imgs;
               //foreach($premium_img as $adz){
                 //     array_push($outputgallery ,array(
                //    'media_url' => media_url($adz->media_name),
               //          ));
                   
                   
                   
              // };
            
            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $premium_img,
                    'amenities' =>$ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                      'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'fav' => $ad->favourite,

                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'agent' => $ad ->agent,
                    'city' => $ad->city->city_name,
                    'priceplan'=>$ad->price_plan,
                   'created_at'=>$ad->created_at->diffForHumans(),
                        'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
              
        if( $premium_ads )
        {
            return Response::json(
                array(
                    'status' => true,
                  
                    'data' =>  $outputpremium,

                    
                    ), 200
            );
            //return response()->json($ads);
        } else
        {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with your Information"
                        ]
                    ), 404
            );
        }
        
    }
    
    public function vincentyGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
  // convert from degrees to radians
  
  
  $earthRadius = 6371000;
  $latFrom = deg2rad($latitudeFrom);
  $lonFrom = deg2rad($longitudeFrom);
  $latTo = deg2rad($latitudeTo);
  $lonTo = deg2rad($longitudeTo);

  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
    pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  return $angle * $earthRadius;
} 
    public function premium()
    {
        $ads = Ad::with('city', 'country', 'state')->whereStatus(1)->orderBy('id', 'desc')->paginate(20);
        
        if($ads)
        {
            return Response::json(
                array(
                    'status' => true,
                    'data' => $ads
                    ), 200
            );
        } else
        {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with your Information"
                        ]
                    ), 404
            );
        }
        
    }
    
    
    
    
            public function searchmapview(Request $request)
    {   
        
              function radians($degrees)
    {
         return 0.0174532925 * $degrees;
    }
                $user = Auth::user();
        if(isset($request->long) && isset($request->house_type) && isset($request->purpose)  && isset($request->bedrooms)){
            $longitude = $request->long;
            $latitude  = $request->lat;
            $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
              if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
          ->Where('status', '=', 1)
          ->Where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                                        ->WhereBetween('price', [$minPrice, $maxPrice])



          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
            ->Where('purpose', '=', $request->purpose)
            ->Where('type', '=', $request->house_type)
                      ->Where('status', '=', 1)

            ->WhereBetween('price', [$minPrice, $maxPrice])



          ->orderBy('distance')
          ->get();
          
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
        $dist =  array();
        $check =  array();


                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                           $ad_distances = unserialize($ad->distances);
                                             foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            }  
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
  $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );
            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'angle' => number_format( $angle , 2),
                    'id'=>$ad->id,
                    'distances'=>$dist,
                    //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                    'params' => 'lhpb',


                    ), 200
            );
            
            
        }
        
        else if( isset($request->house_type) && isset($request->purpose)  && isset($request->bedrooms)){
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                    ->Where('beds', '=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                    ->Where('beds', '>=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
        $dist =  array();
        $check =  array();


                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
  //$angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'angle' => '',
                    'id'=>$ad->id,
                    'distances'=>$dist,
                    //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'params' => 'hpb',

                    // 'longitude' => $longitude,
                    //'latitude' => $latitude,


                    ), 200
            );
            

         
            
        }
        
        
            else if(isset($request->long) && isset($request->purpose)  && isset($request->bedrooms)){
            $longitude = $request->long;
            $latitude  = $request->lat;
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
              if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
                    ->Where('status', '=', 1)

          ->Where('purpose', '=', $request->purpose)
                                        ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
                    ->Where('status', '=', 1)

                    ->Where('purpose', '=', $request->purpose)
                                        ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
        $dist =  array();
        $check =  array();


                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
  $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                     'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                      //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'angle' => number_format( $angle , 2),
                    'id'=>$ad->id,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                                        'params' => 'lpb',



                    ), 200
            );
            
            
            
        }
        
        
            else if(isset($request->long) && isset($request->purpose)  && isset($request->house_type)){
            $longitude = $request->long;
            $latitude  = $request->lat;
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
             // if($request->bedrooms <  5 ) {
            $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('type', '=', $request->house_type)
          ->Where('purpose', '=', $request->purpose)
                    ->Where('status', '=', 1)

          ->WhereBetween('price', [$minPrice, $maxPrice])
          ->orderBy('distance')
          ->get();
             $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();

          
           // }
            
                  $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
  $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'angle' => number_format( $angle , 2),
                    'id'=>$ad->id,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                                        'params' => 'lph',



                    ), 200
            );
            
            
            
        }
        
            else if(isset($request->long) && isset($request->house_type)  && isset($request->bedrooms)){
            $longitude = $request->long;
            $latitude  = $request->lat;
            $longitude = $request->long;
            $latitude  = $request->lat;
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
            
            if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
                    ->Where('status', '=', 1)

          ->Where('type', '=', $request->house_type)
           ->WhereBetween('price', [$minPrice, $maxPrice])


          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
                    ->Where('status', '=', 1)

                    ->Where('type', '=', $request->house_type)
                                                            ->WhereBetween('price', [$minPrice, $maxPrice])


          ->orderBy('distance')
          ->get();
          
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
                         $check =  array();


                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                              if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            }  
                                                
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
  $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'angle' => number_format( $angle , 2),
                    'id'=>$ad->id,
                    //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                                        'params' => 'lhb',



                    ), 200
            );
            
            
            
        }
        
            else if(isset($request->long) && isset($request->house_type)){
 
                 $longitude = $request->long;
            $latitude  = $request->lat;
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('type', '=', $request->house_type)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
             $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
                         $check =  array();


                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                                if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
  $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );
            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                    //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'angle' => number_format( $angle , 2),

                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                                        'params' => 'lh2',



                    ), 200
            );
            
        }
        
           else if(isset($request->long) &&  isset($request->purpose)){
            $longitude = $request->long;
            $latitude  = $request->lat;
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
                                  $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('purpose', '=', $request->purpose)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
             $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
                         $check =  array();


          
                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                      $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );


            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                     //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'angle' => number_format( $angle , 2),

                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                                        'params' => 'lp',



                    ), 200
            );
            
        }
        
        
        
           else if(isset($request->long)  && isset($request->bedrooms)){
            $longitude = $request->long;
            $latitude  = $request->lat;
                     $longitude = $request->long;
            $latitude  = $request->lat;
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
            
            if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
                         $check =  array();


                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                              if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            }   
                                                
                                            }}}

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
  $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                    //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'angle' => number_format( $angle , 2),

                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                                        'params' => 'lb',



                    ), 200
            );
            
            
        }
        
           else if(isset($request->house_type) && isset($request->purpose)){
          
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)

                                                            ->WhereBetween('price', [$minPrice, $maxPrice])

                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
        $check =  array();

                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                              if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}};

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
          //$angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                     //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'angle' => '',

                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'params' => 'hp',

                    // 'longitude' => $longitude,
                    //'latitude' => $latitude,


                    ), 200
            );
            
            
        }
        
           else if(isset($request->house_type)  && isset($request->bedrooms)){
        
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::Where('type', '=', $request->house_type)
                    ->Where('beds', '=', $request->bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)

                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::Where('type', '=', $request->house_type)
                    ->Where('beds', '>=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
                         $check =  array();


                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                            }}};

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                      //$angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                     'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                     //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'angle' => '',

                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'params' => 'hb',

                     'longitude' => '',
                    'latitude' => '',


                    ), 200
            );
            
            
        }
        
           else if(isset($request->purpose)  && isset($request->bedrooms)){
           
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
            if($request->bedrooms <  5 ) {
            
               $ $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('beds', '=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
            
            else {
            
               $ $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('beds', '>=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
        $check =  array();

                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                       $ad_distances = unserialize($ad->distances);
                                         foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}};
                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                      //$angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                     'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                     //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'angle' => '',

                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => '',
                    'latitude' => '',
                                        'params' => 'pb',



                    ), 200
            );
            
            
        }
        
           else if(isset($request->long)){
            $longitude = $request->long;
            $latitude  = $request->lat;
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
             $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
                         $check =  array();
                                               $outputpremium = array();


foreach($premium_ads as $ad){
    
   


                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                           $ad_distances = unserialize($ad->distances);
                                             foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}};
                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                      $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                      //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'angle' => number_format( $angle , 2),

                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,
                                        'params' => 'l',



                    ), 200
            );
            
        }}
           else if( isset($request->house_type) ){
         
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
          
            
           
            
                $premium_ads = Ad::Where('type', '=', $request->house_type)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)

                    ->get();
                       $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();

                            $check =  array();

            
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                            $ad_distances = unserialize($ad->distances);

                                              foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                                if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}};

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                      //$angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                  //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'angle' => '',

                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'params' => 'h',

                     'longitude' => '',
                    'latitude' => '',


                    ), 200
            );
            
            
        }
        
           else if( isset($request->purpose) ){
         
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
           
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                         ->Where('status', '=', 1)

                    
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                       $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();

                    $check =  array();

                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                                                                       $ad_distances = unserialize($ad->distances);

                                              foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                               if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}};

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                      //$angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                                   'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'distances'=>$dist,
                    'id'=>$ad->id,
                    //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'angle' => '',

                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'params' => 'p',

                     'longitude' => '',
                    'latitude' => '',


                    ), 200
            );
            
            
        }
        
           else if(isset($request->bedrooms)){
       
             $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::Where('beds', '=', $request->bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)

                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::Where('beds', '>=', $request->bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
            
               $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
        $check =  array();

                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                                                                       $ad_distances = unserialize($ad->distances);

                                              foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                                if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } 
                                                
                                            }}};

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                      //$angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'angle' => '',
                    'id'=>$ad->id,
                    'distances'=>$dist,
                     //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'params' => 'b',

                      'longitude' => '',
                    'latitude' => '',



                    ), 200
            );
            
            
            
            
            
        }
        

         else {
                $minPrice = $request->min_price;
            $maxPrice =$request->max_price;
                    $premium_ads = Ad::WhereBetween('price', [$minPrice, $maxPrice])->Where('status', '=', 1)->get();
                       $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();
                 $dist =  array();
        $check =  array();

                    
                               $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                            $amenities = unserialize($ad->amenities);
                                            $ad_distances = unserialize($ad->distances);

                                              foreach($distances as $golbal_distance){
                                        if(array_key_exists($golbal_distance->id,(array) $ad_distances)){
                                            if($ad_distances[$golbal_distance['id']] > 0){
                                                if(!in_array($golbal_distance->brand_name ,$check)){
                                               array_push($dist, array('brand_name'=>$golbal_distance->brand_name,'distance' => themeqx_distance_measure($ad_distances[$golbal_distance->id])));
                                                                                        array_push($check , $golbal_distance->brand_name);

                                                
                                            } }}};

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                      $angle = '';

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                     'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'angle' => '',

                    'distances'=>$dist,
                    'id'=>$ad->id,
                     //  'snapped' => $ad->agent->get_address(),
                     'snapped' => '',
                    'gravatar'=>'' ,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'city' => $ad->city->city_name,
                    'created_at'=>$ad->created_at->diffForHumans(),
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                    'full_address'=>$ad->full_address(),
                    'condition' => $ad->ad_condition,
                    'description' => nl2br($ad->description),
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'unit_type'=>$ad->unit_type,
                    'route' => route('single_ad', $ad->slug),
                    'price' =>  number_format($ad->price),
                    'currency_type'=>$ad->currency_type,
                     'price_per_unit'=>number_format($ad->price_per_unit),

                         ));
        };

            return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                      'longitude' => '',
                    'latitude' => '',
                    'params' => 'else',

                    ), 200
            );
        }
                         
            

    }
           
            public function searchmapview2(Request $request)
    {  
                // if ( ! Auth::check())
            //return ['status'=>0, 'msg'=> trans('app.error_msg'), 'redirect_url' => route('login')];

        $user = Auth::user();
                    $purpose = $request->purpose;
        $city = $request->location;
        $bedrooms = $request->bedrooms;
        $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
        
        function radians($degrees)
    {
         return 0.0174532925 * $degrees;
    }
        function codexworldGetDistanceOpt($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
    $rad = M_PI / 180;
    //Calculate distance from latitude and longitude
    $theta = $longitudeFrom - $longitudeTo;
    $dist = sin($latitudeFrom * $rad) 
        * sin($latitudeTo * $rad) +  cos($latitudeFrom * $rad)
        * cos($latitudeTo * $rad) * cos($theta * $rad);

    return acos($dist) / $rad * 60 *  1.853;
}
//$minPrice = $request->min_price;
   //             $maxPrice =$request->max_price;

$loc = null;
$res = false;
if(isset($request->location))  {
    
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode ($city)."&key=AIzaSyDs3BoH5BZIATH8ANI7lpv9WiJTn62R_q8";
    
      $json = json_decode(file_get_contents($url), true);
      
      if ($json['status'] == "OK") {
                $cej = $json['results'][0] ;
                $loc = $cej['geometry']['location'];

          $res = true;
          
      }
      else {
          $res = false;

                //$cej = $json['results'][0] ;
               // $loc = $cej['geometry']['location'];

          
          
      }
}     
          
    
        if($loc && isset($request->house_type) && isset($request->purpose)  && isset($request->bedrooms)){
            $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
        if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
           //$minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
              if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
          ->Where('status', '=', 1)
          ->Where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                                        ->WhereBetween('price', [$minPrice, $maxPrice])



          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
            ->Where('purpose', '=', $request->purpose)
            ->Where('type', '=', $request->house_type)
                      ->Where('status', '=', 1)

            ->WhereBetween('price', [$minPrice, $maxPrice])



          ->orderBy('distance')
          ->get();
          
            }
            
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                     
                               
                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                          $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
  $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );
                                                                                     //$angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'angle' => number_format( $angle , 2),
                    'id'=>$ad->id,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'fav'=>$ad->is_my_favorite(),
                                        'city' => $ad->city->city_name,
                                         'created_at'=>$ad->created_at->diffForHumans(),
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     //'longitude' => $longitude,
                    'location' => 'true',
                    //'params' => 'lhpb',


                    ), 200
            );
            
            
        }
        
        else if( isset($request->house_type) && isset($request->purpose)  && isset($request->bedrooms)){
            // $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
            $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                    ->Where('beds', '=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                    ->Where('beds', '>=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
            
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                  
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                                                          $earthRadius = 6371000;
   $angle = 0;

                                                                                   //  $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
'angle' => number_format( $angle , 2),
                    'purpose' =>  $ad->purpose,
                    'id'=>$ad->id,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' => number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'fasle',

                                        'params' => 'hpb',

                     //'longitude' => $longitude,
                    //'latitude' => $latitude,


                    ), 200
            );
            

         
            
        }
        
        
            else if($loc && isset($request->purpose)  && isset($request->bedrooms)){
                 $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
             //$minPrice = $request->min_price;
            //$maxPrice =$request->max_price;
              if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
                    ->Where('status', '=', 1)

          ->Where('purpose', '=', $request->purpose)
                                        ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
                    ->Where('status', '=', 1)

                    ->Where('purpose', '=', $request->purpose)
                                        ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
            
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                      $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
    $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

                                         //                                             $angle= codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
'angle' => number_format( $angle , 2),
'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'id'=>$ad->id,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>  number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'true',

                    // 'longitude' => $longitude,
                   // 'latitude' => $latitude,
                     //                   'params' => 'lpb',



                    ), 200
            );
            
            
            
        }
        

        
            else if($loc && isset($request->purpose)  && isset($request->house_type)){
                 $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            // $minPrice = $request->min_price;
            //$maxPrice =$request->max_price;
             // if($request->bedrooms <  5 ) {
            $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('type', '=', $request->house_type)
          ->Where('purpose', '=', $request->purpose)
                    ->Where('status', '=', 1)

          ->WhereBetween('price', [$minPrice, $maxPrice])
          ->orderBy('distance')
          ->get();
          
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                  
          
           // }
            
                  $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
    $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

                           //                                                           $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'id'=>$ad->id,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
'angle' => number_format( $angle , 2),

                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'true',

                    // 'longitude' => $longitude,
                    //'latitude' => $latitude,
                                     //   'params' => 'lph',



                    ), 200
            );
            
            
            
        }
        
            else if($loc && isset($request->house_type)  && isset($request->bedrooms)){
                 $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            // $minPrice = $request->min_price;
            //$maxPrice =$request->max_price;
            
            if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
                    ->Where('status', '=', 1)

          ->Where('type', '=', $request->house_type)
           ->WhereBetween('price', [$minPrice, $maxPrice])


          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
                    ->Where('status', '=', 1)

                    ->Where('type', '=', $request->house_type)
                                                            ->WhereBetween('price', [$minPrice, $maxPrice])


          ->orderBy('distance')
          ->get();
          
            }
            
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                  
                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                                                         $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
    $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

                                                                             //         $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'id'=>$ad->id,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                                         'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'slug' => $ad->slug,
'angle' => number_format( $angle , 2),


                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                                        'fav'=>$ad->is_my_favorite(),

                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'true',

                     //'longitude' => $longitude,
                    //'latitude' => $latitude,
                    //                    'params' => 'lhb',



                    ), 200
            );
            
            
            
        }
        
            else if($loc && isset($request->house_type)){
 
                     $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
          //   $minPrice = $request->min_price;
          //  $maxPrice =$request->max_price;
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('type', '=', $request->house_type)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                  
          
                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
    $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

                                                                                   //   $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'id'=>$ad->id,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'angle' => number_format( $angle , 2),

                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'fav'=>$ad->is_my_favorite(),

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'true',

                    // 'longitude' => $longitude,
                   // 'latitude' => $latitude,
                                        'params' => 'lh2',
//


                    ), 200
            );
            
        }
        
           else if($loc &&  isset($request->purpose)){
                $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
           //  $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
                                  $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('purpose', '=', $request->purpose)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                   
          
                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
    $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

                                //                                                      $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
'angle' => number_format( $angle , 2),

                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'id'=>$ad->id,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>  number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'true',





                    ), 200
            );
            
        }
        

           else if($loc  && isset($request->bedrooms)){
     $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
           //  $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
            
            if($request->bedrooms <  5 ) {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '=', $request->bedrooms)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
            
            else
            
            {
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->Where('beds', '>=', $request->bedrooms)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')
          ->get();
          
            }
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                 
                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
    $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians( $latitude ) ) ) );

            //$farawa = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
'angle' => number_format( $angle , 2),

                    'description' => $ad->description,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                    'id'=>$ad->id,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' => number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'true',

                    



                    ), 200
            );
            
            
        }
        
           else if(isset($request->house_type) && isset($request->purpose)){
                $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
          
           ///  $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)


                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('type', '=', $request->house_type)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
            
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                  
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $angle = 0;

                  //                                                                   $angle =codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'id'=>$ad->id,
                    'square_feet'=> $ad ->square_unit_space,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
'angle' => number_format( $angle , 2),

                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'false',

                            //            'params' => 'hp',

                    // 'longitude' => $longitude,
                    //'latitude' => $latitude,


                    ), 200
            );
            
            
        }
        
           else if(isset($request->house_type)  && isset($request->bedrooms)){
        
           //  $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
            $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::Where('type', '=', $request->house_type)
                    ->Where('beds', '=', $request->bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)

                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::Where('type', '=', $request->house_type)
                    ->Where('beds', '>=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                  
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $angle = 0;

                                        //                                             $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'id'=>$ad->id,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
'angle' => number_format( $angle , 2),

                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>  number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'false',

                                      //  'params' => 'hb',

                     //'longitude' => $longitude,
                    //latitude' => $latitude,


                    ), 200
            );
            
            
        }
        
           else if(isset($request->purpose)  && isset($request->bedrooms)){
           
            // $minPrice = $request->min_price;
            //$maxPrice =$request->max_price;
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('beds', '=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
            
            else {
            
                $premium_ads = Ad::where('purpose', '=', $request->purpose)
                    ->Where('beds', '>=', $request->bedrooms)
                              ->Where('status', '=', 1)

                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $angle = 0;
                                                                               //       $angle= codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
'angle' => number_format( $angle , 2),

                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'id'=>$ad->id,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'false',

                    // 'longitude' => $longitude,
                    //'latitude' => $latitude,
                           //             'params' => 'pb',



                    ), 200
            );
            
            
        }
        
           else if($loc){
                 $longitude = $loc['lng'];
            $latitude  = $loc['lat'];
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            // $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
                        $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
                    ->Where('status', '=', 1)

                                                  ->WhereBetween('price', [$minPrice, $maxPrice])

          ->orderBy('distance')//->paginate(4, 1)//->getCollection();
          ->get();;//->paginate(4, 1);
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                           
          
                      $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                  $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $latFrom = deg2rad($latitude);
  $lonFrom = deg2rad($longitude);
  $latTo = deg2rad($ad->$latitude);
  $lonTo = deg2rad($ad->$longitude);
  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
  pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  $angle * $earthRadius;
    $angle = ( 6367 * acos( cos( radians( $ad->latitude ) ) * cos( radians( $latitude ) ) * cos( radians( $longitude ) - radians( $ad->longitude ) ) + sin( radians( $ad->latitude ) ) * sin( radians($latitude ) ) ) );

                                                 // $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'outdoor_ammenties'=> $outdoor,
'angle' => number_format( $angle , 2),

                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'id'=>$ad->id,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'true',

                    // 'longitude' => $longitude,
                   // 'latitude' => $latitude,
                  //                      'params' => 'l',



                    ), 200
            );
            
        }
           else if( isset($request->house_type) ){
         
            // $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
           $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            
           
            
                $premium_ads = Ad::Where('type', '=', $request->house_type)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)

                    ->get();
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                              
            
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $angle = 0;
                                       //                                               $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
'angle' => number_format( $angle , 2),

                    'purpose' =>  $ad->purpose,
                    'id'=>$ad->id,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                        'location' => 'false',

                                       // 'params' => 'h',

                     //'longitude' => $longitude,
                    //'latitude' => $latitude,


                    ), 200
            );
            
            
        }
        
           else if( isset($request->purpose) ){
         
             //$minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
           
             $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
               $premium_ads = Ad::where('purpose', '=', $request->purpose)
                         ->Where('status', '=', 1)

                    
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                 
            
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
   $angle = 0;

                                                                        //             $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
'angle' => number_format( $angle , 2),

                    'purpose' =>  $ad->purpose,
                    'id'=>$ad->id,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                                            'location' => 'false',

                                        //'params' => 'p',

                    // 'longitude' => $longitude,
                    //'latitude' => $latitude,


                    ), 200
            );
            
            
        }
        
           else if(isset($request->bedrooms)){
       
           //  $minPrice = $request->min_price;
           // $maxPrice =$request->max_price;
            $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
            if($request->bedrooms <  5 ) {
            
               $premium_ads = Ad::Where('beds', '=', $request->bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                              ->Where('status', '=', 1)

                    ->get();
                    
            }
            
            else {
            
               $premium_ads = Ad::Where('beds', '>=', $request->bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
            }
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                                 
                   $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $angle = 0;

                                                                    //                  $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                        'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
                    'purpose' =>  $ad->purpose,
'angle' => number_format( $angle , 2),

                    'title' =>   $ad->title,
                    'id'=>$ad->id,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>  number_format($ad->price),
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                                                                                'location' => 'false',

                                    //    'params' => 'b',

                     //'longitude' => $longitude,
                    //'latitude' => $latitude,


                    ), 200
            );
            
            
            
            
            
        }
        

         else {
              $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
           if(!isset($request->minPrice)){ 
             $minPrice = 0;
        $maxPrice = 100000000000000000;
            
        }
        $distances = Brand::all();
        $indore_ammenties = Category::whereCategoryType('indoor')->get();
        $outdoor_ammenties = Category::whereCategoryType('outdoor')->get();
        $outdoor= array();
        $indoor= array();

                    $premium_ads = Ad::WhereBetween('price', [$minPrice, $maxPrice])->Where('status', '=', 1)->get();
                    
                               $outputpremium = array();
                                    foreach($premium_ads as $ad){
                                                $amenities = unserialize($ad->amenities);

                                        
                                                     foreach($indore_ammenties as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                        if(!in_array($in_ammenty->category_name, $indoor))
{
                                         array_push($indoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                    
                                     foreach($outdoor_ammenties  as $in_ammenty){
                                    if(array_key_exists($in_ammenty->id,(array) $amenities)){
                                        ;
                                                                              if(!in_array($in_ammenty->category_name, $outdoor))
{
                                         array_push($outdoor , $in_ammenty->category_name);
                                        
                                    }; };};
                                                                         $earthRadius = 6371000;
  $angle = 0;
                                                                               //       $angle = codexworldGetDistanceOpt($latitude, $longitude , $ad->latitude ,$ad->$longitude);

            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'outdoor_ammenties'=> $outdoor,
                    'indore_ammenties'=> $indoor,
'angle' => number_format( $angle , 2),

                    'purpose' =>  $ad->purpose,
                    'id'=>$ad->id,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'fav'=>$ad->is_my_favorite(),
                     'price_per_unit'=>number_format($ad->price_per_unit),
                    'unit_type'=>$ad->unit_type,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>  number_format($ad->price),
                         ));
        };

            return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                    'location' => 'false',

                          //              'params' => 'else',

                    ), 200
            );
        }
                         
            

    }
    

    
    
            public function searchandler(Request $request)
    { 
        
            $purpose = $request->purpose;
        $city = $request->location;
        $bedrooms = $request->bedrooms;
        $bathrooms = $request->bathrooms;
        $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
        $myRequest = new \Illuminate\Http\Request();
        $myRequest->setMethod('POST');
         if(isset($request->purpose))  { $myRequest->request->add(['purpose' =>$purpose]);     }
        if(isset($request->bedrooms))  {$myRequest->request->add(['bedrooms' =>$bedrooms]);  }
       if(isset($request->minPrice))  { $myRequest->request->add(['min_price' => $minPrice]);  }
        if(isset($request->maxPrice))  {$myRequest->request->add(['max_price' => $maxPrice]); }
        if(isset($request->bathrooms))  {$myRequest->request->add(['bathrooms' => $bathrooms]); }
if(isset($request->location))  {
    
    
    
    
    
      $json = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address='+$city+'&key=AIzaSyDs3BoH5BZIATH8ANI7lpv9WiJTn62R_q8"), true);
      
      if ($json['status'] == "OK") {
                $cej = $json['results'][0] ;
                $loc = $cej['geometry']['location'];

          
          
                  $myRequest->request->add(['long' =>$loc['lng']]);
                  $myRequest->request->add(['lat' =>$loc['lat']]);

          
          
      }
}

ApiController::searchmapview2($myRequest);

      


       //return  Response::json(
        //        array(
        //                'status' => true,
        //                'Authorization' => $loc['lat']
         //                   ), 200
         //   );
    }
    
           
        public function mapview(Request $request)
    {   
        //query database by location
        if(isset($request->longitude) && isset($request->latitude)){
            //get variables
            $longitude = $request->longitude;
            $latitude  = $request->latitude;
           
    
      
     
            //check for a direct match
            $premium_ads = Ad::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( latitude ) ) ) ) AS distance', [$latitude, $longitude, $latitude])
          ->having('distance', '<', 300000000000)
          ->orderBy('distance')
          ->get();
            
            //prepare for multi search
            $longitude = strval($longitude);
            $latitude  = strval($latitude);
            $longcount = strlen($longitude);
            $laticount = strlen($latitude);
            
            //set a base length
            if($longcount > $laticount){
                $length = $laticount;
            }else{
                $length = $longcount;
            }
            
            
            
            //query until property is found
            if($premium_ads){
                //format coordinates
                //$longitude = substr($longitude, 0, $length);
                //$latitude = substr($latitude,  0, $length);
                //$premium_ads = Ad::wherelatitude(floatval($latitude))->wherelongitude(floatval($longitude))->whereStatus(1)->orderBy('id', 'desc')->paginate(20);
                //--$length;
             
             
                            $outputpremium = array();
                                    foreach($premium_ads as $ad){
            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'id'=>$ad->id,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };

                      return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                     'longitude' => $longitude,
                    'latitude' => $latitude,


                    ), 200
            );
                }
            }
            
        
        
        else if(isset($request->longitude)){
            $response = ['error' => 'You have not supplied a latitude or  format you provided is invalid if both the longitude and latitude/latu has been provided then follow the following format \'longitude=15.3235245141254&latitude=-5.235258122536251525\' the order in which the longitude and latitude does not matter as long as/latu the names are lowercase and have the same number of digits'];
        }else if(isset($request->latitude)){
            $response = ['error' => 'You have not supplied a longitude or format you provided is invalid if both the longitude and latitude/latu has been provided then follow the following format \'longitude=15.3235245141254&latitude=-5.235258122536251525\' the order in which the longitude and latitude does not matter as long as/latu the names are lowercase and have the same number of digits'];
        }
        else{
            
            //query database by city
            if(isset($request->city)){
                $city = City::where('city_name', 'LIKE', $request->city)->first();
                if(count($city) == 0){
                    return $response = [
                        'response' => [
                            'error'=> 'city does not exist in database please try another city or add city to database',
                            'solutions' => [ 
                                '1' => 'spell the city correctly ( not case sensitive!)',
                                '2' => 'use the city\'s formal name no ISO CODES, Abbreviations or colloqual names eg, Johannesburg not Joburg or Jozi!)',
                                '3' => 'use the spaces or a dash _ to separate the city names for those cities that have multiple names eg, los_angeles or los angeles not losangeles'
                               ]
                            ],
                        'city'=> $request->city,
                        'request'=> $request,
                        ];
                }
                $ads = Ad::whereCityId($city->id)->orderBy('id', 'desc')->paginate(100);
                if(count($ads) == 0){
                    $response = [
                        'response' => 'no ads in your requested city',
                        'city'=> $city,
                        'request'=> $request,
                        ];
                    }
                else{
                   
                            $outputpremium = array();
                                    foreach($ads as $ad){
            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'agent' => $ad ->feature_agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
         $response = $outputpremium;
         
          return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium
                    ), 200
            );
                           

                }
                 
            }
        }
        
        $outputpremium = array();
         return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium
                    ), 200
            );

                    //return $outputpremium;

    }

    public function premiumPage($page)
    {
        $ads = Ad::where("price_plan", "premium")->orderBy("created_at", "DESC")->skip(10 * ($page - 1))->take(10)->get();
        
        if($ads)
        {
            return Response::json(
                array(
                    'status' => true,
                    'data' => $ads
                    ), 200
            );
        } else
        {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with your Information"
                        ]
                    ), 404
            );
        }
        
    }
    
        public function citiesPage($page)
    {

        $user = DB::table('cities')->where('city_name',$page )->value('id');

        
        if($user)
        
        {
            $limit_premium_ads = 10;
                    $output = array();
                 $city =  Ad::where("city_id",$user )->get();

          foreach($city as $ad){
            array_push($output ,array(
                     'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,

                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,

                    
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);

        
            

          
        } else
        {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with your Information"
                        ]
                    ), 404
            );
        }
        
    }
    
    
    public function agentsPages()
    {
        //$user = DB::table('users')->get();
        $user=  User::where("user_type", "admin")->get();



        
        if($user)
        {
            
               $output = array();

          foreach($user as $ad){
            array_push($output ,array(
                    //'photo' => $ad->get_gravatar,
                    'photo1' => $ad->photo,
                    'email' => $ad->email,
                    'mobile' =>  $ad->mobile,
                    'address' =>   $ad->address,
                    'first_name' => $ad->first_name,
                    'last_name' => $ad->last_name,
                     'name' => $ad->name,
                     'website' => $ad->website,

                    
                         //price' =>   $ad->price,
                         ));
                         
          }
            
                    return Response::json(array('status' => true,'data' =>$output), 200);


          
        } else
        {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with your Information"
                        ]
                    ), 404
            );
        }
        
    }
    
    public function finalSpace()        
    {
        $purpose =Input::get("purpose");
        $city = Input::get("location");
        $bedrooms = Input::get("bedrooms");
        $bathrooms = Input::get("bathrooms");
        $minPrice = Input::get("minPrice");
        $maxPrice =Input::get("maxPrice");
        
        if (!empty(Input::get("location"))) {
            $user = DB::table('cities')->where('city_name',$city )->value('id');

        
        if($user)
        
        {
            
             if (!empty(Input::get("maxPrice")) && !empty(Input::get("minPrice")) && !empty(Input::get("bedrooms"))  && !empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('purpose', '=', $purpose)
                    ->Where('city_id', '=', $user)
                    ->Where('beds', '=', $bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();

        if($ads) {
            
            $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                    'route' => route('single_ad', $ad->slug),
                     'agent' => $ad ->agent,
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
            
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
                    
                      
                    
        }
        
         else if ( !empty(Input::get("bedrooms")) && !empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('purpose', '=', $purpose)
                    ->Where('city_id', '=', $user)
                    ->Where('beds', '=', $bedrooms)
                    ->get();
                    
        if($ads) {
          $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        
                 else if (!empty(Input::get("maxPrice")) && !empty(Input::get("minPrice"))  && !empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('purpose', '=', $purpose)
                    ->Where('city_id', '=', $user)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
                       if($ads) {
          $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        
              else if (!empty(Input::get("maxPrice")) && !empty(Input::get("minPrice"))  && !empty(Input::get("bedrooms"))) {
                 
                      $ads = Ad:: Where('beds', '=', $bedrooms)
                    ->Where('city_id', '=', $user)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
                       if($ads) {
            $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        

           else   if (!empty(Input::get("minPrice")) && !empty(Input::get("maxPrice"))) {
                 
                      $ads = Ad::WhereBetween('price', [$minPrice, $maxPrice])
                      ->where('city_id', '=', $user)
                    ->get();
                       if($ads) {
           $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
       
        
             else   if (!empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('city_id', '=', $user)
                    ->where('purpose', '=', $purpose)
                    ->get();
                    
                       if($ads) {
          $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        
           else   if (!empty(Input::get("bedrooms"))) {
                 
                      $ads = Ad::where('city_id', '=', $user)
                    ->Where('beds', '=', $bedrooms)
                    ->get();
                       if($ads) {
           $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        else  {
       
         $ads = Ad::where('city_id', '=', $user)
                    ->get();
                    
                         if($ads) {
         $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        
        
        }
        }
        
        else{
            
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
            
            
            
        } }
        
        else { 
            
            
            
            if (!empty(Input::get("maxPrice")) && !empty(Input::get("minPrice")) && !empty(Input::get("bedrooms"))  && !empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('purpose', '=', $purpose)
                    ->Where('beds', '=', $bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();

        if($ads) {
           $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
                    
                      
                    
        }
        
         else if ( !empty(Input::get("bedrooms")) && !empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('purpose', '=', $purpose)
                    ->Where('beds', '=', $bedrooms)
                    ->get();
                    
        if($ads) {
            $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        
                 else if (!empty(Input::get("maxPrice")) && !empty(Input::get("minPrice"))  && !empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('purpose', '=', $purpose)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
                       if($ads) {
            $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        
              else if (!empty(Input::get("maxPrice")) && !empty(Input::get("minPrice"))  && !empty(Input::get("bedrooms"))) {
                 
                      $ads = Ad:: Where('beds', '=', $bedrooms)
                    ->WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                    
                       if($ads) {
           $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        

           else   if (!empty(Input::get("minPrice")) && !empty(Input::get("maxPrice"))) {
                 
                      $ads = Ad::WhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
                       if($ads) {
            $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
       
        
             else   if (!empty(Input::get("purpose"))) {
                 
                      $ads = Ad::where('purpose', '=', $purpose)
                    ->get();
                    
                       if($ads) {
           $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        
           else   if (!empty(Input::get("bedrooms"))) {
                 
                      $ads = Ad::Where('beds', '=', $bedrooms)
                    ->get();
                       if($ads) {
           $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
        }
        else  {
       
         $ads = Ad::get();
                    
                         if($ads) {
        $output = array();

          foreach($ads as $ad){
            array_push($output ,array(
                    'media_url' => media_url($ad->feature_img),
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'type' => $ad->type,
                    'square_feet'=> $ad ->square_unit_space,
                    'address' => $ad ->address,
                     'agent' => $ad ->agent,
                    'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
        
        
                  return Response::json(array('status' => true,'data' =>$output), 200);
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
            
            
        }

        }

  
      

 
     
    }
    public function detail($id)
    {
        $premium_ads = Ad::where("id", $id)->get();
        
        if($premium_ads) {
            
                 $outputpremium = array();
                                    foreach($premium_ads as $ad){
            array_push($outputpremium ,array(
                    'media_url' => media_url($ad->feature_img),
                    'gallery_url' => $ad->media_imgs,
                    'amenities' => $ad->amenities,
                    'purpose' =>  $ad->purpose,
                    'title' =>   $ad->title,
                    'bedrooms' => $ad->beds,
                    'type' => $ad->type,
                    'description' => $ad->description,
                    'bathrooms' => $ad ->common_bath,
                    'square_feet'=> $ad ->square_unit_space,
                                        'city' => $ad->city->city_name,
                                                           'created_at'=>$ad->created_at->diffForHumans(),
                                                            'floor' => $ad->floor,
                    'attached_bath' => $ad->attached_bath,
                    'common_bath' => $ad->common_bath,
                    'balcony' => $ad->balcony,
                    'dining_space' => $ad->dining_space,
                    'living_room' => $ad->living_room,
                    'price_plan' => $ad->price_plan,
                    'status' => $ad->status,
                    'video_url' => $ad->video_url,
                    'slug' => $ad->slug,
                    'address' => $ad ->address,
                    'agent' => $ad ->agent,
                    'longitude' => $ad ->longitude,
                    'latitude' => $ad ->latitude,
                     'route' => route('single_ad', $ad->slug),
                         'price' =>   $ad->price,
                         ));
        };
                                                                                                                                                                     
            
            return Response::json(
                array(
                    'status' => true,
                    'data' => $outputpremium,
                    ), 200
            );
        } else {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with you Information"
                        ]
                    ), 404
            );
        }
    }
    
    public function deleteAd(Ad $ad)
    {
        $ad->delete();
        
        return response()->json(null, 204);
    }
    
    public function updateAd(Request $request, Ad $ad)
    {
        $ad->update($request->all());

        return response()->json($ad, 200);
    }
    
    public function createAd(Request $request)
    {
        $ad = Ad::create($request->all());

        return response()->json($ad, 201);
    }
    
    public function getPage($page)
    {
        $ads = Ad::orderBy("id", "DESC")->skip(10 * ($page - 1))->take(10)->get(); 
        if ($ads) {
            return Response::json(
                array(
                        'status' => true,
                        'data' => $ads
                            ), 200
            );
        } else {
            return Response::json(
                array(
                        'error' => [
                            'message' => "Something wrong with your Information"
                        ]
                            ), 404
            );
        }
    }
    public function searchProp(Request $request)
    {
        // rent or sale
        // location
        // bedrooms
        // bathrooms
        // min and max price
        
        $purpose = $request->purpose;
        $city = $request->location;
        $bedrooms = $request->bedrooms;
        $bathrooms = $request->bathrooms;
        $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
        
        $ads = Ad::where('purpose', '=', $purpose)
                    ->orWhere('city_id', '=', $city)
                    ->orWhere('beds', '>', $bedrooms)
                    ->orWhereBetween('price', [$minPrice, $maxPrice])
                    ->get();
        
        // $ads = Ad::where('purpose', '=', 'rent')
        //             ->orWhere('city_id', '=', '42865')
        //             ->orWhere('beds', '>', 2)
        //             ->orWhereBetween('price', [300000, 120000])
        //             ->get();
                    
        if ($ads) {
            return Response::json(
                array(
                        'status' => true,
                        'data' => $ads
                            ), 200
            );
        } else {
            return Response::json(
                array(
                        'error' => [
                            'message' => "Something wrong with your Information"
                        ]
                            ), 404
            );
        }            
    }
    public function search() 
    {
        if (!empty(Input::get("keywords"))) {
            $keywords = Input::get("keywords");
        }


        $type = "";
        if (!empty(Input::get("type"))) {
            $type = Input::get("type");
        }

        $bed = "";
        if (!empty(Input::get("bed"))) {
            $bed = Input::get("bed");
        }
        $bath = "";
        if (!empty(Input::get("bath"))) {
            $bath = Input::get("bath");
        }

        $min = 0;
        if (!empty(Input::get("min"))) {
            $min = Input::get("min");
        }
        $max = 0;
        if (!empty(Input::get("max"))) {
            $max = Input::get("max");
        }

        $query = AD::orderBy("id","DESC");

        if (!empty($keywords)) {
            $query->where('title', 'like', "%$keywords%");
            $query->orWhere('address', 'like', "%$keywords%");
            $query->orWhere('zip', 'like', "%$keywords%");
            $query->orWhere('city', 'like', "%$keywords%");
            $query->orWhere('state', 'like', "%$keywords%");
        }
        $ads = $query->get();
        if ($ads) {
            return Response::json(
                array(
                        'status' => true,
                        'data' => $ads
                            ), 200
            );
        } else {
            return Response::json(
                array(
                      'error' => [
                          'message' => "Something wrong with your Information"
                      ]
                          ), 404
            );
        }
    }
    
    public function agent()
    {
        $agent = User::where("user_type", "admin")->orderBy('id', 'desc')->paginate(20);
        
        if($agent)
        {
            return Response::json(
                array(
                    'status' => true,
                    'data' => $agent
                    ), 200
            );
            //return response()->json($ads);
        } else
        {
            return Response::json(
                array(
                    'error' => [
                        'message' => "Something wrong with your Information"
                        ]
                    ), 404
            );
        }
        
    }
    
    public function searchAds(Request $request)
    {
        
                            return Response::json(
                array(
                        'status' => true,
                        'data' => ["hi"]
                            ), 200
            );
        
        $purpose = $request->purpose;
        $city = $request->location;
        $bedrooms = $request->bedrooms;
        // $bathrooms = $request->bathrooms;
        $minPrice = $request->minPrice;
        $maxPrice = $request->maxPrice;
        
                    return Response::json(
                array(
                        'status' => true,
                        'data' => [$minPrice, $maxPrice]
                            ), 200
            );
        
        $city_id = City::where('city_name', $request->location);
        
        $ads = Ad::WhereBetween('price', [$minPrice, $maxPrice])
                ->where('attached_bath', '<=', $bathrooms)
                ->where('city_id', $city_id->id)
                ->get();
                
        // $ads = Ad::WhereBetween('price', [$minPrice, $maxPrice])
        //         ->where('beds', '<=', $bedrooms)
        //         ->where('attached_bath', '<=', $bathrooms)
        //         ->get();
        
        
        
                    
        if ($ads) {
            return Response::json(
                array(
                        'status' => true,
                        'data' => $ads
                            ), 200
            );
        } else {
            return Response::json(
                array(
                        'error' => [
                            'message' => "Something wrong with your Information"
                        ]
                            ), 404
            );
        }            
    }
}



?>