<?php
namespace App\Http\Controllers\Admin;
use App\Models\Province;
use App\Models\Region;
use App\Models\City;
use App\Models\BuyerRequest;
use App\Models\Bedroom;
use App\Models\Bathroom;
use App\Models\HouseType;
use App\Models\AgeProperty;
use App\Models\SqftProperty;
use App\Models\HeatingFuel;
use App\Models\Amenity;
use App\Models\Parking;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BuyerRequestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
         if($request->ajax()){
             return datatables()->of(BuyerRequest::select('id','user_id', 'province', 'region', 'bedrooms', 'bathrooms', 'type_of_house', 'created_at'))
            ->addColumn('user_name', function($row){
                return $row->users ? '<a href="'.url('/admin/customers/'. $row->user_id . '/edit').'" target="_blank">'.$row->users->name.'</a>': '';
            })
            ->addColumn('view_detail', function($row){
                return '<a class="uk-label" href="'.url('admin/buyer-request/'.$row->id).'">View</a>';
            })
            ->rawColumns(['user_name','view_detail'])->toJson();
        }
        else{
            $this->bladeContent['title']  = 'Buyer Request'; 
            $this->bladeContent['active'] = 'buyer-request';
            return view('admin.buyer_request.index',$this->bladeContent);
        }
    }
    
    public function show($buyerRequestId){
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $this->bladeContent['title']        = 'Buyer Request Details'; 
        $this->bladeContent['active']       = 'buyer-request';
        $this->bladeContent['buyerRequest'] = BuyerRequest::where('id',$buyerRequestId)->first();
        return view('admin.buyer_request.view_detail',$this->bladeContent);
    }
    
    public function edit($id){
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $provinceName                         = BuyerRequest::select('province')->where('id', $id)->first();
        $provinceData                         = Province::select('id')->where('name', $provinceName->province)->first();
        $this->bladeContent['title']          = 'Edit Buyer Request Details'; 
        $this->bladeContent['active']         = 'buyer-request';
        $this->bladeContent['buyerRequest']   = BuyerRequest::where('id',$id)->first();
        $this->bladeContent['provinces']      = [' ' => 'Select Province'] + Province::select('name')->where('status', 1)->orderBy('ordering', 'asc')->pluck('name', 'name')->all();
        $this->bladeContent['regions']        = [' ' => 'Select Region'] + Region::select('name')->where('province_id', $provinceData->id)->orderBy('ordering', 'asc')->pluck('name', 'name')->all();
        $this->bladeContent['cities']         = [' ' => 'Select Region'] + City::select('name')->where('province_id', $provinceData->id)->orderBy('ordering', 'asc')->pluck('name', 'name')->all();
        $this->bladeContent['bedrooms']       = [' ' => 'Select Bedroom'] + Bedroom::select('bedroom')->orderBy('ordering', 'asc')->pluck('bedroom', 'bedroom')->all();
        $this->bladeContent['bathrooms']      = [' ' => 'Select Bathroom'] + Bathroom::select('bathroom')->orderBy('ordering', 'asc')->pluck('bathroom', 'bathroom')->all();
        $this->bladeContent['houseTypes']     = [' ' => 'Select House Type'] + HouseType::select('name')->orderBy('ordering', 'asc')->pluck('name', 'name')->all();
        $this->bladeContent['ageProperties']  = [' ' => 'Select Age Property'] + AgeProperty::select('name')->orderBy('ordering', 'asc')->pluck('name', 'name')->all();
        $this->bladeContent['sqftProperties'] = [' ' => 'Select Sqft Property'] + SqftProperty::select('name')->orderBy('ordering', 'asc')->pluck('name', 'name')->all();
        $this->bladeContent['heatingFuels']   = HeatingFuel::select('name')->orderBy('ordering', 'asc')->get();
        $this->bladeContent['amenities']      = Amenity::select('name')->orderBy('ordering', 'asc')->get();
        $this->bladeContent['parkings']       = Parking::select('name')->orderBy('ordering', 'asc')->get();
        $this->bladeContent['buildingTypes']  = HouseType::select('name')->orderBy('ordering', 'asc')->get();
        return view('admin.buyer_request.edit_details',$this->bladeContent);
    }
    
    public function update(Request $request){
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $buyerId = request('buyer_id');
        
        $buyerRequest                           = BuyerRequest::where('id', $buyerId)->first();
        $buyerRequest->province                 = request('province');
        $buyerRequest->region                   = request('region');
        $buyerRequest->city                     = request('city');
        $buyerRequest->min_price_range          = request('min_price_range');
        $buyerRequest->min_price_tag            = request('min_price_tag');
        $buyerRequest->max_price_range          = request('max_price_range');
        $buyerRequest->max_price_tag            = request('max_price_tag');
        $buyerRequest->bedrooms                 = request('bedroom');
        $buyerRequest->bathrooms                = request('bathroom');
        $buyerRequest->type_of_house            = request('building_type');
        $buyerRequest->parking                  = request('parking');
        $buyerRequest->heating_fuel             = request('heating_fuel');
        $buyerRequest->amenities                = request('amenities');
        $buyerRequest->age_property             = request('age_property');
        $buyerRequest->sqft_property            = request('sqft_property');
        $buyerRequest->preferred_neighbourhoods = request('preferred_neighbourhoods');
        $buyerRequest->other_request            = request('other_request');
        $buyerRequest->financing                = request('financing');
        $buyerRequest->sqft_property            = request('sqft_property');
        $buyerRequest->building_area_tag        = request('building_area_tag');
        $buyerRequest->senior_community         = request('senior_community');
        $buyerRequest->save();
        return redirect()->back()->with('success', 'Buyer details successfully updated.');
    }
    
    public function changeLocation(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $province     = request('province');
        $provinceData = Province::select('id')->where('name',$province)->first();
        $provinceId   = $provinceData->id;
        $locations    = ['' => 'Select Location'] + Location::select('name')->where('province_id', $provinceId)->pluck('name','name')->all();
        $html = '';
        $html .= '  <select name="location" class="uk-select" autocomplete="off">';
                        foreach($locations as $key => $location){
							$html .= '<option value="'.$key.'">'.$location.'</option>';
						}
                           $html .= '</select>';
                    
        return response()->json([
           'success' => 1,
           'html'    => $html
        ]);             
    }
}