<?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
 */

/** @var \Digitouch\SAML2\Block\CustomBlock $customBlock */
$customBlock = $block->getLayout()->createBlock('\Digitouch\SAML2\Block\CustomBlock');

$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']);
$options        = $field['field_options'];
$required       = $field['required'];
$cid              = $helper_data->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'] . ';' : '';

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

if ($field_validate && is_array($field_validate)) {
    $validates = implode(" ", $field_validate);
}
$validates .= " " . $cssClass;
if ($frame_work_css == 'foundation') {
    $medium = 'columns medium-';
} else {
    $medium = 'col-sm-';
}
?>
<div class="subtemplate-wrapper" 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'] : $medium . '12'; ?>">
            <?php if ($options['options']) : ?>
                <select style="<?= $color_text . $background_color . $border_width . $border_radius . $border_color . $border_style; ?>" name="<?= $field_name; ?>" id="<?= $field_id; ?>" class="<?= (1 == $required) ? 'required-entry' : ''; ?> <?= $validates; ?>">
                    <?php if (isset($options['include_blank_option']) && $options['include_blank_option']) : ?>
                        <option value="" selected disabled><?= __("Select a category") ?></option>
                    <?php endif; ?>

                    <?php foreach ($options['options'] as $key => $val) :
                        $item_label = $val['label'];
                        $item_checked = $val['checked'];
                        if ($form_data && isset($form_data[$field_name]) && $form_data[$field_name] == $item_label) :
                            $item_checked = true;
                        endif;
                        $item_label2 = $block->filterMessage($item_label); ?>
                        <option style="<?= $color_text . $background_color . $border_width . $border_radius . $border_color . $border_style; ?>" value="<?= strtolower(str_replace(" ", "-", $helper->escapeQuote($item_label))); ?>" <?= ($item_checked) ? 'selected="selected"' : ''; ?>><?= __($item_label2); ?></option>
                    <?php endforeach; ?>
                </select>
            <?php
            endif;
            ?>
            <?php if ($field_help) : ?>
                <span class="help-block" style="<?= $block->escapeHtml($color_description); ?>"><?= $field_help ?></span>
            <?php endif; ?>
        </div>
    </div>
</div>

<script type="text/javascript">
    requirejs(['jquery'], function(jQuery) {
        (function($) {
            var clientType = "<?php echo $customBlock->getCustomerSegment(); ?>";

            function showChildDropdowns(parent) {
                var parVal = parent.value;
                $(".child-select").closest(".field").css("display", "none");
                $(".child-select-" + parVal).closest(".field").css("display", "block");
                $(".child-select").each(function(index, element) {
                    if (typeof parVal !== "undefined") {
                        if ($(this).hasClass("child-select-" + parVal)) {
                            if ($(this).hasClass("required-entry-disabled")) {
                                $(this).removeClass("required-entry-disabled");
                                $(this).addClass("required-entry");
                            }
                        } else {
                            if ($(this).hasClass("required-entry")) {
                                var firstOprion = $(this).find('option:first').val();
                                $(this).removeClass("required-entry");
                                $(this).addClass("required-entry-disabled");
                                $(this).val(firstOprion);
                            }
                        }
                    }
                });
            }

            $('#<?= $field_id; ?>').on('change', function() {
                showChildDropdowns(this);
            });

            $(document).ready(function() {
                if (clientType == "SHP") {
                    $("#<?= $field_id; ?> option[value='diritto-di-ripensamento']").each(function() {
                        $(this).remove();
                    });
                }

                $(".child-select").each(function(index, element) {
                    if ($(this).hasClass("required-entry")) {
                        $(this).removeClass("required-entry");
                        $(this).addClass("required-entry-disabled");
                    }
                });

                <?php
                if (isset($_GET["argomento"])) :
                    $argomento = $_GET['argomento'];
                ?>
                    var argomento = "<?= $argomento ?>";
                    if (argomento.length > 0) {
                        $('#<?= $field_id; ?>').val(argomento);
                        $('#<?= $field_id; ?>').trigger("change");
                    }
                <?php endif; ?>
                <?php
                if ($customBlock->canShowReturn() === false) :
                ?>
                    $(".argomento").find('option[value="recesso-prodotto"]').remove();
                <?php endif; ?>

            });
        })(jQuery);
    });
</script>
