<?php
require_once __DIR__ . '/config.php';
requireAdmin();
$pdo = getDB();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'add') {
        $name = trim($_POST['name'] ?? '');
        $slug = trim($_POST['slug'] ?? '') ?: preg_replace('/[^a-z0-9]+/', '-', strtolower($name));
        $pdo->prepare("INSERT INTO product_categories (name, slug, sort_order) VALUES (?, ?, ?)")->execute([$name, $slug, (int)($_POST['sort_order'] ?? 0)]);
    } elseif ($_POST['action'] === 'edit' && !empty($_POST['id'])) {
        $id = (int)$_POST['id'];
        $name = trim($_POST['name'] ?? '');
        $slug = trim($_POST['slug'] ?? '');
        $pdo->prepare("UPDATE product_categories SET name=?, slug=?, sort_order=? WHERE id=?")->execute([$name, $slug, (int)($_POST['sort_order'] ?? 0), $id]);
    } elseif ($_POST['action'] === 'delete' && !empty($_POST['id'])) {
        $pdo->prepare("DELETE FROM product_categories WHERE id = ?")->execute([(int)$_POST['id']]);
    }
    header('Location: categories.php');
    exit;
}
$categories = $pdo->query("SELECT * FROM product_categories ORDER BY sort_order, id")->fetchAll(PDO::FETCH_ASSOC);
$pageTitle = 'Categories';
require_once __DIR__ . '/includes/header.php';
?>
<div class="admin-page">
    <h1>Product Categories</h1>
    <form method="post" class="admin-form inline-form">
        <input type="hidden" name="action" value="add">
        <input type="text" name="name" placeholder="Category name" required>
        <input type="text" name="slug" placeholder="slug">
        <input type="number" name="sort_order" placeholder="Order" value="0">
        <button type="submit" class="btn">Add</button>
    </form>
    <table class="admin-table">
        <thead><tr><th>ID</th><th>Name</th><th>Slug</th><th>Order</th><th>Actions</th></tr></thead>
        <tbody>
            <?php foreach ($categories as $c): ?>
            <tr>
                <td><?php echo (int)$c['id']; ?></td>
                <td><?php echo htmlspecialchars($c['name']); ?></td>
                <td><?php echo htmlspecialchars($c['slug']); ?></td>
                <td><?php echo (int)$c['sort_order']; ?></td>
                <td>
                    <a href="#" onclick="document.getElementById('edit-<?php echo $c['id']; ?>').style.display='block';return false;">Edit</a>
                    <form method="post" style="display:inline;" onsubmit="return confirm('Delete?');">
                        <input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?php echo $c['id']; ?>">
                        <button type="submit" class="link-btn">Delete</button>
                    </form>
                </td>
            </tr>
            <tr id="edit-<?php echo $c['id']; ?>" style="display:none;">
                <td colspan="5">
                    <form method="post">
                        <input type="hidden" name="action" value="edit"><input type="hidden" name="id" value="<?php echo $c['id']; ?>">
                        <input type="text" name="name" value="<?php echo htmlspecialchars($c['name']); ?>">
                        <input type="text" name="slug" value="<?php echo htmlspecialchars($c['slug']); ?>">
                        <input type="number" name="sort_order" value="<?php echo (int)$c['sort_order']; ?>">
                        <button type="submit" class="btn">Update</button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
