@include('messages')
<?php
$roles = Config::get('params.roles');
$required = '';
//d($roles,1);
//$currency = $currencies[Config::get('params.currency_default')]['symbol'];
?>

<div class="row">
    <div class="col-sm-6">
        <div class="form-group">
            {!! Form::label('First Name') !!}
            {!! Form::text('firstName', null, ['class' => 'form-control', $required]) !!}
        </div>
    </div>


    <div class="col-sm-6">
        <div class="form-group">
            {!! Form::label('Last Name') !!}
            {!! Form::text('lastName', null, ['class' => 'form-control', $required]) !!}
        </div>
    </div>
</div>

<div class="row">
    <div class="col-sm-6">
        <div class="form-group">
            {!! Form::label('Email') !!}
            {!! Form::text('email', null, ['class' => 'form-control', $required]) !!}
        </div>
    </div>

    <div class="col-sm-6">
        <div class="form-group">
            {!! Form::label('Phone') !!}
            {!! Form::text('phone', null, ['class' => 'form-control', $required]) !!}
        </div>
    </div>
</div>
<div class="row">
    <div class="col-sm-6">
        <div class="form-group">
            {!! Form::label('Password') !!}
            {!! Form::password('password', ['placeholder' => 'Password', 'class' => 'form-control']) !!}
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <p class="mb-0">Type</p>
            <select name="type" id="type" class="form-control">
                <option value="1" {{ $model->type == 1 ? 'selected' : '' }}>Home Owner</option>
                <option value="2"{{ $model->type == 2 ? 'selected' : '' }}>Landlord</option>

            </select>
        </div>
    </div>
</div>


<div class="row">
    <div class="col-sm-6 form-group">
        <div class="form-group">

            <p class="mb-0">Post Code</p>
            <input type="text" name="postcode_lookup" id="postcode_lookup" class="form-control"
                aria-label="Input group example" aria-describedby="btnGroupAddon"
                value="{{ $model->address->postcode }}">
        </div>
    </div>
    <div class="col-md-6 form-group custom-select2" style="" id="div_addresses">
        <label>Select Your Address</label>
        <select id="addresses" name="addresses" class="custom-select"></select>
    </div>




    <div class="col-md-12 form-group">
        {{-- <button type="button" class="btn btn-lg" id="btnAddressManually">Or Enter Address Manually</button> --}}
        <div class="row address-manually">
            <div class="col-md-6 form-group">
                <label class="font-weight-bold">Address Line</label>
                <input type="text" name="address" id="address" class="form-control"
                    value="{{ $model->address->address1 }}">
            </div>
            <div class="col-md-6 form-group">
                <label class="font-weight-bold">Address Line 2</label>
                <input type="text" name="address2" id="address2" class="form-control"
                    value="{{ $model->address->address2 }}">
            </div>
            <div class="col-md-6 form-group">
                <label class="font-weight-bold">City</label>
                <input type="text" name="city" id="city"
                    class="form-control"value="{{ $model->address->city }}">
            </div>
            <div class="col-md-6 form-group">
                <label class="font-weight-bold">Postcode</label>
                <input type="text" name="postcode" id="postcode" value="{{ $model->address->postcode }}"
                    class="form-control">
            </div>
        </div>
    </div>

</div>





<div class="row">
    <div class="col-sm-2">
        <div class="d-flex justify-content-start flex-wrap">
            <div class="custom-control custom-switch custom-switch-success mr-2 mb-1">
                <p class="mb-0">Status</p>
                <input type="checkbox" @if (isset($model->status) && $model->status == 'Active') checked @endif class="custom-control-input"
                    name="status" id="status" value="1">
                <label class="custom-control-label" for="status">Status</label>
            </div>
        </div>
    </div>
</div>
<br clear="all" />
<div class="row">
    <div class="col-md-8">
        <button type="submit" class="btn btn-primary mr-1 mb-1 waves-effect waves-light">Save</button>
        <a href="{{ url('admin/customers') }}" class="btn btn-outline-warning mr-1 mb-1 waves-effect waves-light">Cancel</a>
    </div>
</div>
@section('myscript')
    <script>
        $(document).ready(function() {

            $("#div_addresses").hide();
            $(document).on("keyup","#postcode_lookup", function() {

                var optionHtml ="";
                $('#addresses')
                    .find('option')
                    .remove()
                    .end();


                var postcode = $("#postcode_lookup").val();

                if (postcode.length < 5) {
                    return false;
                }

                $("#div_addresses").show();

                var type ="GET";
                var url = '{{ url('address') }}/' + postcode;
                var dataType ="html";

                $.ajax({
                    url: url,
                    type: type,

                    success: function(response) {
                        if (response.success == true) {
                            if (response.addresses.length > 0) {

                                optionHtml =
                                   "<option value='' >Please select your address</option>";

                                $('#addresses').append(optionHtml);
                                $('#postcode').val($('#postcode_lookup').val());

                                jQuery.each(response.addresses, function() {

                                    $('#city').val(this.city);

                                    optionHtml ="<option value='" + this.address +
                                       "' >" + this.address +"</option>";
                                    $('#addresses').append(optionHtml);
                                });
                                optionHtml ="<option value=''> Not on the list? </option>";
                                $('#addresses').append(optionHtml);

                            } else {
                                optionHtml +="<option value=''>Address not found...</option>";
                                $('#addresses').append(optionHtml);
                            }
                        } else {
                            $('#error').append("<div>fff</div>");
                        }



                    },
                    error: function(jqXHR, error, errorThrown) {}
                });
            });

            $('#addresses').on('change', function(e) {

                var optionSelected = $("option:selected", this);
                var valueSelected = this.value;

                //$("#btnAddressManually").fadeOut(200);
                $('.address-manually').fadeIn(200);
                //var completedata = valueSelected.split(",");
                $("#address").val(valueSelected);
                //$("#address2").val(completedata[2]);
                //$("#city").val(completedata[3]);
                // $("#postcode").$("#postcode_lookup").val();
            });


            $('#btnAddressManually').click(function() {
                $('#postcode').val($('#postcode_lookup').val());
                $(".custom-select2").fadeOut(200);
                $(this).fadeOut(200);
                $('.address-manually').fadeIn(200);
                $('#postcode_lookup').val("");
            })
        });
    </script>
@endsection
