<?php
// ajax_get_post.php - Get post data for editing
session_start();
require_once 'config.php';

header('Content-Type: application/json');

if (!isset($_SESSION['admin'])) {
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

$id = (int)$_GET['id'];
$result = $conn->query("SELECT * FROM blog_posts WHERE id = $id");

if ($result && $result->num_rows > 0) {
    $post = $result->fetch_assoc();
    // Decode HTML entities for content
    $post['content'] = htmlspecialchars_decode($post['content']);
    echo json_encode($post);
} else {
    echo json_encode(['error' => 'Post not found']);
}
?>