<?php
/**
 * AFD PCE Categorised Metadata Fieldset Template
 *
 * @var $block \Afd\Pce\Block\Adminhtml\Form\Field\CategorisedMetadataFieldset
 */

// Get element from block
$element = $block->getElement();

// Get current selected values
$values = $element->getValue() ? explode(',', $element->getValue()) : [];

// Get asset URLs
$cssUrl = $block->getViewFileUrl('Afd_Pce::css/categorised_metadata_fieldset.css');
$jsUrl = $block->getViewFileUrl('Afd_Pce::js/categorised_metadata_fieldset.js');
?>

<!-- Include external CSS file -->
<link rel="stylesheet" type="text/css" href="<?= $block->escapeUrl($cssUrl) ?>" />

<script type="text/javascript">
    // Initialize the external JS module
    require(['Afd_Pce/js/categorised_metadata_fieldset'], function(categorisedMetadataFieldset) {
        categorisedMetadataFieldset({});
    });
</script>

<div class="categorised-metadata-fields">
    <?php foreach ($block->getCategorisedFields()->getCategories() as $categoryCode => $categoryLabel): ?>
        <div class="metadata-category">
            <div class="category-header" data-category="<?= $block->escapeHtmlAttr($categoryCode) ?>">
                <span class="category-toggle">+</span>
                <strong><?= $block->escapeHtml($categoryLabel) ?></strong>
            </div>
            <div class="category-fields" id="category-<?= $block->escapeHtmlAttr($categoryCode) ?>" style="display:none;">
                <?php 
                $fields = $block->getCategorisedFields()->getFieldsByCategory()[$categoryCode] ?? [];
                $descriptions = $block->getCategorisedFields()->getFieldDescriptions();
                ?>
                
                <?php foreach ($fields as $value => $label): ?>
                    <?php $checked = in_array($value, $values) ? 'checked="checked"' : ''; ?>
                    <div class="field-option">
                        <input 
                            type="checkbox" 
                            class="category-field" 
                            id="<?= $block->escapeHtmlAttr($element->getHtmlId() . '_' . $value) ?>" 
                            name="<?= $block->escapeHtmlAttr($element->getName()) ?>[]" 
                            value="<?= $block->escapeHtmlAttr($value) ?>" 
                            <?= $checked ?>
                        />
                        <label for="<?= $block->escapeHtmlAttr($element->getHtmlId() . '_' . $value) ?>">
                            <?= $block->escapeHtml($label) ?>
                            <?php if (isset($descriptions[$value])): ?>
                                <span class="field-info" title="<?= $block->escapeHtmlAttr(__('Hover for more information')) ?>">ℹ️
                                    <div class="field-tooltip">
                                        <div class="tooltip-content">
                                            <?= $block->escapeHtml($descriptions[$value]) ?>
                                        </div>
                                    </div>
                                </span>
                            <?php endif; ?>
                        </label>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<!-- No hidden field needed - checkboxes submit directly -->
