<?php
/**
 * Landofcoder
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Landofcoder.com license that is
 * available through the world-wide-web at this URL:
 * https://landofcoder.com/terms
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category   Landofcoder
 * @package    Lof_Formbuilder
 * @copyright  Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
 * @license    https://landofcoder.com/terms
 */

$helper           = $this->helper("Lof\Formbuilder\Helper\Fields");
$helper_data       = $this->helper("Lof\Formbuilder\Helper\Data");
$form             = $block->getForm();
$form_data        = $this->getData('form_data');
$field            = $this->getData('field_data');
$field_type       = $field['field_type'];
$label            = $block->filterMessage($field['label']);
$inlineCss        = isset($field['inline_css'])?$field['inline_css']:'';
$cssClass   = isset($field['css_class']) ? ($block->escapeHtmlAttr($field['css_class']).' ') : '';
$options          = $field['field_options'];
$required         = $field['required'];
$cid              = $helper_data->getFieldId($field);
$field_name       = $helper->getFieldPrefix() . $cid . $form->getId();
$field_id          = $field_name.uniqid();
$available_fields = $helper->getAvailableFields();
$html             = '';
$field_validate   = isset($available_fields[$field_type])?$available_fields[$field_type]:'';
$validates        = $field_validate;
$field_help       = isset($options['description'])?$block->filterMessage($options['description']):'';
if ($field_validate && is_array($field_validate)) {
    $validates = implode(" ", $field_validate);
}
$width        = isset($field['width'])?$field['width']:'550';
$height       = isset($field['height'])?$field['height']:'400';
$radius       = isset($field['radius'])?(float)$field['radius']:300;
$default_lat  = isset($field['default_lat'])?(float)$field['default_lat']:21.0199438;
$default_long = isset($field['default_long'])?(float)$field['default_long']:105.81731119999995;
$address      = isset($field['address'])?$field['address']:$form_data[$field_name];
$frame_work_css   = '';
if (isset($field['frame_work_css'])) {
    $frame_work_css   = $field['frame_work_css'];
}
if ($frame_work_css == 'foundation') {
    $medium = 'columns medium-';
} else {
    $medium = 'col-sm-';
}
$showLocationLabel = true;
$captchaPublicKey = $helper_data->getConfig('googleapi/publickey');
$showLocation     = isset($field['show_location'])?$field['show_location']:0;
$showRadius       = isset($field['show_radius'])?(int)$field['show_radius']:0;
$showMap          = isset($field['show_map'])?$field['show_map']:0;
$location_label   = isset($field['location_label'])?$field['location_label']:"";
$width            = str_replace("px", "", $width) . "px";
$height           = str_replace("px", "", $height) . "px";

