<?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_type       = $field['field_type'];
$label            = $block->filterMessage($field['label']);
$options          = $field['field_options'];
$required         = $field['required'];
$show_position    = isset($field['show_position'])?$field['show_position']:1;
$max_model_level  = isset($field['max_level'])?$field['max_level']:2;
$cid         = $helper_data->getFieldId($field);
$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']).' ') : '';
if ($field_validate && is_array($field_validate)) {
    $validates = implode(" ", $field_validate);
}
$validates .= " ".$cssClass;
$total = count($options['category_id']);
?>
<div class="subtemplate-wrapper" style="<?= $inlineCss ?>">
    <label for="<?= $field_name; ?>" class="<?= (1==$required)?'required':''; ?>"><?= $label; ?><?= (1==$required)?'<em>*</em>':'';?></label>
    <div class="input-box">
        <?php
        if ($options['category_id']): ?>

            <?php foreach ($options['category_id'] as $key => $val) {
                $category_id = $val['value'];
                $categories = [];
                $categories = $this->getCategories((int)$category_id, $max_model_level);
                ?>
                <?php if ($key == 0): ?>
            <div class="row">
                    <?php if ($show_position == 1): ?><td><?= __("Pos"); ?></td><?php endif; ?>
                    <?php if ($categories): ?>
                        <?php foreach ($categories as $k => $item): ?>
                <td>
                            <?= $item['label']; ?>
                </td>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
<?php endif; ?>
<?php }; ?>
<table class="workflow_table">
    <tbody>
            <?php foreach ($options['category_id'] as $key => $val):
                $category_id = $val['value'];
                $categories = [];
                $categories = $this->getCategories((int)$category_id, $max_model_level);

                ?>
                <?php if ($key == 0): ?>
            <tr>
                    <?php if ($show_position == 1): ?><td><?= __("Pos"); ?></td><?php endif; ?>
                    <?php if ($categories): ?>
                        <?php foreach ($categories as $k => $item): ?>
                        <td>
                            <?= $item['label']; ?>
                        </td>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tr>
        <?php endif; ?>
        <tr>
                <?php if ($show_position == 1): ?>
                <td style="width:25px">
                    <b>
                        <?= ($key+1)."."; ?>
                    </b>
                </td>
            <?php endif; ?>
                <?php if ($categories): ?>
                    <?php $i = 0;
                    $key_name = "";
                    foreach ($categories as $k => $item): $key_name.= "[".$item["id"]."]";?>
                <td>
                    <select name="<?= $field_name."[".$key."][".$item["id"]."]"; ?>" id="<?= $field_name."_".$k.'_'.$key; ?>" class="<?= (1==$required)?'required-entry':''; ?> <?= $validates; ?>">
                        <option value=""><?= __("Select a option");?></option>
                                            <?php if ($item['models']) {
                                                foreach ($item['models'] as $tmp_item) { ?>
                                <option value="<?= $helper->escapeQuote($tmp_item->getTitle()); ?>" data-id="<?= $tmp_item->getId(); ?>"><?= $tmp_item->getTitle(); ?></option>
                                                    <?php
                                                }
                                            }
                                            ?>
                    </select>
                                        <?php if ($i > 0): ?>
                        <script type="text/javascript">
                            require(['prototype'], function(prototype){
                            //&lt;![CDATA[
                            FORM_KEY = '<?= $block->getFormKey(); ?>';
                            new Form.Element.Observer('<?= $field_name."_".($k-1).'_'.$key; ?>', 1, function(element, value) {
                                new Ajax.Request('<?= $this->getDefaultSelectUrl(); ?>', {
                                    asynchronous:true,
                                    evalScripts:true,
                                    parameters:'store_id=<?= $block->getStore()->getStoreId(); ?>' + '&' + 'target_id=<?= $field_name."_".$k.'_'.$key; ?>' + '&' + 'value=' + $('<?= $field_name."_".($k-1).'_'.$key; ?>')[$('<?= $field_name."_".($k-1).'_'.$key; ?>'
                                        ).selectedIndex].readAttribute("data-id") })})
                            //]]&gt;
                        });
                    </script>
                <?php endif; ?>
            </td>
                                <?php $i++;
endforeach; ?>
        <?php endif; ?>
    </tr>
<?php endforeach; ?>
</tbody>
</table>
  <?php endif; ?>
<?php if ($field_help) { ?>
    <span class="help-block"><?= $field_help ?></span>
    <?php } ?>
</div>
</div>
