<?php
if (is_post()) {
    csrf_check();
    db_delete('coupons', post_int('sil'));
    flash('Kupon silindi.');
    back();
}

$coupons = db_all('SELECT * FROM coupons ORDER BY id DESC');
$today = date('Y-m-d');

admin_header('Kuponlar', '<a href="' . admin_url('kupon') . '" class="btn-primary btn-sm">' . icon('plus', 'size-4', '2') . 'Yeni kupon</a>');
?>

<div class="card overflow-x-auto">
    <table class="w-full min-w-[760px] text-left text-sm">
        <thead class="border-b border-zinc-200 text-xs text-zinc-500">
            <tr>
                <th class="px-5 py-3 font-medium">Kod</th>
                <th class="px-5 py-3 font-medium">İndirim</th>
                <th class="px-5 py-3 font-medium">Alt limit</th>
                <th class="px-5 py-3 font-medium">Kullanım</th>
                <th class="px-5 py-3 font-medium">Geçerlilik</th>
                <th class="px-5 py-3 font-medium">Durum</th>
                <th class="px-5 py-3"></th>
            </tr>
        </thead>
        <tbody class="divide-y divide-zinc-100">
            <?php foreach ($coupons as $coupon): ?>
                <?php $expired = $coupon['ends_at'] && $coupon['ends_at'] < $today ?>
                <tr class="hover:bg-zinc-50">
                    <td class="px-5 py-3"><a href="<?= admin_url('kupon?id=' . $coupon['id']) ?>" class="rounded-md bg-zinc-100 px-2 py-1 font-mono text-[13px] font-semibold hover:bg-zinc-200"><?= e($coupon['code']) ?></a></td>
                    <td class="px-5 py-3 font-medium"><?= coupon_label($coupon) ?></td>
                    <td class="px-5 py-3 text-zinc-600"><?= $coupon['min_total'] > 0 ? money($coupon['min_total']) : '—' ?></td>
                    <td class="px-5 py-3 text-zinc-600"><?= (int) $coupon['used'] ?><?= $coupon['usage_limit'] ? ' / ' . (int) $coupon['usage_limit'] : '' ?></td>
                    <td class="px-5 py-3 text-zinc-600"><?= $coupon['ends_at'] ? date_tr($coupon['ends_at']) . ' tarihine kadar' : 'Süresiz' ?></td>
                    <td class="px-5 py-3"><?= $expired ? '<span class="text-xs font-semibold text-zinc-400">Süresi doldu</span>' : status_badge($coupon['status']) ?></td>
                    <td class="px-5 py-3">
                        <div class="flex justify-end gap-1">
                            <a href="<?= admin_url('kupon?id=' . $coupon['id']) ?>" class="btn-ghost btn-sm p-2"><?= icon('pencil', 'size-4') ?></a>
                            <form method="post" data-confirm="Kupon silinsin mi?">
                                <?= csrf_field() ?>
                                <button name="sil" value="<?= $coupon['id'] ?>" class="btn-ghost btn-sm p-2 text-zinc-400 hover:text-rose-600"><?= icon('trash', 'size-4') ?></button>
                            </form>
                        </div>
                    </td>
                </tr>
            <?php endforeach ?>
            <?php if (!$coupons): ?>
                <tr><td colspan="7" class="px-5 py-16 text-center text-zinc-500">Henüz kupon yok.</td></tr>
            <?php endif ?>
        </tbody>
    </table>
</div>

<?php admin_footer() ?>
