<?php
require_once '../../config/config.php';

if (!isset($_POST['id'])) {
    echo 'missing id';
    exit;
}

$id = intval($_POST['id']);
$conn = db_connect();

// 检查是否有关联的链接
$check_links = $conn->query("SELECT COUNT(*) as count FROM links WHERE category_id = $id");
$result = $check_links->fetch_assoc();

if ($result['count'] > 0) {
    echo '该分类下还有链接，请先删除链接';
    exit;
}

// 删除分类
$stmt = $conn->prepare("DELETE FROM categories WHERE id = ?");
$stmt->bind_param("i", $id);

if ($stmt->execute()) {
    echo 'success';
} else {
    echo '删除失败';
}

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