<?php
require_once dirname(dirname(dirname(__FILE__))) . '/includes/init.php';

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

// 检查是否登录
if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
    http_response_code(401);
    echo json_encode(['error' => '未登录']);
    exit;
}

// 获取分类ID
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($id <= 0) {
    http_response_code(400);
    echo json_encode(['error' => 'invalid_id']);
    exit;
}

// 检查该分类下是否有链接
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM links WHERE category_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

// 返回JSON格式的响应
echo json_encode(['count' => (int)$row['count']]);

$stmt->close();
$conn->close();
