<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}
include '../includes/db.php';

$id = intval($_GET['id']);
$result = $conn->query("SELECT * FROM pages WHERE id=$id");
$page = $result->fetch_assoc();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $content = $_POST['content'];
    $stmt = $conn->prepare("UPDATE pages SET content=? WHERE id=?");
    $stmt->bind_param("si", $content, $id);
    if ($stmt->execute()) {
        header("Location: manage_pages.php");
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Content</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5" style="max-width: 800px;">
        <h3>Edit Content: <?php echo ucfirst(str_replace('_', ' ', $page['page_name'])); ?></h3>
        <form method="POST">
            <div class="mb-3">
                <label>Content</label>
                <textarea name="content" class="form-control" rows="10" required><?php echo htmlspecialchars($page['content']); ?></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Update Content</button>
            <a href="manage_pages.php" class="btn btn-secondary">Cancel</a>
        </form>
    </div>
</body>
</html>
