<?php
require_once 'config.php';

// 检查登录状态
function checkAdminAuth() {
    if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
        header('Location: admin.php');
        exit;
    }
}

checkAdminAuth();

$conn = getDBConnection();

// 处理批量删除
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['text_ids'])) {
    $text_ids = array_map('intval', $_POST['text_ids']);
    $ids_string = implode(',', $text_ids);
    
    $query = "DELETE FROM texts WHERE id IN ($ids_string)";
    if (mysqli_query($conn, $query)) {
        header('Location: dashboard.php?message=' . urlencode("成功删除 " . count($text_ids) . " 个文本"));
        exit;
    }
}

// 处理单个删除
if (isset($_GET['id'])) {
    $id = intval($_GET['id']);
    $query = "DELETE FROM texts WHERE id = ?";
    $stmt = mysqli_prepare($conn, $query);
    mysqli_stmt_bind_param($stmt, "i", $id);
    
    if (mysqli_stmt_execute($stmt)) {
        header('Location: dashboard.php?message=' . urlencode("文本 #{$id} 已删除"));
        exit;
    }
}

// 默认重定向
header('Location: dashboard.php');
mysqli_close($conn);
exit;
?>