<?php
session_start();
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 1) {
    header("Location: ../../index.php");
    exit();
}

include('../../includes/config.php');

if (!isset($_GET['id'])) {
    header("Location: categories.php?msg=Invalid+Category+ID");
    exit();
}

$category_id = (int)$_GET['id'];

// Fetch category
$stmt = $conn->prepare("SELECT * FROM categories WHERE id = ?");
$stmt->bind_param("i", $category_id);
$stmt->execute();
$result = $stmt->get_result();
$category = $result->fetch_assoc();
$stmt->close();

if (!$category) {
    header("Location: categories.php?msg=Category+not+found");
    exit();
}

// Handle POST update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['category_name']);
    $desc = trim($_POST['description']);

    if ($name !== '') {
        $update = $conn->prepare("UPDATE categories SET category_name = ?, description = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?");
        $update->bind_param("ssi", $name, $desc, $category_id);
        $update->execute();
        $update->close();
        header("Location: categories.php?msg=Category+updated");
        exit();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Category - Digital POS</title>
    <link rel="stylesheet" href="../../css/dashboard.css">
    <style>
        body, html {
            margin: 0; padding: 0;
            font-family: 'Segoe UI', sans-serif;
            background: url('../../images/bg.jpg') no-repeat center center fixed;
            background-size: cover;
            color: white;
        }

        .overlay {
            position: fixed;
            top: 0; left: 0;
            width: 100%; height: 100%;
            background: rgba(0, 0, 0, 0.6);
            z-index: -1;
        }

        .content {
            max-width: 600px;
            margin: 100px auto;
            padding: 30px;
            background: rgba(255, 255, 255, 0.08);
            border-radius: 20px;
            backdrop-filter: blur(15px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
        }

        h2 { text-align: center; margin-bottom: 25px; }

        form input, form textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 10px;
            border: none;
            background: rgba(255, 255, 255, 0.15);
            color: white;
        }

        form input::placeholder, textarea::placeholder {
            color: rgba(255, 255, 255, 0.6);
        }

        button {
            padding: 12px 20px;
            background: #ff6f00;
            color: white;
            border: none;
            border-radius: 10px;
            font-weight: bold;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="overlay"></div>
<div class="content">
    <h2>Edit Category</h2>
    <form method="POST">
        <input type="text" name="category_name" required value="<?= htmlspecialchars($category['category_name']) ?>" placeholder="Category name">
        <textarea name="description" rows="3" placeholder="Description"><?= htmlspecialchars($category['description']) ?></textarea>
        <button type="submit">Update Category</button>
    </form>
</div>
</body>
</html>
