<?php
// kategoriler.php
$admin_page = 'kategoriler';
$admin_title = 'Kategoriler';
include 'admin_header.php';

$action = $_GET['action'] ?? 'list';
$id = (int)($_GET['id'] ?? 0);
$msg = ''; $msg_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $sort_order = (int)($_POST['sort_order'] ?? 0);
    $slug = slugify($name);
    try {
        if ($_POST['edit_id'] ?? 0) {
            $eid = (int)$_POST['edit_id'];
            db()->prepare("UPDATE vs_categories SET name=?,slug=?,description=?,sort_order=? WHERE id=?")->execute([$name,$slug,$description,$sort_order,$eid]);
            $msg = 'Kategori güncellendi.'; $msg_type='success';
        } else {
            $check = db()->prepare("SELECT COUNT(*) FROM vs_categories WHERE slug=?");
            $check->execute([$slug]);
            if ($check->fetchColumn()) $slug .= '-'.time();
            db()->prepare("INSERT INTO vs_categories (name,slug,description,sort_order) VALUES (?,?,?,?)")->execute([$name,$slug,$description,$sort_order]);
            $msg = 'Kategori eklendi.'; $msg_type='success';
        }
        $action = 'list';
    } catch(Exception $e) { $msg = 'Hata: '.$e->getMessage(); $msg_type='error'; }
}

if ($action === 'delete' && $id) {
    try { db()->prepare("DELETE FROM vs_categories WHERE id=?")->execute([$id]); $msg='Silindi.'; $msg_type='success'; } catch(Exception $e) {}
    $action = 'list';
}

$edit_cat = null;
if ($action === 'edit' && $id) {
    $s = db()->prepare("SELECT * FROM vs_categories WHERE id=?"); $s->execute([$id]);
    $edit_cat = $s->fetch(PDO::FETCH_ASSOC);
    if (!$edit_cat) $action = 'list';
}

$cats = [];
if ($action === 'list') { try { $cats = db()->query("SELECT c.*,(SELECT COUNT(*) FROM vs_products WHERE category_id=c.id) as product_count FROM vs_categories c ORDER BY sort_order")->fetchAll(PDO::FETCH_ASSOC); } catch(Exception $e) {} }
?>
<?php if ($msg): ?><div class="alert alert-<?= $msg_type ?>"><?= clean($msg) ?></div><?php endif; ?>

<?php if ($action === 'list'): ?>
<div class="page-header">
    <div><h1>Kategoriler</h1></div>
    <a href="?action=add" class="btn btn-primary">+ Yeni Kategori</a>
</div>
<div class="admin-card">
    <table>
        <thead><tr><th>Ad</th><th>Slug</th><th>Ürün Sayısı</th><th>Sıra</th><th>İşlemler</th></tr></thead>
        <tbody>
        <?php foreach($cats as $c): ?>
        <tr>
            <td><strong><?= clean($c['name']) ?></strong><br><small style="color:var(--gray);"><?= clean(mb_substr($c['description']??'',0,60)) ?></small></td>
            <td style="color:var(--gray);font-size:12px;"><?= clean($c['slug']) ?></td>
            <td><?= $c['product_count'] ?></td>
            <td><?= $c['sort_order'] ?></td>
            <td><a href="?action=edit&id=<?= $c['id'] ?>" class="btn btn-outline btn-sm">Düzenle</a> <a href="?action=delete&id=<?= $c['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Sil?')">Sil</a></td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php else: ?>
<div class="page-header"><h1><?= $action==='edit'?'Düzenle':'Yeni Kategori' ?></h1><a href="?action=list" style="color:var(--gray);font-size:13px;">← Geri</a></div>
<div class="admin-card" style="padding:28px;max-width:600px;">
    <form method="POST">
        <?php if ($action==='edit'): ?><input type="hidden" name="edit_id" value="<?= $edit_cat['id'] ?>"><?php endif; ?>
        <div style="display:flex;flex-direction:column;gap:16px;">
            <div class="admin-form-group"><label>Kategori Adı *</label><input type="text" name="name" required value="<?= clean($edit_cat['name']??'') ?>"></div>
            <div class="admin-form-group"><label>Açıklama</label><textarea name="description"><?= clean($edit_cat['description']??'') ?></textarea></div>
            <div class="admin-form-group"><label>Sıralama</label><input type="number" name="sort_order" value="<?= (int)($edit_cat['sort_order']??0) ?>"></div>
            <button type="submit" class="btn btn-primary">Kaydet</button>
        </div>
    </form>
</div>
<?php endif; ?>
<?php include 'admin_footer.php'; ?>
