<?php include("auth.php");

$id = $_GET['id'];
$cat = $conn->query("SELECT * FROM categories WHERE id=$id")->fetch_assoc();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit Category</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-5">
    <div class="card shadow">
        <div class="card-header bg-dark text-light">
            Edit Category
        </div>

        <div class="card-body">
            <form method="post">
                <div class="mb-3">
                    <label>Category Name</label>
                    <input type="text" name="name" class="form-control"
                           value="<?= htmlspecialchars($cat['name']) ?>" required>
                </div>

                <button name="update" class="btn btn-warning">Update</button>
                <a href="categories.php" class="btn btn-secondary">Back</a>
            </form>
        </div>
    </div>
</div>

<?php
if (isset($_POST['update'])) {
    $stmt = $conn->prepare("UPDATE categories SET name=? WHERE id=?");
    $stmt->bind_param("si", $_POST['name'], $id);
    $stmt->execute();
    header("Location: categories.php");
}
?>
</body>
</html>
