<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Homepage;
use GuzzleHttp\Client;
use App\Locater;
use App\Page;
use DB;
use Validator;
use Illuminate\Support\Facades\Hash;

class SearchController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
        $this->record_per_page=10;
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {
        $client = new Client();
        if(!empty($request->locality)){
            $response = $client->request('GET', 'https://anitadongre.concr.in/api/v1/locations/list?keyword='.$request->locality.'&brandId=',[ 'headers' => [
                'Accept'     => 'application/json',
                'Authorization'      => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjdlNDUxMWRiZjg2NDgwMDk5MmE3ZjE4MDk2ZDUwOTY1ZjJmMDNmMGVjOGYzYzhjMTQwNGRkOTZmZGUxY2RmNjllZWY3OGE4ZmU3MTIzYTU0In0.eyJhdWQiOiIxIiwianRpIjoiN2U0NTExZGJmODY0ODAwOTkyYTdmMTgwOTZkNTA5NjVmMmYwM2YwZWM4ZjNjOGMxNDA0ZGQ5NmZkZTFjZGY2OWVlZjc4YThmZTcxMjNhNTQiLCJpYXQiOjE1NjIxNDYyOTUsIm5iZiI6MTU2MjE0NjI5NSwiZXhwIjoxNTYzNDQyMjk1LCJzdWIiOiIiLCJzY29wZXMiOlsicmVhZC1sb2NhdGlvbiJdfQ.RcGybPTQUPHMAA5y7nclG01hgaa7e8imF6x0q0SR9PqIMFQ9yVjXJCu4Hx4i3-AwVv27FE6RQ84-EpzPO1XmCHpuqLg6F4OWyn8Xv6pceLSxAzSKvrsYeS2W1uqCxFxeBaQmx4Uq5xdf0wQ9Ao-3914XrFTnWMYh1Rd94o4mPmlyeBB8H2e1nIM4Vtuh98-oDMxclhh_1A1XXUOe7L4TILqBMjyQlaRIDBTnVdA_fNYK7ZPWDy6j-t_yQpf4NOXWXGbldpZ0KAu2Fb1llsnizE0Ryd310z1zUZPtgbqdkC7mCBpXQf9wmE3GUihLj84Vyh7Q-dnKZw0HlZDdRWasFxTpss0wcEDDcLM1u7Sgl2wT83Sq7h61hfShhM18fqHwQL5TKKDyQ3pv6z4-AVhcwV5IOPMJPyZLfrydezTEn6cNf8q-gAvTMFqayQEoKLtMlXWQ1Oy81UQIo9_3LTGaKrS2qz8lNNY132qFaXb6NwRsv73MWb1DkZdEBj9CoV5UncYeCVDHephaAQxKZva5HGoi1-Gvd2ltY_S64zuOZORpcyCMHwyTHKOc8w7dGrJpT1uXHDtF73KB5XXNFRu66HWS0V1WniBtxYzB8cG_eDlU_Ak9QnywlXTcIkq10pbnad7hnsRdmSDL3ksA7yVLMAZ9MAyR-T7giAw_BTslM5I']
            ]);
        }else{
            $response = $client->request('GET', 'https://anitadongre.concr.in/api/v1/locations/list?keyword=',[ 'headers' => [
                'Accept'     => 'application/json',
                'Authorization'      => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjdlNDUxMWRiZjg2NDgwMDk5MmE3ZjE4MDk2ZDUwOTY1ZjJmMDNmMGVjOGYzYzhjMTQwNGRkOTZmZGUxY2RmNjllZWY3OGE4ZmU3MTIzYTU0In0.eyJhdWQiOiIxIiwianRpIjoiN2U0NTExZGJmODY0ODAwOTkyYTdmMTgwOTZkNTA5NjVmMmYwM2YwZWM4ZjNjOGMxNDA0ZGQ5NmZkZTFjZGY2OWVlZjc4YThmZTcxMjNhNTQiLCJpYXQiOjE1NjIxNDYyOTUsIm5iZiI6MTU2MjE0NjI5NSwiZXhwIjoxNTYzNDQyMjk1LCJzdWIiOiIiLCJzY29wZXMiOlsicmVhZC1sb2NhdGlvbiJdfQ.RcGybPTQUPHMAA5y7nclG01hgaa7e8imF6x0q0SR9PqIMFQ9yVjXJCu4Hx4i3-AwVv27FE6RQ84-EpzPO1XmCHpuqLg6F4OWyn8Xv6pceLSxAzSKvrsYeS2W1uqCxFxeBaQmx4Uq5xdf0wQ9Ao-3914XrFTnWMYh1Rd94o4mPmlyeBB8H2e1nIM4Vtuh98-oDMxclhh_1A1XXUOe7L4TILqBMjyQlaRIDBTnVdA_fNYK7ZPWDy6j-t_yQpf4NOXWXGbldpZ0KAu2Fb1llsnizE0Ryd310z1zUZPtgbqdkC7mCBpXQf9wmE3GUihLj84Vyh7Q-dnKZw0HlZDdRWasFxTpss0wcEDDcLM1u7Sgl2wT83Sq7h61hfShhM18fqHwQL5TKKDyQ3pv6z4-AVhcwV5IOPMJPyZLfrydezTEn6cNf8q-gAvTMFqayQEoKLtMlXWQ1Oy81UQIo9_3LTGaKrS2qz8lNNY132qFaXb6NwRsv73MWb1DkZdEBj9CoV5UncYeCVDHephaAQxKZva5HGoi1-Gvd2ltY_S64zuOZORpcyCMHwyTHKOc8w7dGrJpT1uXHDtF73KB5XXNFRu66HWS0V1WniBtxYzB8cG_eDlU_Ak9QnywlXTcIkq10pbnad7hnsRdmSDL3ksA7yVLMAZ9MAyR-T7giAw_BTslM5I']
            ]);
        }
        
    	$statusCode = $response->getStatusCode();
        $body = $response->getBody()->getContents();
        $locatorApiData = json_decode($body);
        $locaterkeyvalueddata = Locater::where('status', '=', '1')->with('page')->get()->pluck('page.slug', 'locater_id_url');
        //echo"<pre>";
        //print_r($locaterkeyvalueddata);die;
        
        /*$locatorDBdata = DB::table('locaters')
                    ->join('pages', 'locaters.id', '=', 'pages.locater_id')
                    ->select('locaters.locater_id_url', 'pages.slug')
                    ->where('locaters.status', '1')
                    ->get();
        $locaterkeyvalueddata = array();
        foreach($locatorDBdata as $val){
            $locaterkeyvalueddata[$val->locater_id_url] = $val->slug;
        }*/
                 
        $homepage = Homepage::where('homepages.status', '=', '1')->first();
        return view('frontend.searchs.index', ['homepage' => $homepage,'locatorApiData'=>$locatorApiData,'locaterkeyvalueddata'=>$locaterkeyvalueddata]);
    }

    public function detail(Request $request, $slug = '') {
        $pageData = Page::where('pages.slug', '=', $slug)->with('locaterlists')->first();
        
        /*Fetch Locater ID*/ 
        $locatorID = $pageData->locaterlists->locater_id_url;
        $client = new Client();
        $response = $client->request('GET', 'https://anitadongre.concr.in/api/v1/locations/'.$locatorID,[ 'headers' => [
            'Accept'     => 'application/json',
            'Authorization'      => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjdlNDUxMWRiZjg2NDgwMDk5MmE3ZjE4MDk2ZDUwOTY1ZjJmMDNmMGVjOGYzYzhjMTQwNGRkOTZmZGUxY2RmNjllZWY3OGE4ZmU3MTIzYTU0In0.eyJhdWQiOiIxIiwianRpIjoiN2U0NTExZGJmODY0ODAwOTkyYTdmMTgwOTZkNTA5NjVmMmYwM2YwZWM4ZjNjOGMxNDA0ZGQ5NmZkZTFjZGY2OWVlZjc4YThmZTcxMjNhNTQiLCJpYXQiOjE1NjIxNDYyOTUsIm5iZiI6MTU2MjE0NjI5NSwiZXhwIjoxNTYzNDQyMjk1LCJzdWIiOiIiLCJzY29wZXMiOlsicmVhZC1sb2NhdGlvbiJdfQ.RcGybPTQUPHMAA5y7nclG01hgaa7e8imF6x0q0SR9PqIMFQ9yVjXJCu4Hx4i3-AwVv27FE6RQ84-EpzPO1XmCHpuqLg6F4OWyn8Xv6pceLSxAzSKvrsYeS2W1uqCxFxeBaQmx4Uq5xdf0wQ9Ao-3914XrFTnWMYh1Rd94o4mPmlyeBB8H2e1nIM4Vtuh98-oDMxclhh_1A1XXUOe7L4TILqBMjyQlaRIDBTnVdA_fNYK7ZPWDy6j-t_yQpf4NOXWXGbldpZ0KAu2Fb1llsnizE0Ryd310z1zUZPtgbqdkC7mCBpXQf9wmE3GUihLj84Vyh7Q-dnKZw0HlZDdRWasFxTpss0wcEDDcLM1u7Sgl2wT83Sq7h61hfShhM18fqHwQL5TKKDyQ3pv6z4-AVhcwV5IOPMJPyZLfrydezTEn6cNf8q-gAvTMFqayQEoKLtMlXWQ1Oy81UQIo9_3LTGaKrS2qz8lNNY132qFaXb6NwRsv73MWb1DkZdEBj9CoV5UncYeCVDHephaAQxKZva5HGoi1-Gvd2ltY_S64zuOZORpcyCMHwyTHKOc8w7dGrJpT1uXHDtF73KB5XXNFRu66HWS0V1WniBtxYzB8cG_eDlU_Ak9QnywlXTcIkq10pbnad7hnsRdmSDL3ksA7yVLMAZ9MAyR-T7giAw_BTslM5I']
        ]);
    	$statusCode = $response->getStatusCode();
        $body = $response->getBody()->getContents();
        $locationDeatilAPIdata = json_decode($body);
        //echo"<pre>";
        //print_r($locationDeatilAPIdata);
        //die;
        
        $homepage = Homepage::where('homepages.status', '=', '1')->first();
        return view('frontend.searchs.detail', ['homepage' => $homepage,'locationDeatilAPIdata'=>$locationDeatilAPIdata, 'pageData' => $pageData]);
    }

    public function autocomplete(Request $request)
    {
        $client = new Client();
        $response = $client->request('GET', 'https://anitadongre.concr.in/api/v1/locations/search?keyword='.$request['query'],[ 'headers' => [
            'Accept'     => 'application/json',
            'Authorization'      => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjdlNDUxMWRiZjg2NDgwMDk5MmE3ZjE4MDk2ZDUwOTY1ZjJmMDNmMGVjOGYzYzhjMTQwNGRkOTZmZGUxY2RmNjllZWY3OGE4ZmU3MTIzYTU0In0.eyJhdWQiOiIxIiwianRpIjoiN2U0NTExZGJmODY0ODAwOTkyYTdmMTgwOTZkNTA5NjVmMmYwM2YwZWM4ZjNjOGMxNDA0ZGQ5NmZkZTFjZGY2OWVlZjc4YThmZTcxMjNhNTQiLCJpYXQiOjE1NjIxNDYyOTUsIm5iZiI6MTU2MjE0NjI5NSwiZXhwIjoxNTYzNDQyMjk1LCJzdWIiOiIiLCJzY29wZXMiOlsicmVhZC1sb2NhdGlvbiJdfQ.RcGybPTQUPHMAA5y7nclG01hgaa7e8imF6x0q0SR9PqIMFQ9yVjXJCu4Hx4i3-AwVv27FE6RQ84-EpzPO1XmCHpuqLg6F4OWyn8Xv6pceLSxAzSKvrsYeS2W1uqCxFxeBaQmx4Uq5xdf0wQ9Ao-3914XrFTnWMYh1Rd94o4mPmlyeBB8H2e1nIM4Vtuh98-oDMxclhh_1A1XXUOe7L4TILqBMjyQlaRIDBTnVdA_fNYK7ZPWDy6j-t_yQpf4NOXWXGbldpZ0KAu2Fb1llsnizE0Ryd310z1zUZPtgbqdkC7mCBpXQf9wmE3GUihLj84Vyh7Q-dnKZw0HlZDdRWasFxTpss0wcEDDcLM1u7Sgl2wT83Sq7h61hfShhM18fqHwQL5TKKDyQ3pv6z4-AVhcwV5IOPMJPyZLfrydezTEn6cNf8q-gAvTMFqayQEoKLtMlXWQ1Oy81UQIo9_3LTGaKrS2qz8lNNY132qFaXb6NwRsv73MWb1DkZdEBj9CoV5UncYeCVDHephaAQxKZva5HGoi1-Gvd2ltY_S64zuOZORpcyCMHwyTHKOc8w7dGrJpT1uXHDtF73KB5XXNFRu66HWS0V1WniBtxYzB8cG_eDlU_Ak9QnywlXTcIkq10pbnad7hnsRdmSDL3ksA7yVLMAZ9MAyR-T7giAw_BTslM5I']
        ]);
    	$statusCode = $response->getStatusCode();
        $body = $response->getBody()->getContents();
        $locationlist = json_decode($body);
        $output = '<ul class="dropdown-menu" style="display:block; position:relative">';
        foreach($locationlist->data as $row){
            $output .= '<li><i class="mdi mdi-map-marker"></i><a href="#">'.$row->name.'</a></li>';
        }
        $output .= '</ul>';
        echo $output;
        //return response()->json($data);
    }
    
}
