<?php
/**
 * ITSWEBER CMS – Page Builder Admin Form: Stats / Key Figures
 * Loaded via AJAX into section cards.
 *
 * @author  Benny Weber
 * @copyright ITSWEBER, www.itsweber.de
 *
 * @var array $data    Decoded JSON content
 * @var array $section DB row (id, section_type, instance_label, ...)
 */

$sectionId   = (int) $section['id'];
$items       = (isset($data['items']) && is_array($data['items'])) ? $data['items'] : [];
$containerId = 'stats-items-container-' . $sectionId;
$addBtnId    = 'stats-add-item-' . $sectionId;
?>

<?php admin_field('text', 'title', __('admin.section.heading'), $data['title'] ?? '', [
    'placeholder' => 'z.B. Unsere Erfolge',
]) ?>

<?php admin_field('text', 'subtitle', __('admin.section.subtitle'), $data['subtitle'] ?? '', [
    'placeholder' => 'z.B. Zahlen die fuer sich sprechen',
]) ?>

<div class="form-group">
    <label class="form-label">Kennzahlen</label>
    <div class="section-items-sortable" id="<?= $containerId ?>">
        <?php foreach ($items as $idx => $item): ?>
        <?php admin_item_card_open($idx, 'Kennzahl') ?>
            <div class="form-group">
                <label class="form-label">Wert *</label>
                <input type="text" name="stat_value[]" class="form-control" value="<?= e($item['value'] ?? '') ?>" required placeholder="z.B. 150">
            </div>
            <div class="form-group">
                <label class="form-label">Label</label>
                <input type="text" name="stat_label[]" class="form-control" value="<?= e($item['label'] ?? '') ?>" placeholder="z.B. Kunden">
            </div>
            <div class="form-group">
                <label class="form-label">Prefix</label>
                <input type="text" name="stat_prefix[]" class="form-control" value="<?= e($item['prefix'] ?? '') ?>" placeholder="z.B. +">
            </div>
            <div class="form-group">
                <label class="form-label">Suffix</label>
                <input type="text" name="stat_suffix[]" class="form-control" value="<?= e($item['suffix'] ?? '') ?>" placeholder="z.B. %">
            </div>
            <div class="form-group">
                <label class="form-label">Icon (Lucide Name)</label>
                <input type="text" name="stat_icon[]" class="form-control" value="<?= e($item['icon'] ?? '') ?>" placeholder="z.B. users">
            </div>
        <?php admin_item_card_close() ?>
        <?php endforeach; ?>
    </div>
    <button type="button" class="btn btn-secondary btn-sm" id="<?= $addBtnId ?>" style="margin-top:var(--space-xs);"><?= __('admin.section.add_item') ?></button>
</div>

<script>
(function() {
    var c = document.getElementById('<?= $containerId ?>');
    if (!c) return;

    document.getElementById('<?= $addBtnId ?>').addEventListener('click', function() {
        var n = c.querySelectorAll('.admin-item-card').length;
        if (n >= 8) { alert('Maximal 8 Kennzahlen'); return; }
        var d = document.createElement('div');
        d.className = 'admin-item-card section-item-sortable';
        d.innerHTML =
            '<div class="admin-item-card-header">' +
                '<div class="item-move-buttons"><button type="button" class="btn btn-xs item-move-up">&#9650;</button><button type="button" class="btn btn-xs item-move-down">&#9660;</button></div>' +
                '<strong class="admin-item-card-label">#' + (n+1) + '</strong>' +
                '<button type="button" class="btn btn-xs btn-danger admin-item-remove">&#10005; <?= __('admin.section.remove_item') ?></button>' +
            '</div>' +
            '<div class="admin-item-card-body">' +
                '<div class="form-group"><label class="form-label">Wert *</label><input type="text" name="stat_value[]" class="form-control" required placeholder="z.B. 150"></div>' +
                '<div class="form-group"><label class="form-label">Label</label><input type="text" name="stat_label[]" class="form-control" placeholder="z.B. Kunden"></div>' +
                '<div class="form-group"><label class="form-label">Prefix</label><input type="text" name="stat_prefix[]" class="form-control" placeholder="z.B. +"></div>' +
                '<div class="form-group"><label class="form-label">Suffix</label><input type="text" name="stat_suffix[]" class="form-control" placeholder="z.B. %"></div>' +
                '<div class="form-group"><label class="form-label">Icon (Lucide Name)</label><input type="text" name="stat_icon[]" class="form-control" placeholder="z.B. users"></div>' +
            '</div>';
        c.appendChild(d);
        a(d);
    });

    c.querySelectorAll('.admin-item-card').forEach(a);

    function a(item) {
        var u=item.querySelector('.item-move-up'),d=item.querySelector('.item-move-down'),r=item.querySelector('.admin-item-remove');
        if(u)u.addEventListener('click',function(e){e.preventDefault();var p=item.previousElementSibling;if(p&&p.classList.contains('admin-item-card'))c.insertBefore(item,p);});
        if(d)d.addEventListener('click',function(e){e.preventDefault();var n=item.nextElementSibling;if(n&&n.classList.contains('admin-item-card'))c.insertBefore(n,item);});
        if(r)r.addEventListener('click',function(e){e.preventDefault();item.remove();});
    }
})();
</script>

<?php admin_section_separator() ?>

<?php admin_field_grid_open(3) ?>
    <?php admin_color_scheme_field($data['color_scheme'] ?? '') ?>
    <?php render_section_text_color_field($data) ?>
    <?php admin_field('select', 'columns', __('admin.section.columns'), $data['columns'] ?? '4', [
        'options' => ['2' => __('admin.section.columns_2'), '3' => __('admin.section.columns_3'), '4' => __('admin.section.columns_4')],
        'wrap_style' => 'margin:0;',
    ]) ?>
<?php admin_field_grid_close() ?>
