<?php
if (is_post()) {
    csrf_check();
    $category = db_find('categories', post_int('sil'));
    if ($category) {
        if (db_value('SELECT COUNT(*) FROM categories WHERE parent_id = ?', [$category['id']])) {
            flash('Önce bu kategorinin alt kategorilerini silin ya da taşıyın.', 'error');
        } else {
            db_query('UPDATE products SET category_id = NULL WHERE category_id = ?', [$category['id']]);
            upload_delete($category['image']);
            db_delete('categories', $category['id']);
            flash('"' . $category['name'] . '" silindi. Ürünleri kategorisiz kaldı.');
        }
    }
    back();
}

$counts = array_column(db_all('SELECT category_id, COUNT(*) AS total FROM products GROUP BY category_id'), 'total', 'category_id');
$categories = category_tree();

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

<div class="card overflow-x-auto">
    <table class="w-full min-w-[640px] 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">Kategori</th>
                <th class="px-5 py-3 text-right font-medium">Ürün</th>
                <th class="px-5 py-3 text-right font-medium">Sıra</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 ($categories as $category): ?>
                <tr class="hover:bg-zinc-50">
                    <td class="px-5 py-3">
                        <a href="<?= admin_url('kategori?id=' . $category['id']) ?>" class="flex items-center gap-3" style="padding-left: <?= $category['depth'] * 1.75 ?>rem">
                            <?php if ($category['depth']): ?><span class="text-zinc-300">└</span><?php endif ?>
                            <span class="size-9 shrink-0 overflow-hidden rounded-lg bg-zinc-100"><?= image_tag($category['image'], $category['name'], 'h-full w-full object-cover', 'text-sm') ?></span>
                            <span class="font-medium hover:underline"><?= e($category['name']) ?></span>
                        </a>
                    </td>
                    <td class="px-5 py-3 text-right text-zinc-700"><?= (int) ($counts[$category['id']] ?? 0) ?></td>
                    <td class="px-5 py-3 text-right text-zinc-500"><?= (int) $category['sort'] ?></td>
                    <td class="px-5 py-3"><?= status_badge($category['status']) ?></td>
                    <td class="px-5 py-3">
                        <div class="flex justify-end gap-1">
                            <a href="<?= admin_url('kategori?id=' . $category['id']) ?>" class="btn-ghost btn-sm p-2" title="Düzenle"><?= icon('pencil', 'size-4') ?></a>
                            <form method="post" data-confirm="Bu kategori silinsin mi?">
                                <?= csrf_field() ?>
                                <button name="sil" value="<?= $category['id'] ?>" class="btn-ghost btn-sm p-2 text-zinc-400 hover:text-rose-600" title="Sil"><?= icon('trash', 'size-4') ?></button>
                            </form>
                        </div>
                    </td>
                </tr>
            <?php endforeach ?>
            <?php if (!$categories): ?>
                <tr><td colspan="5" class="px-5 py-16 text-center text-zinc-500">Henüz kategori yok.</td></tr>
            <?php endif ?>
        </tbody>
    </table>
</div>

<?php admin_footer() ?>
