<?php
ob_start();
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/functions.php';

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

// 检查登录状态
if (!isLoggedIn()) {
    echo json_encode(['success' => false, 'message' => '请先登录']);
    exit;
}

// 检查权限
$user = currentUser();
if ($user['role'] !== 'admin' && $user['role'] !== 'employee') {
    echo json_encode(['success' => false, 'message' => '权限不足']);
    exit;
}

// 获取参数
$id = intval($_POST['id'] ?? 0);

if ($id <= 0) {
    echo json_encode(['success' => false, 'message' => '参数错误']);
    exit;
}

// 删除公告
$db->query("DELETE FROM announcements WHERE id = $id");
// 删除相关已读记录
$db->query("DELETE FROM announcement_reads WHERE announcement_id = $id");

echo json_encode(['success' => true, 'message' => '删除成功']);
exit;