<?php
/**
 * ITSWEBER CMS – Page Builder Admin Form: Service Cards
 * Loaded via AJAX into section cards.
 * Migrated from plugins/service-cards-section.
 *
 * @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'];
$cards       = (isset($data['cards']) && is_array($data['cards'])) ? $data['cards'] : [];
$containerId = 'scs-cards-container-' . $sectionId;
$addBtnId    = 'scs-add-card-' . $sectionId;
?>

<?php admin_field('text', 'subtitle', __('admin.section.subtitle'), $data['subtitle'] ?? '', [
    'placeholder' => __('admin.section.service_ph_title'),
]) ?>

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

<?php admin_field('text', 'description', __('admin.section.description'), $data['description'] ?? '', [
    'placeholder' => __('admin.section.service_ph_text'),
]) ?>

<div class="form-group">
    <label class="form-label"><?= __('admin.section.service_items_label') ?></label>
    <div class="section-items-sortable" id="<?= $containerId ?>">
        <?php foreach ($cards as $idx => $card): ?>
        <?php admin_item_card_open($idx, __('admin.section.service_card_label')) ?>
            <div class="form-group">
                <label class="form-label"><?= __('admin.section.title') ?> *</label>
                <input type="text" name="card_title[]" class="form-control" value="<?= e($card['title'] ?? '') ?>" placeholder="<?= __('admin.section.service_ph_card_title') ?>" required>
            </div>
            <div class="form-group">
                <label class="form-label"><?= __('admin.section.service_price_label') ?></label>
                <input type="text" name="card_price[]" class="form-control" value="<?= e($card['price'] ?? '') ?>" placeholder="<?= __('admin.section.service_ph_price') ?>">
            </div>
            <div class="form-group full-width">
                <label class="form-label"><?= __('admin.section.description') ?></label>
                <textarea name="card_text[]" class="form-control" rows="2" placeholder="<?= __('admin.section.service_ph_desc') ?>"><?= e($card['text'] ?? '') ?></textarea>
            </div>
            <div class="form-group full-width">
                <label class="form-label"><?= __('admin.section.service_svg_label') ?></label>
                <textarea name="card_icon[]" class="form-control" rows="3" placeholder='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">...</svg>' style="font-family:var(--font-family-mono);font-size:var(--font-size-small);"><?= e($card['icon'] ?? '') ?></textarea>
                <small class="form-hint"><?= __('admin.section.service_svg_paste_hint') ?> <?= __('admin.section.service_svg_hint') ?></small>
            </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.service_add_card') ?></button>
</div>

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

    document.getElementById('<?= $addBtnId ?>').addEventListener('click', function() {
        var count = container.querySelectorAll('.admin-item-card').length;
        if (count >= 12) { alert('<?= __('admin.section.service_max_hint') ?>'); return; }
        var div = document.createElement('div');
        div.className = 'admin-item-card section-item-sortable';
        div.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">#' + (count+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"><?= __('admin.section.title') ?> *</label><input type="text" name="card_title[]" class="form-control" placeholder="<?= __('admin.section.service_ph_card_title') ?>" required></div>' +
                '<div class="form-group"><label class="form-label"><?= __('admin.section.service_price_label') ?></label><input type="text" name="card_price[]" class="form-control" placeholder="<?= __('admin.section.service_ph_price') ?>"></div>' +
                '<div class="form-group full-width"><label class="form-label"><?= __('admin.section.description') ?></label><textarea name="card_text[]" class="form-control" rows="2" placeholder="<?= __('admin.section.service_ph_desc') ?>"></textarea></div>' +
                '<div class="form-group full-width"><label class="form-label"><?= __('admin.section.service_svg_label') ?></label><textarea name="card_icon[]" class="form-control" rows="3" placeholder=\'<svg viewBox="0 0 24 24">...</svg>\' style="font-family:var(--font-family-mono);font-size:var(--font-size-small);"></textarea><small class="form-hint"><?= __('admin.section.service_svg_paste_hint') ?></small></div>' +
            '</div>';
        container.appendChild(div);
        attachHandlers(div);
    });

    container.querySelectorAll('.admin-item-card').forEach(attachHandlers);

    function attachHandlers(item) {
        var up = item.querySelector('.item-move-up'), dn = item.querySelector('.item-move-down'), rm = item.querySelector('.admin-item-remove');
        if (up) up.addEventListener('click', function(e) { e.preventDefault(); var p = item.previousElementSibling; if (p && p.classList.contains('admin-item-card')) container.insertBefore(item, p); });
        if (dn) dn.addEventListener('click', function(e) { e.preventDefault(); var n = item.nextElementSibling; if (n && n.classList.contains('admin-item-card')) container.insertBefore(n, item); });
        if (rm) rm.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'] ?? '2', [
        'options' => ['1' => __('admin.section.columns_auto'), '2' => __('admin.section.columns_2'), '3' => __('admin.section.columns_3')],
        'wrap_style' => 'margin:0;',
    ]) ?>
<?php admin_field_grid_close() ?>
