<?php
// pages/components/coupon_template_selection_modal.php

use Components\Button;
use Components\Base;
use Components\Models\Coupon;
use Components\Router;
use Components\Support\Htmx;
use Components\Ui\Stepper;

$isTemplate = (int)\mh_get('is_template', 1, 'int');
if ($isTemplate <= 0) { $isTemplate = (int)\mh_post('is_template', 1); }

$panelId = Htmx::resolvePanelId();
$presets = Coupon::getPresetsWithIds();
$steps = [
    [
        'id' => 'preset',
        'label' => (string)\mh_lng('UI_STEP_COUPON_PRESET', 'Select Pre-Set'),
        'description' => (string)\mh_lng('UI_STEP_COUPON_PRESET_DESC', 'Choose from commonly used coupon settings'),
    ],
    [
        'id' => 'details',
        'label' => (string)\mh_lng('UI_STEP_COUPON_FINALIZE', 'Finalize'),
        'description' => (string)\mh_lng('UI_STEP_COUPON_FINALIZE_DESC', 'Customize settings, labels, and restrictions'),
    ],
];

ob_start();
?>
<div>
    <script>
        (function() {
            var m = document.getElementById('<?= $panelId; ?>');
            if (m) {
                m.setAttribute('data-title', <?= json_encode(\mh_lng('UI_TITLE_CREATE_TEMPLATE_COUPON', 'Create New')); ?>);
                m.setAttribute('data-size', '4xl-sm');
            }
        })();
    </script>

    <div class="absolute z-100 w-full px-6 py-4 border-b border-zinc-200 bg-zinc-50/80 -m-6">
        <?= Stepper::render($steps, 'preset') ?>
    </div>
    <div class="h-18"></div>

    <div class="space-y-4">
        <?php foreach ($presets as $group): ?>
            <div class="mb-4 last:mb-0">
                <div class="px-1 mb-2 text-[11px] font-bold text-secondary uppercase tracking-wider">
                    <?= Base::h($group['group'] ?? '') ?>
                </div>
                <div class="grid grid-cols-3 gap-2">
                    <?php foreach (($group['items'] ?? []) as $item): ?>
                        <?php
                        $presetId = (string)($item['id'] ?? '');
                        ?>
                        <?= Button::render($item['label'] ?? '', [
                            'variant' => 'outline',
                            'size' => 'sm',
                            'fullWidth' => true,
                            'attrs' => [
                                'hx-get' => Router::url('coupon_modal', ['partial' => 1, 'mode' => 'new', 'is_template' => $isTemplate, 'preset_id' => $presetId]),
                                'hx-target' => '#config-modal-body',
                                'hx-swap' => 'innerHTML',
                                'hx-push-url' => 'false',
                            ],
                        ]) ?>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="pt-4 border-t border-divider mt-4">
        <?= Button::render(\mh_lng('UI_PRESET_FROM_SCRATCH', 'Start from scratch'), [
            'fullWidth' => true,
            'variant' => 'outline',
            'size' => 'md',
            'icon' => Base::SVG_PLUS,
            'attrs' => [
                'hx-get' => Router::url('coupon_modal', ['partial' => 1, 'mode' => 'new', 'is_template' => $isTemplate, 'preset_id' => 'empty']),
                'hx-target' => '#config-modal-body',
                'hx-swap' => 'innerHTML',
                'hx-push-url' => 'false',
            ],
        ]) ?>
    </div>
</div>
<?php
echo ob_get_clean();
