<?php
$status = get('durum', '');
$q = get('q', '');

$where = '1 = 1';
$params = [];
if (isset(order_statuses()[$status])) {
    $where .= ' AND status = ?';
    $params[] = $status;
}
if ($q !== '') {
    $where .= ' AND (order_no LIKE ? OR name LIKE ? OR email LIKE ? OR phone LIKE ?)';
    array_push($params, "%$q%", "%$q%", "%$q%", "%$q%");
}

$counts = array_column(db_all('SELECT status, COUNT(*) AS total FROM orders GROUP BY status'), 'total', 'status');
$total = db_value("SELECT COUNT(*) FROM orders WHERE $where", $params);
$pager = paginate($total, ADMIN_PER_PAGE, get_int('sayfa', 1));
$orders = db_all("SELECT * FROM orders WHERE $where ORDER BY id DESC LIMIT {$pager['limit']} OFFSET {$pager['offset']}", $params);

admin_header('Siparişler');
?>

<div class="flex flex-wrap items-center justify-between gap-4">
    <div class="flex flex-wrap gap-1.5">
        <a href="<?= admin_url('siparisler') ?>" class="rounded-full px-3.5 py-1.5 text-[13px] font-medium transition <?= $status === '' ? 'bg-ink text-white' : 'bg-white text-zinc-600 ring-1 ring-zinc-200 hover:text-ink' ?>">Tümü <span class="opacity-60"><?= array_sum($counts) ?></span></a>
        <?php foreach (order_statuses() as $key => [$label]): ?>
            <a href="<?= admin_url('siparisler?durum=' . $key) ?>" class="rounded-full px-3.5 py-1.5 text-[13px] font-medium transition <?= $status === $key ? 'bg-ink text-white' : 'bg-white text-zinc-600 ring-1 ring-zinc-200 hover:text-ink' ?>"><?= $label ?> <span class="opacity-60"><?= (int) ($counts[$key] ?? 0) ?></span></a>
        <?php endforeach ?>
    </div>
    <form class="flex gap-2">
        <?php if ($status): ?><input type="hidden" name="durum" value="<?= e($status) ?>"><?php endif ?>
        <input name="q" value="<?= e($q) ?>" placeholder="No, ad, e-posta, telefon" class="input w-64 py-2">
        <button class="btn-light btn-sm"><?= icon('search', 'size-4') ?></button>
    </form>
</div>

<div class="card mt-6 overflow-x-auto">
    <table class="w-full min-w-[800px] text-left text-sm">
        <thead class="border-b border-zinc-200 text-xs font-medium text-zinc-500">
            <tr>
                <th class="px-5 py-3 font-medium">Sipariş</th>
                <th class="px-5 py-3 font-medium">Müşteri</th>
                <th class="px-5 py-3 font-medium">Tarih</th>
                <th class="px-5 py-3 font-medium">Ödeme</th>
                <th class="px-5 py-3 font-medium">Durum</th>
                <th class="px-5 py-3 text-right font-medium">Tutar</th>
            </tr>
        </thead>
        <tbody class="divide-y divide-zinc-100">
            <?php foreach ($orders as $order): ?>
                <tr class="cursor-pointer hover:bg-zinc-50" onclick="location.href='<?= admin_url('siparis?id=' . $order['id']) ?>'">
                    <td class="px-5 py-3.5 font-semibold">#<?= e($order['order_no']) ?></td>
                    <td class="px-5 py-3.5">
                        <p class="font-medium"><?= e($order['name']) ?></p>
                        <p class="text-xs text-zinc-500"><?= e($order['email']) ?></p>
                    </td>
                    <td class="px-5 py-3.5 text-zinc-600"><?= date_tr($order['created_at'], true) ?></td>
                    <td class="px-5 py-3.5 text-zinc-600"><?= payment_label($order['payment']) ?></td>
                    <td class="px-5 py-3.5"><?= order_status_badge($order['status']) ?></td>
                    <td class="px-5 py-3.5 text-right font-semibold"><?= money($order['total']) ?></td>
                </tr>
            <?php endforeach ?>
            <?php if (!$orders): ?>
                <tr><td colspan="6" class="px-5 py-16 text-center text-zinc-500">Sipariş bulunamadı.</td></tr>
            <?php endif ?>
        </tbody>
    </table>
</div>

<?php pagination($pager) ?>

<?php admin_footer() ?>
