<?php
session_start();

require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Category.php';

$database = new Database();
$conn = $database->getConnection();
$auth = new Auth($conn);
$error = '';

if (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit();
}

$auth->enforceSuperAdminPrivileges($_SESSION['user_id']);

$category_id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$category_id) {
    header('Location: category_list.php');
    exit();
}

$category = new Category($conn);
$categoryDetails = $category->getCategoryById($category_id);
if (!$categoryDetails) {
    header('Location: category_list.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $categoryname = trim($_POST['categoryname'] ?? '');
    $categorydescription = trim($_POST['categorydescription'] ?? '');

    if ($categoryname === '' || $categorydescription === '') {
        $error = 'Category name and description are required.';
    } elseif ($category->updateCategory($category_id, $categoryname, $categorydescription)) {
        header('Location: category_list.php');
        exit();
    } else {
        $error = 'Failed to update category.';
    }
}
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
<link rel="stylesheet" href="../../assets/css/simple.css">
    <style>
        .back-link {
            display: inline-block;
            margin-bottom: 15px;
            padding: 10px 15px;
            background-color: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .back-link:hover {
            background-color: #2980b9;
            color: white;
            text-decoration: none;
        }

        .form-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-top: 20px;
        }

        .cancel-link {
            display: inline-block;
            padding: 10px 20px;
            border-radius: 5px;
            color: #fff;
            text-decoration: none;
            background-color: #6c757d;
        }

        .cancel-link:hover {
            background-color: #5a6268;
            color: #fff;
            text-decoration: none;
        }

        .error-message {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            border-radius: 5px;
            color: #721c24;
            margin-bottom: 20px;
            padding: 15px;
        }
    </style>

    <a href="category_list.php" class="back-link">Back to Categories</a>

    <h2>Edit Category</h2>

    <?php if ($error !== ''): ?>
        <div class="error-message"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>

    <form method="post" action="">
        <div class="form-group">
            <label for="categoryname">Category Name:</label>
            <input type="text" id="categoryname" name="categoryname" value="<?php echo htmlspecialchars($categoryDetails['category_name'] ?? ''); ?>" required>
        </div>

        <div class="form-group">
            <label for="categorydescription">Description:</label>
            <input type="text" id="categorydescription" name="categorydescription" value="<?php echo htmlspecialchars($categoryDetails['category_description'] ?? ''); ?>" required>
        </div>

        <div class="form-actions">
            <button type="submit">Update Category</button>
            <a href="category_list.php" class="cancel-link">Cancel</a>
        </div>
    </form>
<?php include __DIR__ . '/../../includes/footer.php'; ?>
