@extends('layouts.app')
@section('content')
<div class="main-header">
    <div class="admin-header">
        <div class="uk-grid">
            <div class="uk-width-auto">
                <h1 class="uk-margin-small-top main-heading">{{$title}} ({{'#B'.$buyerRequest->id}})</h1>
            </div>
            <div class="uk-width-expand uk-text-right">
                <a class="uk-button uk-button-default" href="{{url('admin/buyer-request/'.$buyerRequest->id)}}"> Back</a>
            </div>
        </div>
    </div>
</div>
<div class="uk-margin uk-width-1-1 uk-margin-auto uk-card uk-card-default uk-card-body small-padding">
    @include('partials.messages')
	{{ Form::open(array('route' => 'admin.buyer-request.update', 'method' => 'POST', 'class' => 'uk-form-stacked','id' => 'edit_form')) }}
    @csrf
    {{ Form::hidden('buyer_id', $buyerRequest->id) }}
    <div uk-grid>
    	<div class="uk-width-1-2@m">  
            <div class="uk-margin-small">
                <label for="province" class="uk-form-label">Province </label>
                <div class="uk-form-controls">
                    {{Form::select('province', $provinces, $buyerRequest->province,['class' => 'uk-select change-service-area','autocomplete' => 'off'])}}
                </div>    
            </div>
            <div class="uk-margin-small">
                <label for="region" class="uk-form-label">Region </label>
                <div class="uk-form-controls region-dropdown">
                    {{Form::select('region', $regions, $buyerRequest->region,['class' => 'uk-select change-region','autocomplete' => 'off'])}}    
                </div>
            </div> 
            @php
                
                if($buyerRequest->city){
                    $cityList = json_decode($buyerRequest->city, true);
                }else{
                    $cityList = [];
                }
                
            @endphp
            <div class="uk-margin-small">
                <label for="city" class="uk-form-label">City </label>
                <div class="uk-form-controls city-dropdown">
                    <select name="city[]" class="uk-select" id="multipleCity" autocomplete="off" multiple="">
                        @if(count($cities) > 0)
                            <option value="">Select City</option>
                            @foreach($cities as $key => $value)
                                @if(in_array($key,$cityList))
                                    <option value="{{$key}}" selected>{{$value}}</option>
                                @else
                                    <option value="{{$key}}">{{$value}}</option>
                                @endif
                                
                            @endforeach
                        @else
                            <option value="">Select City</option>
                        @endif
                    </select>
                </div>
            </div>  
            <div class="uk-grid-small" uk-grid>
                <div class="uk-width-expand@m">
                    <div class="uk-margin-small">
                        <label for="min_price_range" class="uk-form-label">Min Price Range </label>
                        <div class="uk-form-controls">
                            {{Form::text('min_price_range', $buyerRequest->min_price_range,['class' => 'uk-input','autocomplete' => 'off'])}}
                        </div>    
                    </div>
                </div>
                <div class="uk-width-1-6@m">
                    <div class="uk-margin-small">
                        <label for="min_price_tag" class="uk-form-label">Min Price Tag </label>
                        <div class="uk-form-controls">
                            {{Form::select('min_price_tag', Config::get('enums.priceTag'), $buyerRequest->min_price_tag,['class' => 'uk-select','autocomplete' => 'off'])}}
                        </div>    
                    </div>
                </div>
                <div class="uk-width-expand@m">
                    <div class="uk-margin-small">
                        <label for="max_price_range" class="uk-form-label">Max Price Range </label>
                        <div class="uk-form-controls">
                            {{Form::text('max_price_range', $buyerRequest->max_price_range,['class' => 'uk-input','autocomplete' => 'off'])}}
                        </div>    
                    </div>
                </div>
                <div class="uk-width-1-6@m">
                    <div class="uk-margin-small">
                        <label for="max_price_tag" class="uk-form-label">Max Price Tag </label>
                        <div class="uk-form-controls">
                            {{Form::select('max_price_tag', Config::get('enums.priceTag'), $buyerRequest->max_price_tag,['class' => 'uk-select','autocomplete' => 'off'])}}
                        </div>    
                    </div>
                </div>
            </div>
            <div class="uk-margin-small">
                <div class="uk-grid-small" uk-grid>
                    <div class="uk-width-1-2@m">
                        <div class="uk-margin-small">
                            <label for="bedroom" class="uk-form-label">Bedrooms </label>
                            <div class="uk-form-controls">
                                {{Form::select('bedroom', $bedrooms, $buyerRequest->bedrooms, ['class' => 'uk-select','autocomplete' => 'off'])}}
                            </div>    
                        </div>
                    </div>
                    <div class="uk-width-1-2@m">
                        <div class="uk-margin-small">
                            <label for="bathroom" class="uk-form-label">Bathrooms </label>
                            <div class="uk-form-controls">
                                {{Form::select('bathroom', $bathrooms, $buyerRequest->bathrooms,['class' => 'uk-select','autocomplete' => 'off'])}}
                            </div>    
                        </div>    
                    </div>
                </div>
            </div>
            @php
                if($buyerRequest->parking){
                    $parkingData = json_decode($buyerRequest->parking, true);
                }else{
                    $parkingData = [];
                }
                
                if($buyerRequest->heating_fuel){
                    $heatingFuelData = json_decode($buyerRequest->heating_fuel, true);
                }else{
                    $heatingFuelData = [];
                }
                
                if($buyerRequest->amenities){
                    $amenitiesData = json_decode($buyerRequest->amenities, true);
                }else{
                    $amenitiesData = [];
                }
                
                if($buyerRequest->type_of_house){
                    $buildingData = json_decode($buyerRequest->type_of_house, true);
                }else{
                    $buildingData = [];
                }
            @endphp
            <div class="uk-margin-small">
                <div class="uk-grid-small" uk-grid>
                    <div class="uk-width-1-2@m">
                        <div class="uk-margin-small">
                            <label for="sqft_property" class="uk-form-label">Building Area </label>
                            <div class="uk-form-controls">
                                {{Form::text('sqft_property', $buyerRequest->sqft_property,['class' => 'uk-input','autocomplete' => 'off'])}}
                            </div>    
                        </div>    
                    </div>
                    <div class="uk-width-1-2@m">
                        <div class="uk-margin-small">
                            <label for="building_area_tag" class="uk-form-label">Building Area Tag</label>
                            <div class="uk-form-controls">
                                {{Form::select('building_area_tag', Config::get('enums.buildingAreaTag'), $buyerRequest->building_area_tag,['class' => 'uk-select','autocomplete' => 'off'])}}
                            </div>    
                        </div>  
                    </div>
                    <div class="uk-width-1-2@m">
                        <div class="uk-margin">
                            <label for="financing" class="uk-form-label">Financing </label>
                            <div class="uk-grid-small uk-child-width-auto uk-grid">
                                <label>{{Form::radio('financing', 'Cash', ($buyerRequest->financing == 'Cash' ? true : ''), ['class' => 'uk-radio'])}} Cash</label>
                                <label>{{Form::radio('financing', 'Mortgage(preapproved)', ($buyerRequest->financing == 'Mortgage (preapproved)' ? true : ''), ['class' => 'uk-radio'])}} Mortgage(preapproved)</label>
                            </div>
                        </div> 
                    </div>
                    <div class="uk-width-1-2@m">
                        <div class="uk-margin">
                            <label for="senior_community" class="uk-form-label">Senior Community </label>
                            <div class="uk-grid-small uk-child-width-auto uk-grid">
                                <label>{{Form::radio('senior_community', 1, ($buyerRequest->senior_community == 1 ? true : ''), ['class' => 'uk-radio'])}} Yes</label>
                                <label>{{Form::radio('senior_community', 0, ($buyerRequest->senior_community == 0 ? true : ''), ['class' => 'uk-radio'])}} No</label>
                            </div>
                        </div> 
                    </div>
                </div>
            </div>
            <div uk-grid>
                <div class="uk-width-1-2@m">
                    <div class="uk-margin-small">
                        <label for="building_type" class="uk-form-label">Building Type </label>
                        @foreach($buildingTypes as $buildingType)
                        <div class="uk-form-controls">
                            <label>{{Form::checkbox('building_type[]', $buildingType->name, is_array($buildingData) && in_array($buildingType->name, @$buildingData),['class' => 'uk-checkbox','autocomplete' => 'off'])}} {{$buildingType->name}}</label>
                        </div>  
                        @endforeach
                    </div>
                </div>
                <div class="uk-width-1-2@m">
                    <div class="uk-margin-small">
                        <label for="parking" class="uk-form-label">Parking </label>
                        @foreach($parkings as $parking)
                        <div class="uk-form-controls">
                            <label>{{Form::checkbox('parking[]', $parking->name, is_array($parkingData) && in_array($parking->name, @$parkingData),['class' => 'uk-checkbox','autocomplete' => 'off'])}} {{$parking->name}}</label>
                        </div>  
                        @endforeach
                    </div>
                </div>
            </div>
        </div>
        <div class="uk-width-1-2@m">
            <div class="uk-margin-small">
                <label for="age_property" class="uk-form-label">Age Property </label>
                <div class="uk-form-controls">
                    {{Form::select('age_property', $ageProperties, $buyerRequest->age_property,['class' => 'uk-select','autocomplete' => 'off'])}}
                </div>    
            </div>
            <div class="uk-margin-small">
                <label for="preferred_neighbourhoods" class="uk-form-label">Preferred Neighbourhoods </label>
                <div class="uk-form-controls">
                    {{Form::textarea('preferred_neighbourhoods', $buyerRequest->preferred_neighbourhoods,['class' => 'uk-textarea','autocomplete' => 'off', 'rows' => '4'])}}
                </div>    
            </div>
            <div class="uk-margin-small">
                <label for="other_request" class="uk-form-label">Other Information / Features Required</label>
                <div class="uk-form-controls">
                    {{Form::textarea('other_request', $buyerRequest->other_request,['class' => 'uk-textarea','autocomplete' => 'off', 'rows' => '4'])}}
                </div>    
            </div>
            <div class="uk-grid" uk-grid>
                <div class="uk-width-1-2@m">
                    <div class="uk-margin-small">
                        <label for="amenities" class="uk-form-label">Amenities WishList</label>
                        @foreach($amenities as $amenity)
                        <div class="uk-form-controls">
                            <label>{{Form::checkbox('amenities[]', $amenity->name, is_array($amenitiesData) && in_array($amenity->name, @$amenitiesData),['class' => 'uk-checkbox','autocomplete' => 'off'])}} {{$amenity->name}}</label>
                        </div>    
                        @endforeach
                    </div>
                </div>
                <div class="uk-width-1-2@m">
                    <div class="uk-margin-small">
                        <label for="heating_fuel" class="uk-form-label">Heating and Cooling </label>
                        @foreach($heatingFuels as $heatingFuel)
                        <div class="uk-form-controls">
                            <label>{{Form::checkbox('heating_fuel[]', $heatingFuel->name, is_array($heatingFuelData) && in_array($heatingFuel->name, @$heatingFuelData),['class' => 'uk-checkbox','autocomplete' => 'off'])}} {{$heatingFuel->name}}</label>
                        </div>    
                        @endforeach
                    </div>
                </div>    
            </div>
        </div>
    </div>    
    <div class="uk-margin">
        <div class="uk-text-center">
            {{ Form::submit('Update', array('class' => 'uk-button uk-button-primary','name' => 'save', 'id' => 'edit_submit')) }}
            <a class="uk-button uk-button-default" href="{{url('admin/buyer-request/'.$buyerRequest->id)}}"> Cancel</a>
        </div>
    </div>
    {{ Form::close() }}
