<?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");
$data_helper      = $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'];
$cid              = $data_helper->getFieldId($field);
$frame_work_css = '';
if (isset($field['frame_work_css'])) {
    $frame_work_css      = $field['frame_work_css'];
}
$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']) : '';
$inlineCss        = isset($field['inline_css']) ? $field['inline_css'] : '';
$cssClass   = isset($field['css_class']) ? ($block->escapeHtmlAttr($field['css_class']).' ') : '';
$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'] . ';' : '';

$border_width         = isset($options['border_width']) ? 'border-width:' . (int)$options['border_width'] . 'px;' : '';
$border_radius        = isset($options['border_radius']) ? 'border-radius:' . (int)$options['border_radius'] . 'px;' : '';
$border_color         = isset($options['border_color']) ? 'border-color:' . $options['border_color'] . ';' : '';
$border_style         = isset($options['border_style']) ? 'border-style:' . $options['border_style'] . ';' : '';
$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'] . ';' : '';
$date_validation_year      = isset($field['date_validation_year']) ? @trim($field['date_validation_year']) : '';
$date_validation_to      = isset($field['date_validation_to']) ? @trim($field['date_validation_to']) : '';

$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'] . ';' : '';

$icon              = isset($options['icon']) ? $options['icon'] : '';
$icon_color          = isset($options['icon_color']) ? $options['icon_color'] : '';

if ($field_validate && is_array($field_validate)) {
    $validates = implode(" ", $field_validate);
}
$date_format = $data_helper->getConfig("general_settings/dateformat");
if ($frame_work_css == 'foundation') {
    $medium = 'columns medium-';
} else {
    $medium = 'col-sm-';
}

$maxDate = "";
if ($date_validation_year) {
    $date_validation_year = (int)$date_validation_year;
    $currentYear =  date("Y");
    if ($date_validation_year > 0 && $date_validation_year < 200) {
        $date_validation_to = date("Y") - $date_validation_year;
    }
}
if ($date_validation_to) {
    if (strpos($date_validation_to, "/") === false) {
        $month = date('m');
        $day = date('d');
        $maxDate = (int)$month."/".(int)$day."/".(int)$date_validation_to;
    } else {
        $maxArr = explode("/", $date_validation_to);
        $maxDate = (count($maxArr) == 3)?$date_validation_to:"";
    }
}
$maxDateValue = $maxDate;
$verifyDateRange = "";
$maxDateClass = "validate-date-range date-range-".$field_id."-to";

if (!empty($maxDate)) {
    $verifyDateRange = "validate-date validate-date-range date-range-".$field_id."-".$field_id."to";
} else {
    $maxDateValue = "01/01/2100";
}
?>
<div class="subtemplate-wrapper <?= $cssClass; ?>" style="<?= $inlineCss ?>">
    <label for="<?= $field_id; ?>" style="<?= $font_size . $font_style . $font_weight . $color_label; ?>" class="<?= (1==$required) ? 'required' : ''; ?>"><?= $label; ?><?= (1==$required) ? '<em>*</em>' : '';?></label>
    <div class="row">
        <div class="input-box <?= isset($field['fieldcol']) ? $medium . $field['fieldcol'] : 'col-sm-12'; ?>">
            <div class="subtemplate-inner">
                <input type="hidden" class="<?= $maxDateClass; ?>" value="<?= $maxDateValue; ?>"/>
                <input autocomplete="off" style="<?= $color_text . $background_color . $border_width . $border_radius . $border_color . $border_style; ?>" name="<?= $field_name; ?>" id="<?= $field_id; ?>" title="<?= $label; ?>" value="<?= ($form_data && isset($form_data[$field_name])) ? $form_data[$field_name] : ''; ?>" class="input-text <?= (1==$required) ? 'required-entry' : ''?> <?= $verifyDateRange; ?>" type="text"/>
                <?php if ($icon) { ?>
                <i class="fa <?= $icon; ?> form-field-icon" style="color:<?= $icon_color; ?>;"></i>
                <?php } ?>
                <?php  if ($field_help) { ?>
                <span style="<?= $color_description; ?>" class="help-block" ><?= $field_help; ?></span>
                <?php } ?>
                <script type="text/javascript">
                require([
                    "jquery",
                    'mage/translate',
                    "mage/calendar"
                    ], function($,$t){
                        var max = null;
                        <?php if (!empty($maxDate)): ?>
                        max = new Date("<?= $maxDate; ?>");
                        <?php endif; ?>

                        $("#<?= $field_id; ?>").calendar({
                            changeYear: true,
                            changeMonth: true,
                            yearRange: "c-90:c+90",
                            shortYearCutoff: "+10",
                            showButtonPanel: true,
                            currentText: $t('Go Today'),
                            closeText: $t('Close'),
                            showWeek: true,
                            buttonText: $t('Select Date'),
                            dateFormat: "<?= $date_format ?>",
                            maxDate: max,
                        });
                    });
                </script>
                </div>
            </div>
        </div>
</div>
