<?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") {
    $playerId = $_POST['player_id'];
    $playerName = $_POST['player_name'];
    $description = $_POST['description'];
    
    $stmt = $conn->prepare("UPDATE players SET player_name=?, description=? WHERE id=?");
    $stmt->bind_param("ssi", $playerName, $description, $playerId);
    
    if ($stmt->execute()) {
        echo "玩家信息更新成功！";
    } else {
        echo "更新失败：" . $stmt->error;
    }
    $stmt->close();
}

$conn->close();
?>