<?php
session_start();
require_once('./config.php');

// 验证用户是否登录
if (!isset($_SESSION['user_id'])) {
    die("未授权访问！");
}

// 获取当前用户ID
$currentUserId = $_SESSION['user_id'];

// 查询当前用户等级
$stmt = $conn->prepare("SELECT level FROM users WHERE id = ?");
$stmt->bind_param("i", $currentUserId);
$stmt->execute();
$result = $stmt->get_result();
$userData = $result->fetch_assoc();
$stmt->close();

// 检查是否为管理员（等级4）
if ($userData['level'] != 4) {
    die("权限不足！");
}

// 处理删除用户逻辑
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $userId = $_POST['user_id'];
    
    // 先删除关联的玩家数据
    $stmt = $conn->prepare("DELETE FROM players WHERE user_id = ?");
    $stmt->bind_param("i", $userId);
    $stmt->execute();
    $stmt->close();
    
    // 再删除用户
    $stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
    $stmt->bind_param("i", $userId);
    
    if ($stmt->execute()) {
        echo "用户删除成功！";
    } else {
        echo "删除失败：" . $stmt->error;
    }
    $stmt->close();
}

$conn->close();
?>