<?php
require_once 'config.php';

if (!isset($_GET['id'])) {
    header('Content-Type: application/json');
    echo json_encode(['error' => '缺少ID参数']);
    exit;
}

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

try {
    $stmt = $pdo->prepare("SELECT cd.*, cc.name AS category_name, cc.icon FROM custom_details cd LEFT JOIN custom_categories cc ON cd.category_id = cc.id WHERE cd.id = ?");
    $stmt->execute([$id]);
    $detail = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$detail) {
        header('Content-Type: application/json');
        echo json_encode(['error' => '未找到该详情']);
        exit;
    }

    header('Content-Type: application/json; charset=utf-8');
    echo json_encode([
        'title'    => $detail['title'],
        'content'  => $detail['content'],
        'tips'     => $detail['tips'] ?? '',
        'category' => $detail['category_name'] ?? '',
        'icon'     => $detail['icon'] ?? '',
    ]);
} catch (PDOException $e) {
    header('Content-Type: application/json');
    echo json_encode(['error' => '数据库查询失败']);
}