<?php
namespace App\Http\Controllers\Admin;
use App\Models\Province;
use App\Models\Region;
use App\Models\City;
use App\Models\SellerRequest;
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 App\Models\RequestImage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SellerRequestController 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(SellerRequest::select('id','user_id', 'province', 'region', 'city', '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/seller-request/'.$row->id).'">View</a>';
            })
            ->rawColumns(['user_name','view_detail'])->toJson();
        }
        else{
            $this->bladeContent['title']  = 'Seller Request'; 
            $this->bladeContent['active'] = 'seller-request';
            return view('admin.seller_request.index',$this->bladeContent);
        }
    }
    
    public function show($sellerRequestId){
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $this->bladeContent['title']         = 'Seller Request Details'; 
        $this->bladeContent['active']        = 'seller-request';
        $this->bladeContent['sellerRequest'] = SellerRequest::where('id',$sellerRequestId)->first();
        $this->bladeContent['sellerImages']  = RequestImage::where('request_id', $sellerRequestId)->get();
        return view('admin.seller_request.view_detail',$this->bladeContent);
    }
    
    public function edit($id){
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $provinceName                         = SellerRequest::select('province')->where('id', $id)->first();
        $provinceData                         = Province::select('id')->where('name', $provinceName->province)->first();
        $this->bladeContent['title']          = 'Edit Seller Request Details'; 
        $this->bladeContent['active']         = 'seller-request';
        $this->bladeContent['sellerRequest']  = SellerRequest::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 City'] + 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.seller_request.edit_details',$this->bladeContent);
    }
    
    public function update(Request $request){
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $sellerId = request('seller_id');

        $sellerRequest                           = SellerRequest::where('id', $sellerId)->first();
        $sellerRequest->province                 = request('province');
        $sellerRequest->region                   = request('region');
        $sellerRequest->city                     = request('city');
        $sellerRequest->sell_price               = request('sell_price');
        $sellerRequest->sell_price_tag           = request('sell_price_tag');
        $sellerRequest->bedrooms                 = request('bedroom');
        $sellerRequest->bathrooms                = request('bathroom');
        $sellerRequest->type_of_house            = request('type_of_house');
        $sellerRequest->parking                  = request('parking');
        $sellerRequest->heating_fuel             = request('heating_fuel');
        $sellerRequest->amenities                = request('amenities');
        $sellerRequest->age_property             = request('age_property');
        $sellerRequest->sqft_property            = request('sqft_property');
        $sellerRequest->preferred_neighbourhoods = request('preferred_neighbourhoods');
        $sellerRequest->other_request            = request('other_request');
        $sellerRequest->senior_community         = request('senior_community');
        $sellerRequest->building_area_tag        = request('building_area_tag');
        $sellerRequest->suite                    = request('suite');
        $sellerRequest->suite_option             = request('suite_option');
        $sellerRequest->buying_new_home          = request('buying_new_home');
        $sellerRequest->already_buy_request      = request('already_buy_request');
        $sellerRequest->currently_listed_realtor = request('currently_listed_realtor');
        $sellerRequest->save();
        return redirect()->back()->with('success', 'Seller details successfully updated.');
    }
    
    public function sellerChangeCity(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $region     = request('region');
        $regionData = Region::select('id')->where('name',$region)->first();
        $regionId   = $regionData->id;
        $cities     = ['' => 'Select City'] + City::select('name')->where('region_id', $regionId)->pluck('name','name')->all();
        $html = '';
        $html .= '  <select name="city" class="uk-select" autocomplete="off">';
                        foreach($cities as $key => $city){
							$html .= '<option value="'.$key.'">'.$city.'</option>';
						}
                           $html .= '</select>';
                    
        return response()->json([
           'success' => 1,
           'html'    => $html
        ]);             
    }
    
    public function removeImage(Request $request){
     
        $id        = request('id');
        $userId    = request('user_id');
        $requestId = request('request_id');
        
        $requestImage   = RequestImage::where('id', $id)->first();
        
        $fileName  = $requestImage->filename;
        
        $requestImage->delete();
        
        $path      = storage_path('request_images/'.$userId.'/'.$requestId.'/'.$fileName);
        
        if(file_exists($path)){
            unlink($path);
        }
        
        return response()->json([
            'success'  => 1,
            'message' => 'Image successfully removed',
        ]);
        

    }
}