<?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");
$helperData = $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']);
$options    = $field['field_options'];
$required   = $field['required'];
$frame_work_css   = '';
if (isset($field['frame_work_css'])) {
    $frame_work_css      = $field['frame_work_css'];
}
$show_address     = isset($field['show_address']) ? $field['show_address'] : 1;
$show_city        = isset($field['show_city']) ? $field['show_city'] : 1;
$show_state       = isset($field['show_state']) ? $field['show_state'] : 1;
$show_zipcode     = isset($field['show_zipcode']) ? $field['show_zipcode'] : 1;
$show_country     = isset($field['show_country']) ? $field['show_country'] : 1;
$address_placeholder     = isset($field['address_placeholder']) ? $field['address_placeholder'] : '';
$city_placeholder     = isset($field['city_placeholder']) ? $field['city_placeholder'] : '';
$state_placeholder     = isset($field['state_placeholder']) ? $field['state_placeholder'] : '';
$zipcode_placeholder     = isset($field['zipcode_placeholder']) ? $field['zipcode_placeholder'] : '';
$country_placeholder     = isset($field['country_placeholder']) ? $field['country_placeholder'] : '';
$cid         = $helperData->getFieldId($field);
$field_name       = $helper->getFieldPrefix() . $cid . $form->getId();
$field_id          = $field_name . uniqid();
$available_fields = $helper->getAvailableFields();
$field_validate   = isset($available_fields[$field_type]) ? $available_fields[$field_type] : '';
$validates        = $field_validate;
$field_help       = isset($options['description']) ? $block->filterMessage($options['description']) : '';
$inlineCss        = isset($field['inline_css']) ? $field['inline_css'] : '';
$cssClass          = isset($field['css_class']) ? ($block->escapeHtmlAttr($field['css_class']).' ') : '';
if ($field_validate && is_array($field_validate)) {
    $validates = implode(" ", $field_validate);
}
$scopeHelper = $this->helper('\Magento\Directory\Helper\Data');
$_countries = $scopeHelper->getCountryCollection()->toOptionArray(false);

