<?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\Fields;

$helper           = $this->helper(Fields::class);
$dataHelper       = $this->helper(Lof\Formbuilder\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'];
$required         = $field['required'];
$cid              = $dataHelper->getFieldId($field);
$fieldName       = $helper->getFieldPrefix() . $cid . $form->getId();
$fieldId          = $fieldName.uniqid();
$availableFields = $helper->getAvailableFields();
$fieldValidate   = $availableFields[$fieldType] ?? '';
$validates        = $fieldValidate;
$fieldHelp       = isset($options['description']) ? $block->filterMessage($options['description']) : '';
$inlineCss        = $field['inline_css'] ?? '';
if ($fieldValidate && is_array($fieldValidate)) {
    $validates = implode(" ", $fieldValidate);
}
$limit = isset($field['limit']) ? (int)$field['limit'] : 5;
$default = isset($field['default']) ? (float)$field['default'] : 0;

$frameWorkCss   = '';
if (isset($field['frame_work_css'])) {
    $frameWorkCss      = $field['frame_work_css'];
}
if ($frameWorkCss == 'foundation') {
    $medium = 'columns medium-';
} else {
    $medium = 'col-sm-';
}
?>
<div class="subtemplate-wrapper" style="<?= $inlineCss ?>">
    <div class="row">
        <?php if ($label && $label != '') { ?>
            <div class="input-box-label <?= $medium; ?>6">
                <label for="<?= $fieldId; ?>" class="<?= (1 == $required) ? 'required' : ''; ?>"><?= __($label); ?><?= (1 == $required) ? '<em>*</em>' : '';?></label>
            </div>
        <?php } ?>
        <div class="input-box <?= $medium; ?>6">
            <input name="<?= $fieldName; ?>" id="<?= $fieldId; ?>" title="<?= $label; ?>" value="<?= ($formData && isset($formData[$fieldName])) ? $formData[$fieldName] : ''; ?>" class="input-text <?= (1 == $required) ? 'required-entry' : ''; ?>" type="hidden"/>
            <input name="<?= $fieldName."_limit"; ?>" id="<?= $fieldId."_limit"; ?>" title="<?= $label; ?>" value="<?= $limit; ?>" class="input-text" type="hidden"/>
            <div id="<?= $fieldId."_rating"; ?>" class="ves-rating-play"></div>
            <script type="text/javascript">
                require(['jquery', 'Lof_Formbuilder/js/raty/raty'], function(){
                    jQuery('#<?= $fieldId."_rating"; ?>').raty({
                        path: '<?= $block->escapeUrl($dataHelper->getMediaUrl()."lof/formbuilder/rating/"); ?>',
                        score: <?= $default; ?>,
                        number: <?= $limit; ?>,
                        targetScore: '#<?= $fieldId; ?>',
                        click: function(score, evt) {
                            jQuery('#<?= $fieldId; ?>').val(score);
                            return false;
                        }
                    });
                });
            </script>
            <?php if ($fieldHelp) { ?>
            <span class="help-block"><?= $fieldHelp; ?></span>
            <?php } ?>
        </div>
    </div>
</div>
