<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$basePath = '../../';
include '../../includes/header.php';

$db = (new Database())->connect();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    die('Invalid category ID.');
}

/*
    Load category
*/
$stmt = $db->prepare("
    SELECT *
    FROM categories
    WHERE id = ?
");

$stmt->execute([$id]);

$category = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$category) {
    die('Category not found.');
}
?>

<div class="container mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Edit Category</h3>

        <a href="index.php" class="btn btn-secondary">
            Back to Categories
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">

            <form action="update.php" method="POST">

                <input
                    type="hidden"
                    name="id"
                    value="<?= $category['id'] ?>"
                >

                <div class="mb-3">
                    <label class="form-label">Category Name</label>

                    <input
                        type="text"
                        name="name"
                        class="form-control"
                        value="<?= htmlspecialchars($category['name']) ?>"
                        required
                        autofocus
                    >
                </div>

                <button type="submit" class="btn btn-success">
                    Update Category
                </button>

                <a href="index.php" class="btn btn-secondary">
                    Cancel
                </a>

            </form>

        </div>
    </div>

</div>

<?php include '../../includes/footer.php'; ?>