<div class="page-header">
    <div>
        <h1>Vouchers</h1>
        <p>Manage discount coupon codes for customers.</p>
    </div>
    <?php if (\Core\Auth::can('voucher.create')): ?>
    <a href="<?= url('vouchers/create') ?>" class="btn btn-accent">
        <i class="bi bi-plus-lg me-1"></i> Create Voucher
    </a>
    <?php endif; ?>
</div>

<!-- Filter -->
<div class="card mb-4">
    <div class="card-body py-3">
        <form method="GET" action="<?= url('vouchers') ?>" class="row g-2 align-items-end">
            <div class="col-12 col-md-5">
                <div class="input-group input-group-sm">
                    <span class="input-group-text bg-white"><i class="bi bi-search text-secondary"></i></span>
                    <input type="text" name="search" class="form-control"
                           placeholder="Search code or description…" value="<?= e($filters['search']) ?>">
                </div>
            </div>
            <div class="col-6 col-md-2">
                <select name="is_active" class="form-select form-select-sm">
                    <option value="">All Status</option>
                    <option value="1" <?= $filters['is_active']==='1'?'selected':'' ?>>Active</option>
                    <option value="0" <?= $filters['is_active']==='0'?'selected':'' ?>>Inactive</option>
                </select>
            </div>
            <div class="col-6 col-md-3 d-flex gap-2">
                <button type="submit" class="btn btn-accent btn-sm px-3"><i class="bi bi-funnel me-1"></i>Filter</button>
                <a href="<?= url('vouchers') ?>" class="btn btn-outline-secondary btn-sm px-3"><i class="bi bi-x-lg"></i></a>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="bi bi-ticket-perforated-fill text-accent"></i>
        <h6 class="card-title">All Vouchers</h6>
        <span class="ms-auto text-secondary" style="font-size:.8rem;"><?= $total ?> total</span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($vouchers)): ?>
        <div class="text-center py-5 text-secondary">
            <i class="bi bi-ticket-perforated fs-1 d-block mb-3 opacity-25"></i>
            <h6>No vouchers yet</h6>
            <a href="<?= url('vouchers/create') ?>" class="btn btn-accent btn-sm mt-2">Create First Voucher</a>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th>Code</th>
                        <th>Type</th>
                        <th class="text-end">Value</th>
                        <th class="text-end">Min Order</th>
                        <th class="text-center">Usage</th>
                        <th>Validity</th>
                        <th class="text-center">Status</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($vouchers as $v): ?>
                    <?php
                        $expired = strtotime($v['expires_at']) < time();
                        $started = strtotime($v['starts_at']) <= time();
                    ?>
                    <tr>
                        <td>
                            <code class="fw-700 text-accent" style="font-size:.9rem;"><?= e($v['code']) ?></code>
                            <?php if ($v['description']): ?>
                            <div class="text-secondary" style="font-size:.75rem;"><?= e(str_limit($v['description'],50)) ?></div>
                            <?php endif; ?>
                        </td>
                        <td>
                            <span class="badge bg-light text-dark border" style="font-size:.75rem;">
                                <?= $v['discount_type'] === 'percentage' ? 'Percentage' : 'Fixed' ?>
                            </span>
                        </td>
                        <td class="text-end fw-700">
                            <?= $v['discount_type'] === 'percentage'
                                ? $v['discount_value'] . '%'
                                : money((float)$v['discount_value']) ?>
                            <?php if ($v['max_discount']): ?>
                            <div class="text-secondary" style="font-size:.72rem;">max <?= money((float)$v['max_discount']) ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="text-end"><?= money((float)$v['min_order_amt']) ?></td>
                        <td class="text-center">
                            <span class="fw-600"><?= number_format($v['used_count']) ?></span>
                            <?php if ($v['usage_limit']): ?>
                            <span class="text-secondary"> / <?= $v['usage_limit'] ?></span>
                            <?php else: ?>
                            <span class="text-secondary"> / ∞</span>
                            <?php endif; ?>
                        </td>
                        <td style="font-size:.78rem;">
                            <div><?= format_date($v['starts_at']) ?></div>
                            <div class="text-secondary">→ <?= format_date($v['expires_at']) ?></div>
                            <?php if ($expired): ?>
                            <span class="badge bg-danger" style="font-size:.65rem;">Expired</span>
                            <?php elseif (!$started): ?>
                            <span class="badge bg-warning text-dark" style="font-size:.65rem;">Upcoming</span>
                            <?php else: ?>
                            <span class="badge bg-success" style="font-size:.65rem;">Active</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <div class="form-check form-switch d-flex justify-content-center mb-0">
                                <input class="form-check-input toggle-status" type="checkbox" role="switch"
                                       <?= $v['is_active'] ? 'checked' : '' ?>
                                       data-url="<?= url('vouchers/'.$v['id'].'/toggle') ?>"
                                       <?= !\Core\Auth::can('voucher.edit') ? 'disabled' : '' ?>>
                            </div>
                        </td>
                        <td class="text-center">
                            <div class="d-flex gap-1 justify-content-center">
                                <?php if (\Core\Auth::can('voucher.edit')): ?>
                                <a href="<?= url('vouchers/'.$v['id'].'/edit') ?>" class="btn btn-sm btn-outline-primary" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <?php endif; ?>
                                <?php if (\Core\Auth::can('voucher.delete')): ?>
                                <form method="POST" action="<?= url('vouchers/'.$v['id'].'/delete') ?>">
                                    <?= csrf_field() ?>
                                    <button class="btn btn-sm btn-outline-danger"
                                            data-confirm="Delete voucher '<?= e($v['code']) ?>'?">
                                        <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($vouchers) ?> of <?= $total ?></small>
            <?= paginate($total, $page, ITEMS_PER_PAGE, url('vouchers')) ?>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
