<?php

namespace App\Traits;

use App\Models\Project\Order;
use App\Models\Project\Vendor_detail;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;



trait NearByTrait
{
    public function nearByInLocation($expense_type, $lat, $lng, $radius=6)
    {
        /*
        * using eloquent approach, make sure to replace the "Restaurant" with your actual model name
        * replace 6371000 with 6371 for kilometer and 3956 for miles
        */
        // $data = Vendor_detail::selectRaw("name, lisence_no, ref_no, insta_link, twitter_link, website, whatsapp, mobile, location_lat, location_lng, location, logo, 
        // (3956 * ACOS(COS(RADIANS(location_lat)) * COS( RADIANS(".$lat.")) * COS(RADIANS(".$lng.") - RADIANS(location_lng)) + SIN(RADIANS(location_lat) ) * SIN(RADIANS(".$lat.")))) AS distance", [$lat, $lng, $lat])
        // ->where('vendor_status', '=', 1)
        // ->where('type', '=', $expense_type)
        // ->having("distance", "<", $radius)
        // ->orderBy("distance",'asc')
        // ->get();
        if($expense_type != ""):
            $sql = 'SELECT *, (6371 * ACOS(COS(RADIANS(location_lat)) * COS(RADIANS('.$lat.') ) * COS(RADIANS('.$lng.') -RADIANS(location_lng)) + SIN(RADIANS(location_lat)) * SIN(RADIANS('.$lat.')))) AS distance FROM vendor_detail WHERE type='.$expense_type.' AND vendor_status = 1 HAVING distance <= '.$radius.' ORDER BY distance ASC';
            $data = DB::select($sql);

        else:
            $sql = 'SELECT *, (6371 * ACOS(COS(RADIANS(location_lat)) * COS(RADIANS('.$lat.') ) * COS(RADIANS('.$lng.') -RADIANS(location_lng)) + SIN(RADIANS(location_lat)) * SIN(RADIANS('.$lat.')))) AS distance FROM vendor_detail WHERE vendor_status = 1 HAVING distance <= '.$radius.' ORDER BY distance ASC';
            $data = DB::select($sql);
        endif;
        return $data;
    }
}