if (!$showRadius) {
    $radius = 0;
}
if (empty($location_label)) {
    $showLocationLabel = false;
}
?>
<div class="subtemplate-wrapper <?= $cssClass; ?>" style="<?= $inlineCss ?>">
    <label for="<?= $field_id; ?>" class="<?= (1==$required)?'required':''; ?>"><?= $label; ?><?= (1==$required)?'<em>*</em>':'';?></label>
    <div class="row">
        <div <?php if (!$showLocation) { ?>style="display:none"<?php } ?> class="form-group <?= $medium; ?>6">
            <div class="row">
                <?php if ($showLocationLabel): ?><label class="<?= $medium; ?>2 control-label"><?= $block->escapeHtml($location_label); ?></label><?php endif; ?>
                <div class="<?= $medium; ?>10"><input type="text" class="form-control" name="<?= $field_name; ?>" id="<?= $field_id."-address"; ?>"  value="<?= $address; ?>"/></div>
            </div>
        </div>
        <div <?php if (!$showRadius) { ?>style="display:none"<?php } ?> class="form-group <?= $medium; ?>6">
            <div class="row">
                <label class="<?= $medium; ?>2 control-label"><?= $block->escapeHtml(__("Radius:")); ?></label>
                <div class="<?= $medium; ?>10"><input type="text" class="form-control" name="<?= $field_name."_radius"; ?>" id="<?= $field_id."-radius"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_radius"]))?$form_data[$field_name."_radius"]:$radius; ?>"/></div>
            </div>
        </div>
    </div>
    <div id="<?= $field_id; ?>" <?= !$showMap?'class="hidden"':'';?> style="width: <?= $width; ?>; height: <?= $height; ?>;margin-top:20px"></div>
    <div class="clearfix">&nbsp;</div>
    <div class="m-t-small">
        <input name="<?= $field_name."_lat"; ?>" id="<?= $field_id."-lat"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_lat"]))?$form_data[$field_name."_lat"]:$default_lat; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_long"; ?>" id="<?= $field_id."-long"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_long"]))?$form_data[$field_name."_long"]:$default_long; ?>" class="input-text" type="hidden"/>

        <input name="<?= $field_name."_addressLine1"; ?>" id="<?= $field_id."-addressLine1"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_addressLine1"]))?$form_data[$field_name."_addressLine1"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_addressLine2"; ?>" id="<?= $field_id."-addressLine2"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_addressLine2"]))?$form_data[$field_name."_addressLine2"]:""; ?>" class="input-text" type="hidden"/>

        <input name="<?= $field_name."_street"; ?>" id="<?= $field_id."-street"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_street"]))?$form_data[$field_name."_street"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_streetNumber"; ?>" id="<?= $field_id."-streetNumber"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_streetNumber"]))?$form_data[$field_name."_streetNumber"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_district"; ?>" id="<?= $field_id."-district"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_district"]))?$form_data[$field_name."_district"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_city"; ?>" id="<?= $field_id."-city"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_city"]))?$form_data[$field_name."_city"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_state"; ?>" id="<?= $field_id."-state"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_state"]))?$form_data[$field_name."_state"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_stateOrProvince"; ?>" id="<?= $field_id."-stateOrProvince"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_stateOrProvince"]))?$form_data[$field_name."_stateOrProvince"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_country"; ?>" id="<?= $field_id."-country"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_country"]))?$form_data[$field_name."_country"]:""; ?>" class="input-text" type="hidden"/>
        <input name="<?= $field_name."_postcode"; ?>" id="<?= $field_id."-postcode"; ?>" value="<?= ($form_data && isset($form_data[$field_name."_postcode"]))?$form_data[$field_name."_postcode"]:""; ?>" class="input-text" type="hidden"/>
    </div>
    <div class="clearfix"></div>
    <!-- <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&libraries=places&key=<?= $captchaPublicKey ?>"></script> -->
    <script type="text/javascript">
        require([
            'jquery',
            'Lof_Formbuilder/js/locationpicker/locationpicker'
            ], function(){
                jQuery('#<?= $field_id; ?>').locationpicker({
                    location: {latitude: <?= $default_lat; ?>, longitude: <?= $default_long; ?>},
                    radius: <?= $radius; ?>,
                    inputBinding: {
                        latitudeInput: jQuery('#<?= $field_id; ?>-lat'),
                        longitudeInput: jQuery('#<?= $field_id; ?>-long'),
                        radiusInput: jQuery('#<?= $field_id; ?>-radius'),
                        locationNameInput: jQuery('#<?= $field_id; ?>-address'),
                        addressLine1Input: jQuery('#<?= $field_id; ?>-addressLine1'),
                        addressLine2Input: jQuery('#<?= $field_id; ?>-addressLine2'),
                        streetNameInput: jQuery('#<?= $field_id; ?>-street'),
                        districtInput: jQuery('#<?= $field_id; ?>-district'),
                        streetNumberInput: jQuery('#<?= $field_id; ?>-streetNumber'),
                        cityInput: jQuery('#<?= $field_id; ?>-city'),
                        stateInput: jQuery('#<?= $field_id; ?>-state'),
                        stateOrProvinceInput: jQuery('#<?= $field_id; ?>-stateOrProvince'),
                        countryInput: jQuery('#<?= $field_id; ?>-country'),
                        postcodeInput: jQuery('#<?= $field_id; ?>-postcode'),
                    },
                    enableAutocomplete: true
                });
            });
        </script>
        <?php if ($field_help) : ?>
        <span class="help-block"><?= $field_help; ?></span>
        <?php endif; ?>
</div>