</div>    
@endsection
@section('footer-script')
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
 <script>
 
    $(document).on('change','.change-service-area',function(){
        var province = $(this).val();

        $.ajax(
        {
            url: url + "/admin/select-region",
            type: 'POST',
            dataType: "JSON",
            data: {
                'province': province,
            },
            success: function (res)
            {
                if(res.success == 1){
                    $('.region-dropdown').html(res.html);
                }
            },
            error: function () {
                UIkit.modal.alert('Oops there was an error! Please try again.');
            }
        });
    });
    
    $('#multipleCity').select2({ width: '100%' });    
    
    $(document).on('change','.change-region',function(){
        var region = $(this).val();

        $.ajax(

        {
            url: url + "/admin/select-city",
            type: 'POST',
            dataType: "JSON",
            data: {
                'region': region,
            },
            success: function (res)
            {
                if(res.success == 1){
                    $('.city-dropdown').html(res.html);
                    $('#multipleCity').select2();
                }
            },
            error: function () {
                UIkit.modal.alert('Oops there was an error! Please try again.');
            }
        });
    });
    
    $("#edit_form").validate({
        submitHandler: function(form) {
            $('#edit_submit').attr('disabled',true);
            form.submit();
        }
    });
    
    $(document).on('change','.change-province',function(){
        var province = $(this).val();

        $.ajax(
        {
            url: url + "/admin/buyer-change-location",
            type: 'POST',
            dataType: "JSON",
            data: {
                'province': province,
            },
            success: function (res)
            {
                if(res.success == 1){
                    $('.location-dropdown').html(res.html);
                }
            },
            error: function () {
                UIkit.modal.alert('Oops there was an error! Please try again.');
            }
        });
    });
    
</script>
@endsection