<div class="page-header">
    <div><h1>Discount Offers</h1><p>Item, category or order-level discounts.</p></div>
    <?php if (\Core\Auth::can('voucher.create')): ?>
    <a href="<?= url('discounts/create') ?>" class="btn btn-accent"><i class="bi bi-plus-lg me-1"></i> Add Discount</a>
    <?php endif; ?>
</div>

<div class="card">
    <div class="card-header">
        <i class="bi bi-percent text-accent"></i>
        <h6 class="card-title">All Discount Offers</h6>
        <span class="ms-auto text-secondary" style="font-size:.8rem;"><?= $total ?> total</span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($discounts)): ?>
        <div class="text-center py-5 text-secondary">
            <i class="bi bi-percent fs-1 d-block mb-3 opacity-25"></i>
            <h6>No discount offers yet</h6>
            <a href="<?= url('discounts/create') ?>" class="btn btn-accent btn-sm mt-2">Add First Discount</a>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Applies To</th>
                        <th>Target</th>
                        <th class="text-end">Value</th>
                        <th>Validity</th>
                        <th class="text-center">Status</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($discounts as $d): ?>
                    <?php $expired = strtotime($d['expires_at']) < time(); ?>
                    <tr>
                        <td><span class="fw-600"><?= e($d['title']) ?></span></td>
                        <td>
                            <span class="badge bg-light text-dark border" style="font-size:.75rem;">
                                <?= ucfirst($d['applies_to']) ?>
                            </span>
                        </td>
                        <td class="text-secondary" style="font-size:.82rem;">
                            <?= e($d['target_label'] ?? 'All Orders') ?>
                        </td>
                        <td class="text-end fw-700 text-accent">
                            <?= $d['discount_type'] === 'percentage'
                                ? $d['discount_value'] . '%'
                                : money((float)$d['discount_value']) ?>
                        </td>
                        <td style="font-size:.78rem;">
                            <?= format_date($d['starts_at']) ?> → <?= format_date($d['expires_at']) ?>
                            <?php if ($expired): ?>
                            <span class="badge bg-danger ms-1" style="font-size:.65rem;">Expired</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <span class="badge <?= $d['is_active'] && !$expired ? 'bg-success' : 'bg-secondary' ?>">
                                <?= $d['is_active'] && !$expired ? 'Active' : 'Inactive' ?>
                            </span>
                        </td>
                        <td class="text-center">
                            <div class="d-flex gap-1 justify-content-center">
                                <?php if (\Core\Auth::can('voucher.edit')): ?>
                                <a href="<?= url('discounts/'.$d['id'].'/edit') ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <?php endif; ?>
                                <?php if (\Core\Auth::can('voucher.delete')): ?>
                                <form method="POST" action="<?= url('discounts/'.$d['id'].'/delete') ?>">
                                    <?= csrf_field() ?>
                                    <button class="btn btn-sm btn-outline-danger"
                                            data-confirm="Delete discount '<?= e($d['title']) ?>'?">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                                <?php endif; ?>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php if ($total > ITEMS_PER_PAGE): ?>
        <div class="d-flex justify-content-between align-items-center px-4 py-3 border-top">
            <small class="text-secondary">Showing <?= count($discounts) ?> of <?= $total ?></small>
            <?= paginate($total, $page, ITEMS_PER_PAGE, url('discounts')) ?>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
