<?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_height= isset($field['fieldheight']) ? 'height: ' . $field['fieldheight'] : 'height: ' . '30px';
$field_type  = $field['field_type'];
$label       = $block->filterMessage($field['label']);
$placeholder = isset($field['placeholder']) ? $block->filterMessage($field['placeholder']) : '';
$options     = $field['field_options'];
$inlineCss   = isset($field['inline_css']) ? $field['inline_css'] : '';
$cssClass   = isset($field['css_class']) ? ($block->escapeHtmlAttr($field['css_class']).' ') : '';
$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']) : '';
if ($field_validate && is_array($field_validate)) {
    $validates = implode(" ", $field_validate);
}
$min                  = isset($options['minlength']) ? (int)$options['minlength'] : 0;
$max                  = isset($options['maxlength']) ? (int)$options['maxlength'] : 100;
$validation           = isset($options['validation']) ? $options['validation'] : '';
$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'] . ';' : '';
$icon                 = isset($options['icon']) ? $options['icon'] : '';
$icon_color           = isset($options['icon_color']) ? $options['icon_color'] : '';
$min_max_length_units = isset($options['min_max_length_units']) ? $options['min_max_length_units'] : 'characters';
$defaultValue         = isset($field['default_value']) ? $block->filterMessage($field['default_value']) : '';
if ($form_data && isset($form_data[$field_name]) && $form_data[$field_name]!='') {
    $defaultValue = $form_data[$field_name];
}

$classes = $cssClass.'input-text ' . $validation . ' ' . $validates . ' validate-length validate-min-max';
if (1==$required) {
    $classes .= ' required-entry';
}
if ($min > 0) {
    $classes .= ' minimum-length-' . $min;
}
if ($max > 0) {
    $classes .= ' maximum-length-' . $max;
}

$style = $font_size . $font_style . $font_weight . $color_label;
$type  = 'tel';
if (isset($field['is_hidden_field']) && $field['is_hidden_field']==1) {
    $type = 'hidden';
}
$attr = '';
if (isset($field['is_readonly']) && $field['is_readonly']==1) {
    $attr = 'readonly';
}
?>
<?php if ($frame_work_css != 'foundation') : ?>
<div class="subtemplate-wrapper" style="<?= $inlineCss ?>">
    <?php if ($type != "hidden") : ?>
    <label for="<?= $field_id; ?>" class="<?= (1==$required) ? 'required' : ''; ?>" style="<?= $style ?>"><?= __($label); ?><?= (1==$required) ? '<em>*</em>' : '';?></label>
    <?php endif; ?>
    <div class="row">
        <div class="<?= isset($field['fieldcol']) ? 'col-sm-' . $field['fieldcol'] : 'col-sm-12'; ?>">
            <div class="subtemplate-inner">
                <input <?php if ($placeholder) :
                    echo "placeholder='" . $placeholder . "'";
                endif; ?> style="<?= $color_text . $background_color . $border_width . $border_radius . $border_color . $border_style . $field_height; ?>" name="<?= $field_name; ?>" minlength="<?= (int)$min ?>" id="<?= $field_id; ?>" title="<?= $label; ?>" value="<?= $block->escapeHtmlAttr($this->helper('Lof\Formbuilder\Helper\Data')->getPostValue($field_name) ?: $defaultValue) ?>" class="<?= $classes; ?>" type="<?= $type; ?>" data-units="<?= $min_max_length_units; ?>" <?= $attr; ?>/>
                <?php if ($icon) : ?>
                <i class="fa <?= $icon; ?> form-field-icon" style="color: <?= $icon_color; ?>;"></i>
                <?php endif; ?>
                <?php if ($field_help) : ?>
                <span class="help-block" style="<?= $color_description; ?>"><?= $field_help; ?></span>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php elseif ($frame_work_css == 'foundation') : ?>
<div class="subtemplate-wrapper" style="<?= $inlineCss ?>">
    <?php if ($type != "hidden") : ?>
    <label for="<?= $field_id; ?>" class="<?= (1==$required) ? 'required' : ''; ?>" style="<?= $style ?>"><?= __($label); ?><?= (1==$required) ? '<em>*</em>' : '';?></label>
    <?php endif; ?>
    <div class="row">
        <div class="columns <?= isset($field['fieldcol']) ? 'medium-' . $field['fieldcol'] : 'medium-12'; ?>">
            <div class="subtemplate-inner">
                <input <?php if ($placeholder) :
                    echo "placeholder='" . $placeholder . "'";
                endif; ?> style="<?= $color_text . $background_color . $border_width . $border_radius . $border_color . $border_style; ?>" name="<?= $field_name; ?>" minlength="<?= (int)$min ?>" id="<?= $field_id; ?>" title="<?= $label; ?>" value="<?= $block->escapeHtmlAttr($this->helper('Lof\Formbuilder\Helper\Data')->getPostValue($field_name) ?: $defaultValue) ?>" class="<?= $classes; ?>" type="<?= $type; ?>" data-units="<?= $min_max_length_units; ?>" <?= $attr; ?>/>
                <?php if ($icon) : ?>
                <i class="fa <?= $icon; ?> form-field-icon" style="color: <?= $icon_color; ?>;"></i>
                <?php endif; ?>
                <?php if ($field_help) : ?>
                <span class="help-block" style="<?= $color_description; ?>"><?= $field_help; ?></span>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>
