<?php
session_start();
require '../admin/config.php'; // 确保此文件中有数据库连接
// 检查是否登录
if (!isset($_SESSION['user_id']) || !isset($_SESSION['username'])) {
    header("Location: /auth"); // 未登录时跳转到登录页面
    exit();
}

// 获取当前用户信息
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];

// 检查Token是否匹配
if (!isset($_GET['token']) || $_GET['token'] != $_SESSION['player_token']) {
    header("Location: ../index.php"); // Token不匹配时跳转回玩家列表页面
    exit();
}

// 获取传递的玩家名字
$player_name = $_GET['player_name'];

// 验证玩家名字是否有效
if (empty($player_name)) {
    header("Location: ../index.php"); // 玩家名字为空时跳转回玩家列表页面
    exit();
}

// 验证玩家是否属于当前用户，并获取服务器ID
$stmt = $conn->prepare("SELECT player_name, description, created_at, server_id FROM players WHERE user_id = ? AND player_name = ?");
$stmt->bind_param("is", $user_id, $player_name);
$stmt->execute();
$result = $stmt->get_result();
$player = $result->fetch_assoc();

if (!$player) {
    header("Location: ../index.php"); // 玩家不存在或不属于当前用户时跳转回玩家列表页面
    exit();
}

// 获取服务器信息
$server_id = $player['server_id'];
$stmt = $conn->prepare("SELECT ip_address, port FROM mc_servers WHERE id = ?");
$stmt->bind_param("i", $server_id);
$stmt->execute();
$result = $stmt->get_result();
$server = $result->fetch_assoc();


// 检查 easyauth 数据库连接是否成功
if ($easyauthConn->connect_error) {
    die("连接到 easyauth 数据库失败: " . $easyauthConn->connect_error);
}

// 在 easyauth 表中查找相同玩家名的 uuid
$easyauth_stmt = $easyauthConn->prepare("SELECT uuid FROM easyauth WHERE username = ?");
$easyauth_stmt->bind_param("s", $player_name);
$easyauth_stmt->execute();
$easyauth_result = $easyauth_stmt->get_result();
$easyauth_player = $easyauth_result->fetch_assoc();

// 处理注销请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['logout'])) {
    // 连接主数据库
//    $conn = new mysqli('localhost', 'awa', 'yzculture', 'ccs_auth');
    if ($conn->connect_error) {
        die("连接到主数据库失败: " . $conn->connect_error);
    }

    // 连接 easyauth 数据库
//    $easyauth_conn = new mysqli('localhost', 'ccs', 'ccsdatabase', 'easyauth');
    if ($easyauthConn->connect_error) {
        die("连接到 easyauth 数据库失败: " . $easyauthConn->connect_error);
    }

    // 启用事务
    $conn->autocommit(false);
    $easyauthConn->autocommit(false);

    try {
        // 删除主数据库中的玩家记录
        $stmt = $conn->prepare("DELETE FROM players WHERE user_id = ? AND player_name = ?");
        $stmt->bind_param("is", $user_id, $player_name);
        $stmt->execute();
        $stmt->close();

        // 删除 easyauth 数据库中的玩家记录
        $easyauth_stmt = $easyauthConn->prepare("DELETE FROM easyauth WHERE username = ?");
        $easyauth_stmt->bind_param("s", $player_name);
        $easyauth_stmt->execute();
        $easyauth_stmt->close();

        // 提交事务
        $conn->commit();
        $easyauthConn->commit();

        // 关闭数据库连接
        $conn->close();
        $easyauthConn->close();

        // 销毁会话并跳转到登录页面
        session_unset();
        session_destroy();
        header("Location: /auth");
        exit();
    } catch (Exception $e) {
        // 回滚事务
        $conn->rollback();
        $easyauthConn->rollback();
        $conn->close();
        $easyauthConn->close();
        die("注销失败: " . $e->getMessage());
    }
}
// 关闭数据库连接
$conn->close();
$easyauthConn->close();

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>玩家详情 - 圆周云境</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        .player-info-container {
            max-width: 600px;
            margin: 20px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .player-info {
            margin-bottom: 20px;
        }

        .player-info p {
            margin-bottom: 10px;
            font-size: 16px;
            line-height: 1.5;
        }

        .server-info {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #e0e0e0;
        }

        .server-info p {
            margin-bottom: 5px;
            font-size: 14px;
        }

        .back-button {
            margin-top: 20px;
            text-align: center;
        }

        .menu-item {
            display: inline-block;
            padding: 8px 15px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            transition: background-color 0.3s;
        }

        .menu-item:hover {
            background-color: #0056b3;
        }

        .top-bar {
            background-color: #333;
            color: white;
            padding: 10px 0;
            position: relative;
        }

        .top-bar-content {
            text-align: center;
            width: 100%;
        }

        .blue-bar {
            height: 3px;
            background-color: #007bff;
            width: 100%;
        }

        .combined-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
        }

        .logout-container {
            margin-top: 20px;
            text-align: center;
        }

        .logout-button {
            display: inline-block;
            padding: 8px 15px;
            background-color: #ff4d4d;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            border: 2px solid #ff4d4d;
            transition: background-color 0.3s;
            cursor: pointer;
        }

        .logout-button:hover {
            background-color: #ff0000;
            border-color: #ff0000;
        }
    </style>
</head>
<body>
<div class="combined-container">
    <div class="header">
        <h1>玩家详情</h1>
    </div>
    
    <div class="player-info-container">
        <div class="player-info">
            <p>玩家名: <?php echo htmlspecialchars($player['player_name']); ?></p>
            <p>称号: <?php echo htmlspecialchars($player['description']); ?></p>
            <p>UUID: <?php echo htmlspecialchars($easyauth_player['uuid'] ?? '玩家未登录，未找到UUID'); ?></p>
        </div>
        
        <div class="server-info">
            <h3>服务器连接信息</h3>
            <p>服务器IP: <?php echo htmlspecialchars($server['ip_address'] . ":" . $server['port']); ?></p>
        </div>
        
        <div class="back-button">
            <a href="../index.php" class="menu-item">返回玩家列表</a>
        </div>
        
        <div class="logout-container">
            <form action="" method="post">
                <button type="submit" name="logout" value="1" class="logout-button" onclick="return confirm('您确定要注销账号吗？此操作不可逆！')">注销账号</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>