$onchange = '';
if ($show_country == 1 && $show_state == 1) {
    $onchange = 'onChange="getState(this.value);"';
}
$color_label          = isset($options['color_label']) ? 'color:' . $options['color_label'] . ';' : '';
$color_text           = isset($options['color_text']) ? 'color:' . $options['color_text'] . ';' : '';
$color_description    = isset($options['color_description']) ? 'color:' . $options['color_description'] . ';' : '';
$background_color     = isset($options['background_color']) ? 'background-color:' . $options['background_color'] . ';' : '';
$font_size            = isset($options['font_size']) ? 'font-size:' . (int)$options['font_size'] . 'px;' : '';
$font_style           = isset($options['font_style']) ? 'font-style:' . $options['font_style'] . ';' : '';
$font_weight          = isset($options['font_weight']) ? 'font-weight:' . $options['font_weight'] . ';' : '';
$style = $font_size . $font_style . $font_weight . $color_label;
if ($frame_work_css != 'foundation') {
    ?>
    <div class="subtemplate-wrapper <?= $cssClass; ?>" style="<?= $inlineCss ?>">
        <label for="<?= $field_id; ?>" class="<?= (1==$required) ? 'required' : ''; ?>" style="<?= $style ?>"><?= __($label); ?><?= (1==$required) ? '<em>*</em>' : ''; ?></label>
        <div class="row">
            <div class="input-box <?= isset($field['fieldcol']) ? 'col-sm-' . $field['fieldcol'] : 'col-sm-12'; ?>">
                <input type="hidden" name="<?= $field_name; ?>" value="<?= ($form_data && $form_data[$field_name]) ? $form_data[$field_name] : ''; ?>" id="<?= $field_id; ?>"/>

                <?php if ($show_address == 1): ?>
                    <div class="row">
                        <div class="input-line col-sm-12">
                            <div class="street">
                                <input type="text" name="<?= $field_name . '_street'; ?>" id="<?= $field_id . '_street'; ?>" class="input-text <?= (1==$required) ? 'required-entry' : ''; ?>" value="<?= ($form_data && isset($form_data[$field_name . '_street'])) ? $form_data[$field_name . '_street'] : ''; ?>"<?php if ($address_placeholder) { ?> placeholder="<?= $address_placeholder; ?>"<?php } ?>/>
                                <label for="<?= $field_id . '_street'; ?>"><?= __('Address'); ?></label>
                            </div>
                        </div>

                    </div>
                <?php endif; ?>


                <?php if ($show_city == 1 || $show_state == 1): ?>
                    <div class="row">
                        <div class="input-line">
                            <?php if ($show_city == 1): ?>
                                <div class="city col-sm-6">
                                    <input type="text" name="<?= $field_name . '_city'; ?>" id="<?= $field_id . '_city'; ?>" class="input-text" value="<?= ($form_data && isset($form_data[$field_name . '_city'])) ? $form_data[$field_name . '_city'] : ''; ?>"<?php if ($city_placeholder) { ?> placeholder="<?= $city_placeholder; ?>"<?php } ?>/>
                                    <label for="<?= $field_id . '_city'; ?>"><?= __('City'); ?></label>
                                </div>
                            <?php endif; ?>
                            <?php if ($show_state == 1): ?>
                                <div class="state col-sm-6" id="<?= 'state_' . $field_id; ?>">
                                    <input type="text" name="<?= $field_name . '_state'; ?>" id="<?= $field_id . '_state'; ?>" class="input-text validate-state" value="<?= ($form_data && isset($form_data[$field_name . '_state'])) ? $form_data[$field_name . '_state'] : ''; ?>"<?php if ($state_placeholder) { ?> placeholder="<?= $state_placeholder; ?>"<?php } ?>/>
                                    <label for="<?= $field_id . '_state'; ?>"><?= $block->escapeHtml(__('State / Province / Region')); ?></label>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ($show_zipcode == 1 || $show_country == 1): ?>
                    <div class="row">
                        <div class="input-line">
                            <div class="col-sm-12">
                                <div class="row">
                                    <?php if ($show_zipcode == 1): ?>
                                        <div class="zipcode col-sm-6">
                                            <input type="text" name="<?= $field_name . '_zipcode'; ?>" id="<?= $field_id . '_zipcode'; ?>" class="input-text validate-zip-international" value="<?= ($form_data && isset($form_data[$field_name . '_zipcode'])) ? $form_data[$field_name . '_zipcode'] : ''; ?>"<?php if ($zipcode_placeholder) { ?> placeholder="<?= $zipcode_placeholder; ?>"<?php } ?>/>
                                            <label for="<?= $field_id . '_zipcode'; ?>"><?= $block->escapeHtml(__('Zipcode')); ?></label>
                                        </div>
                                    <?php endif; ?>
                                    <?php if ($show_country == 1): ?>
                                        <div class="country col-sm-6">
                                            <?php if (count($_countries) > 0) { ?>
                                            <select name="<?= $field_name . '_country'; ?>" id="<?= $field_id . '_country'; ?>" class="<?= (1==$required) ? 'required-entry' : ''; ?>" <?= $onchange; ?>>
                                                <option value=""><?= $block->escapeHtml(__('-- Please Select --')); ?></option>
                                                <?php foreach ($_countries as $_country) {  ?>
                                                <option value="<?= $_country['label']; ?>" <?= ($form_data && isset($form_data[$field_name . '_country'])) ? ' selected="selected"' : ''; ?>><?= $_country['label']; ?></option>
                                                <?php } ?>
                                            </select>
                                            <?php } else { ?>
                                            <input type="text" name="<?= $field_name . '_country'; ?>" value="<?= ($form_data && isset($form_data[$field_name . '_country'])) ? $form_data[$field_name . '_country'] : ''; ?>" id="<?= $field_id . '_country';?>" class="input-text"<?php if ($country_placeholder) { ?> placeholder="<?= $country_placeholder; ?>"<?php } ?>/>
                                            <?php } ?>
                                            <label for="<?= $field_id . '_country'; ?>"><?= $block->escapeHtml(__('Country')); ?></label>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ($field_help) { ?>
                <span class="help-block"><?= $field_help ?></span>
                <?php } ?>
            </div>
        </div>
    </div>
    <?php
} else { ?>
    <div class="subtemplate-wrapper <?= $cssClass; ?>" style="<?= $inlineCss ?>">
        <label for="<?= $field_id; ?>" class="<?= (1==$required) ? 'required' : ''; ?>" style="<?= $style ?>"><?= __($label); ?><?= (1==$required) ? '<em>*</em>' : '';?>
            <div class="row">
                <div class="input-box columns <?= isset($field['fieldcol']) ? 'medium-' . $field['fieldcol'] : 'medium-12'; ?>">
                    <input type="hidden" name="<?= $field_name; ?>" value="<?= ($form_data && $form_data[$field_name]) ? $form_data[$field_name] : ''; ?>" id="<?= $field_id; ?>"/>

                    <?php if ($show_address == 1): ?>
                        <div class="row">
                            <div class="input-line columns medium-12">
                                <div class="street">
                                    <label for="<?= $field_name . '_street'; ?>">
                                        <input type="text" name="<?= $field_name . '_street'; ?>" id="<?= $field_id . '_street';?>" class="input-text <?= (1==$required) ? 'required-entry' : ''; ?>" value="<?= ($form_data && isset($form_data[$field_name . '_street'])) ? $form_data[$field_name . '_street'] : ''; ?>"/>
                                        <?= __('Address'); ?>
                                    </label>
                                </div>
                            </div>

                        </div>
                        <div class="row">
                            <div class="input-line columns medium-12">
                                <div class="street">
                                    <label for="<?= $field_id . '_street2'; ?>">
                                        <input type="text" name="<?= $field_name . '_street2'; ?>" id="<?= $field_id . '_street2';?>" class="input-text <?= (1==$required) ? 'required-entry' : ''; ?>" value="<?= ($form_data && isset($form_data[$field_name . '_street2'])) ? $form_data[$field_name . '_street2'] : ''; ?>"/>
                                        <?= __('Address 2'); ?>
                                    </label>
                                </div>
                            </div>

                        </div>
                    <?php endif; ?>


                    <?php if ($show_city == 1 || $show_state == 1): ?>
                        <div class="row">
                            <div class="input-line">
                                <?php if ($show_city == 1): ?>
                                    <div class="city columns medium-6">
                                        <label for="<?= $field_id . '_city'; ?>">
                                            <input type="text" name="<?= $field_name . '_city';?>" id="<?= $field_id . '_city';?>" class="input-text" value="<?= ($form_data && isset($form_data[$field_name . '_city'])) ? $form_data[$field_name . '_city'] : ''; ?>"/>
                                            <?= __('City'); ?>
                                        </label>
                                    </div>
                                <?php endif; ?>
                                <?php if ($show_state == 1): ?>
                                    <div class="state columns medium-6" id="<?= 'state_' . $field_id;?>">
                                        <label for="<?= $field_id . '_state'; ?>">
                                            <input type="text" name="<?= $field_name . '_state';?>" id="<?= $field_id . '_state';?>" class="input-text validate-state" value="<?= ($form_data && isset($form_data[$field_name . '_state'])) ? $form_data[$field_name . '_state'] : ''; ?>"/>
                                            <?= $block->escapeHtml(__('State / Province / Region'));?>
                                        </label>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>

                    <?php if ($show_zipcode == 1 || $show_country == 1): ?>
                        <div class="row">
                            <div class="input-line">
                                <div class="columns medium-12">
                                    <div class="row">
                                        <?php if ($show_zipcode == 1): ?>
                                            <div class="zipcode columns medium-6">
                                                <label for="<?= $field_id . '_zipcode'; ?>">
                                                    <input type="text" name="<?= $field_name . '_zipcode'; ?>" id="<?= $field_id . '_zipcode';?>" class="input-text validate-zip-international" value="<?= ($form_data && isset($form_data[$field_name . '_zipcode'])) ? $form_data[$field_name . '_zipcode'] : ''; ?>"/>
                                                    <?= $block->escapeHtml(__('Zipcode')); ?>
                                                </label>
                                            </div>
                                        <?php endif; ?>
                                        <?php if ($show_country == 1): ?>
                                            <div class="country columns medium-6">
                                                <?php if (count($_countries) > 0) { ?>
                                                <select name="<?= $field_name . '_country'; ?>" id="<?= $field_id . '_country'; ?>" class="<?= (1==$required) ? 'required-entry' : ''; ?>" <?= $onchange; ?>>
                                                    <option value=""><?= $block->escapeHtml(__('-- Please Select --')); ?></option>
                                                    <?php foreach ($_countries as $_country) {  ?>
                                                    <option value="<?= $_country['label']; ?>" <?= ($form_data && isset($form_data[$field_name . '_country'])) ? ' selected="selected"' : ''; ?>><?= $_country['label']; ?></option>
                                                    <?php } ?>
                                                </select>
                                                <?php } else { ?>
                                                <label for="<?= $field_id . '_country';?>">
                                                    <input type="text" name="<?= $field_name . '_country'; ?>" value="<?= ($form_data && isset($form_data[$field_name . '_country'])) ? $form_data[$field_name . '_country'] : ''; ?>" id="<?= $field_id . '_country';?>" class="input-text"/>
                                                    <?php }
                                                ?>
                                                    <?= $block->escapeHtml(__('Country')); ?>
                                                </label>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>

                    <?php if ($field_help) : ?>
                    <span class="help-block"><?= $field_help ?></span>
                    <?php endif; ?>
                </div>
            </div>
        </label>
    </div>

    <?php } ?>
    <?php if ($show_country == 1 && $show_state == 1) : ?>
    <script type="text/javascript">
        function getState(val) {
            ajaxurl  =  '<?= $block->escapeUrl($this->getUrl('formbuilder/form/state')); ?>';
            jQuery('.formbuilder-loading').addClass('ajax-loading');
            jQuery.ajax({
                type: "POST",
                url: ajaxurl,
                dataType: 'json',
                data:'country_id='+val+'&field_name=<?= $field_id . '_state';?>',
                success: function(data){
                    jQuery("#<?= 'state_' . $field_id;?>").html(data);
                    jQuery('.formbuilder-loading').removeClass('ajax-loading');
                }
            });
        }
    </script>
    <?php endif; ?>
