<div class="formpartbg">
    <fieldset>
        <table>
            <tr>
                <td>&nbsp;</td>
                <td style="width: 100%;">   <input id="autocompleteGPlace" placeholder="Enter your address" onFocus="geolocate()" type="text" class="form-control col-sm-12" autocomplete="off"/></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <form class="form-horizontal" action="<?php echo site_url('gkpos/orders/to_delivery') ?>" method="post" id="gkposToDeliveryForm">
            <table>
                <tr style="margin-bottom: 2px">
                    <td style="width:45%">
                        <input data-error="#gkposToDeliveryFormPhoneError" name="phone" id="phone" placeholder="<?php echo $this->lang->line('gkpos_phone') . ' ', $this->lang->line('gkpos_number') ?>" class="form-control input-sm customer-phone" type="text">
                        <div id="gkposToDeliveryFormPhoneError"></div>
                    </td>
                    <td style="width:5%"> <span data-toggle="tooltip-delivery-phone" data-placement="bottom" title="<?php echo $this->lang->line('gkpos_click_to_get_customer_by_phone') ?>"><a href="javascript:void(0)" onclick="searchCustomerByKey('phone')"><i class="fa fa-search fa-2x text-center" aria-hidden="true" style="width: 50px; height: 30px; background: #f00"></i></a></span></td>
                    <td colspan="2"><input data-error="#gkposToDeliveryFormPhoneError" name="name" placeholder="<?php echo $this->lang->line('gkpos_name') ?>" class="form-control input-sm customer-name" type="text"></div></td>  
                </tr>
                <tr style="margin-bottom: 2px">
                    <td colspan="2"><input name="street" placeholder="<?php echo $this->lang->line('gkpos_street') . ' ', $this->lang->line('gkpos_name') ?>" class="form-control required field input-sm" type="text" id="route"></td>
                    <td colspan="2"><input name="postcode" placeholder="<?php echo $this->lang->line('gkpos_postal_code') ?>" class="form-control text-uppercase required field input-sm" type="text" id="postal_code"></td>
                </tr>
                <tr style="margin-bottom: 2px">
                    <td><input name="house"  id="street_number" placeholder="<?php echo $this->lang->line('gkpos_house') ?>" class="form-control input-sm" type="text"></td>
                    <td><input name="floor_or_apt" placeholder="<?php echo $this->lang->line('gkpos_floor_or_apt_no') ?>" class="form-control input-sm" id="floor_or_apt"  type="text"></td>
                    <td colspan="2"><input name="building" placeholder="<?php echo $this->lang->line('gkpos_building') ?>" class="form-control input-sm" type="text" id="building"></td>  
                </tr>
                <tr>
                    <td><input name="city" placeholder="city" class="form-control input-sm field" type="text" id="locality"></td>
                    <td><input name="state" placeholder="state" class="form-control  input-sm field" type="text" id="administrative_area_level_1"></td>
                    <td><input name="country" placeholder="country" class="form-control  input-sm field" type="text" id="country"></td>
                    <td colspan="2">
                        <div class="stylish">
                            <span>
                                <input onclick="$(this).closest('div').find('select').slideToggle(110)" class="form-control customernambg" readonly="1" type="text" name="delivery_time" id="delivery_time"  placeholder="<?php echo $this->lang->line('gkpos_delivery_time') ?>" style="left: 0; width: 150px;"/>
                                <select size=15 onclick="$(this).hide().closest('div').find('input').val($(this).find('option:selected').val());" class="customernambg" style="min-height:250px; margin: 0 auto; left:0px; margin-top: -20px">
                                    <option value="10" class="btn btn-default btn-lg btn-block text-lowercase">10-min</option>
                                    <option value="20" class="btn btn-default btn-lg btn-block text-lowercase">20-min</option>
                                    <option value="30" class="btn btn-default btn-lg btn-block text-lowercase">30-min</option>
                                    <option value="40" class="btn btn-default btn-lg btn-block text-lowercase">40-min</option>
                                    <option value="50" class="btn btn-default btn-lg btn-block text-lowercase">50-min</option>
                                </select>
                            </span>
                        </div>
                    </td>
                </tr>
            </table>
            <div class="form-group" style="padding-top: 20px; "> 
                <div class="col-md-12">
                    <input type="hidden" name="existing_type" value="<?php (isset($existing_type) && ($existing_type != '' || $existing_type != null) ) ? print strtolower($existing_type) : print"" ?>">
                    <input type="hidden" name="order_type" value="<?php (isset($new_type) && ($new_type != '' || $new_type != null) ) ? print strtolower($new_type) : print"" ?>">
                    <input type="hidden" name="order_id" value="<?php echo $order_id ?>">
                    <input class="btn btn-block btn-danger round" type="submit" name="submit_form" value="<?php echo $this->lang->line('gkpos_finished') ?>" onclick="saveGkposToDeliveryInfo('gkposToDeliveryForm')">
                </div>
            </div>
            <div class="form-group"> 
                <div class="col-md-12">
                    <ul id="gkposDeliveryFormErrorMsgBox" class="error_message_box"></ul>
                </div>
            </div>
        </form>
    </fieldset>
