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

use Lof\Formbuilder\Helper\Data;
use Lof\Formbuilder\Helper\Fields;

$helper            = $this->helper(Fields::class);
$helperData = $this->helper(Data::class);
$form              = $block->getForm();
$formData         = $this->getData('form_data');
$field             = $this->getData('field_data');
$fieldType        = $field['field_type'];
$label             = $block->filterMessage($field['label']);
$options           = $field['field_options'];
$inlineCss         = $field['inline_css'] ?? '';
$cssClass   = isset($field['css_class']) ? ($block->escapeHtmlAttr($field['css_class']).' ') : '';
$required          = $field['required'];
$cid               = $helperData->getFieldId($field);
$fieldName        = $helper->getFieldPrefix() . $cid . $form->getId();
$fieldId          = $fieldName.uniqid();
$availableFields  = $helper->getAvailableFields();
$html              = '';
$fieldValidate    = $availableFields[$fieldType] ?? '';
$validates         = $fieldValidate;
$fieldHelp        = isset($options['description']) ? $block->filterMessage($options['description']) : '';

$colorLabel       = isset($options['color_label']) ? 'color:'.$options['color_label'].';' : '';
$colorText        = isset($options['color_text']) ? 'color:'.$options['color_text'].';' : '';
$colorDescription = isset($options['color_description']) ? 'color:'.$options['color_description'].';' : '';
$backgroundColor  = isset($options['background_color']) ? 'background-color:'.$options['background_color'].';' : '';

$fontSize         = isset($options['font_size']) ? 'font-size:'.(int)$options['font_size'].'px;' : '';
$fontStyle        = isset($options['font_style']) ? 'font-style:'.$options['font_style'].';' : '';
$fontWeight       = isset($options['font_weight']) ? 'font-weight:'.$options['font_weight'].';' : '';

if ($fieldValidate && is_array($fieldValidate)) {
    $validates = implode(" ", $fieldValidate);
}
$includeOtherOption = $options['include_other_option'] ?? 0;

$gridColumn = $field['grid_column'] ?? 1;
$total      = count($options);
$x          = 0;
?>
<div class="subtemplate-wrapper <?= $cssClass; ?>" style="<?= $inlineCss ?>">
    <div class="row">
        <?php if ($label && $label != '') { ?>
        <div class="input-box-label col-sm-6">
            <label style="<?= $fontSize.$fontStyle.$fontWeight.$colorLabel; ?>"  for="<?= $fieldId; ?>" class="<?= (1 == $required) ? 'required' : ''; ?>"><?= __($label); ?><?= (1 == $required) ? '<em>*</em>' : '';?></label>
        </div>
        <?php } ?>
        <div class="input-box col-sm-6">
            <div class="input-list">
                <ul class="list-options" style="<?= $colorText.$backgroundColor; ?>">
                    <?php if ($options['options']) {
                        foreach ($options['options'] as $key => $val) {
                            $itemLabel = $val['label'];
                            $itemLabelShow = str_replace(["{{", "}}"], ['<img src="{{', '}}" alt="img"/>'], $itemLabel);
                            $itemChecked = $val['checked'];
                            if ($formData && isset($formData[$fieldName.$key]) && $formData[$fieldName.$key] == $itemLabel) {
                                $itemChecked = true;
                            }
                            $itemLabelShow2 = $block->filterMessage($itemLabelShow);
                            if (!$includeOtherOption && ($key == (count($options['options']) - 1))) { ?>
                                <div class="row">
                                    <div class="col-lg-12">
                                        <input type="checkbox" name="<?= $fieldName.'[]'; ?>" id="<?= $fieldId.$key; ?>" value="<?= $block->escapeQuote($itemLabel); ?>" class="<?= (1 == $required) ? 'validate-one-required-by-name required-entry' : ''; ?>" <?= ($itemChecked) ? 'checked="checked"' : ''?>/> <label for="<?= $fieldName.$key; ?>"><?= __($itemLabelShow2); ?></label>
                                    </div>
                                </div>
                                <?php } else { ?>

                                    <?php if ($gridColumn == 1 || $x % $gridColumn == 0) { ?>
                                        <div class="row">
                                    <?php } ?>

                                    <div class="col-lg-<?= 12 / $gridColumn ?>">
                                        <input type="checkbox" name="<?= $fieldName.'[]' ?>" id="<?= $fieldId.$key; ?>"  value="<?= $block->escapeQuote($itemLabel); ?>" class="<?= (1 == $required) ? 'validate-one-required-by-name required-entry' : ''; ?>" <?= ($itemChecked) ? 'checked="checked"' : ''?>/> <label for="<?= $fieldName.$key; ?>"><?= __($itemLabelShow2); ?></label>

                                    </div>

                                    <?php if ($gridColumn == 1 || ($x + 1) % $gridColumn == 0 || ($x == $total - 1)) {  ?>
                                        </div>
                                    <?php } ?>

                                    <?php $x++; ?>

                                    <?php }
                                } ?>
                                <?php if ($includeOtherOption) {
                                    $key++;?>
                                    <div class="row">
                                        <div class="col-lg-12">
                                            <input type="checkbox" name="<?= $fieldName.'[]'; ?>" id="<?= $fieldId.'_other'; ?>" title="<?= __('Other'); ?>" value="other" class="<?= (1 == $required) ? 'validate-one-required-by-name required-entry' : ''; ?>"/> <label for="<?= $fieldName.'_other'; ?>"><?= __("Other"); ?></label> <input name="<?= $fieldName.'_other'; ?>" id="<?= $fieldId.$key; ?>" title="<?= __("Other"); ?>" value="<?= $block->filterMessage(($formData && isset($formData[$fieldName.$key])) ? $formData[$fieldName.$key] : ''); ?>" class="input-text" type="text" />
                                        </div>
                                    </div>
                                    <?php } ?>
                                    <?php } ?>
                                </ul>
                            </div>
                            <?php if ($fieldHelp) { ?>
                                <span class="help-block" style="<?= $colorDescription; ?>"><?= $fieldHelp; ?></span>
                                <?php } ?>
            </div>
    </div>
</div>