</div>
<script type="text/javascript">
    jQuery(document).ready(function () {
        $('[data-toggle="tooltip-delivery-phone"]').tooltip();
        $.getScript('https://maps.googleapis.com/maps/api/js?key=<?php echo GOOGLE_API_KEY; ?>&libraries=places&callback=initAutocomplete', function (data, textStatus) {
            if (textStatus) {
                console.log(textStatus);
            }
        });
    });
    function saveGkposToDeliveryInfo(formId) {
        
        $('#' + formId).validate({
            submitHandler: function () {
                var myform = document.getElementById(formId);
                var formdata = new FormData(myform);
                $.ajax({
                    url: $('#' + formId).attr('action'),
                    data: formdata,
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    dataType: 'json',
                    beforeSend: function () {
                        $("#deliveryAjaxLoading").show();
                    },
                    success: function (response) {
                        $("#deliveryAjaxLoading").hide();
                        if (response.success) {
                            getBaseAjaxPage('<?php echo site_url('gkpos/orders/indexajax/') ?>' + response.order_id, 'Delivery-' + response.name);
                        } else {
                            alert(response.message);
                        }
                    }
                });
            },
            errorClass: "has-error",
            highlight: function (e) {
                $(e).closest('.form-group').addClass('has-error');
            },
            unhighlight: function (e) {
                $(e).closest('.form-group').removeClass('has-error');
            },
            rules: {
                name: {lettersonly: true, required: true},
                phone: {phone: true, required: true},
                street: "required",
                postcode: {required: true, postcodeUK: true}
            },
            messages: {
                name: "<?php echo $this->lang->line('gkpos_valid_name_required') ?>",
                phone: "<?php echo $this->lang->line('gkpos_valid_phone_required') ?>",
                street: "<?php echo $this->lang->line('gkpos_customer_street_required') ?>",
                postcode: "<?php echo $this->lang->line('gkpos_customer_postcode_required') ?>",
                delivery_time: "<?php echo $this->lang->line('gkpos_order_delivery_time_required') ?>"
            }
        });
    }
    var placeSearch, autocomplete;
    var componentForm = {
        street_number: 'short_name',
        route: 'long_name',
        locality: 'long_name',
        administrative_area_level_1: 'short_name',
        country: 'long_name',
        postal_code: 'short_name'
    };
    function initAutocomplete() {
        autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocompleteGPlace')), {types: ['geocode']});
        autocomplete.addListener('place_changed', fillInAddress);
    }
    function fillInAddress() {
        var place = autocomplete.getPlace();
        for (var component in componentForm) {
            document.getElementById(component).value = '';
            document.getElementById(component).disabled = false;
        }
        for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            if (componentForm[addressType]) {
                var val = place.address_components[i][componentForm[addressType]];
                document.getElementById(addressType).value = val;
            }
        }
    }
    function geolocate() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var geolocation = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };
                var circle = new google.maps.Circle({
                    center: geolocation,
                    radius: position.coords.accuracy
                });
                autocomplete.setBounds(circle.getBounds());
            });
        }

    }
</script>
