<?php
// admin_servers.php
require 'config/db.php'; // Eliminado el ../ porque está en la raíz

if (session_status() === PHP_SESSION_NONE) { session_start(); }

// Seguridad: Solo admin
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: index.php");
    exit;
}

$message = "";

// Lógica para añadir
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_server'])) {
    $stmt = $pdo->prepare("INSERT INTO game_servers (game_id, server_name) VALUES (?, ?)");
    $stmt->execute([(int)$_POST['game_id'], trim($_POST['server_name'])]);
    $message = "Server added successfully!";
}

// Lógica para borrar
if (isset($_GET['delete'])) {
    $stmt = $pdo->prepare("DELETE FROM game_servers WHERE id = ?");
    $stmt->execute([(int)$_GET['delete']]);
    header("Location: admin_servers.php?msg=deleted");
    exit;
}

if(isset($_GET['msg']) && $_GET['msg'] == 'deleted') $message = "Server removed.";

$games = $pdo->query("SELECT id, name FROM games ORDER BY name ASC")->fetchAll();

// Agrupamos los servidores por juego
$raw_servers = $pdo->query("SELECT s.*, g.name as game_name FROM game_servers s JOIN games g ON s.game_id = g.id ORDER BY g.name, s.server_name")->fetchAll();

$grouped_servers = [];
foreach ($raw_servers as $s) {
    $grouped_servers[$s['game_name']][] = $s;
}

include 'includes/header.php'; // Eliminado el ../
?>

<style>
    :root {
        --admin-bg: #0d1117;
        --admin-card: #161b22;
        --admin-accent: #00ff88; 
        --admin-border: #30363d;
    }
    body { background-color: var(--admin-bg); color: white; font-family: 'Inter', sans-serif; }
    
    .admin-container { max-width: 900px; margin: 40px auto; padding: 0 20px; }
    
    .page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; }
    .page-title { font-family: 'Syncopate', sans-serif; font-size: 20px; font-weight: 900; margin: 0; }
    .page-title span { color: var(--admin-accent); }

    .add-form { 
        background: var(--admin-card); 
        border: 1px solid var(--admin-border); 
        padding: 25px; 
        border-radius: 15px; 
        margin-bottom: 40px;
        display: flex;
        gap: 15px;
        align-items: center;
    }
    .add-form select, .add-form input {
        background: #0d1117;
        border: 1px solid var(--admin-border);
        color: white;
        padding: 12px;
        border-radius: 8px;
        flex: 1;
    }
    .btn-add {
        background: var(--admin-accent);
        color: black;
        border: none;
        padding: 12px 25px;
        border-radius: 8px;
        font-weight: 800;
        cursor: pointer;
        transition: 0.3s;
    }

    .game-group {
        background: var(--admin-card);
        border: 1px solid var(--admin-border);
        border-radius: 12px;
        margin-bottom: 10px;
        overflow: hidden;
    }
    .game-trigger {
        width: 100%;
        padding: 15px 20px;
        background: none;
        border: none;
        color: white;
        display: flex;
        justify-content: space-between;
        align-items: center;
        cursor: pointer;
        font-weight: 700;
    }
    .game-trigger i { color: var(--admin-accent); }

    .server-list {
        display: none; 
        padding: 0 20px 15px 20px;
        border-top: 1px solid var(--admin-border);
        background: rgba(0,0,0,0.2);
    }
    .server-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px 0;
        border-bottom: 1px solid rgba(255,255,255,0.05);
        font-size: 14px;
    }
    .btn-delete { color: #ff4d4d; text-decoration: none; font-size: 11px; font-weight: 800; }
</style>

<div class="admin-container">
    <div class="page-header">
        <h1 class="page-title">SERVER<span>.MANAGER</span></h1>
        <a href="admin_panel.php" style="color: var(--admin-accent); text-decoration: none; font-size: 12px; font-weight: 800;">← BACK TO PANEL</a>
    </div>

    <?php if($message): ?>
        <div style="background: rgba(0,255,136,0.1); border: 1px solid var(--admin-accent); color: var(--admin-accent); padding: 15px; border-radius: 10px; margin-bottom: 20px;">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="POST" class="add-form">
        <select name="game_id" required>
            <option value="" disabled selected>Select Game...</option>
            <?php foreach($games as $g): ?>
                <option value="<?php echo $g['id']; ?>"><?php echo htmlspecialchars($g['name']); ?></option>
            <?php endforeach; ?>
        </select>
        <input type="text" name="server_name" placeholder="New Server Name" required>
        <button type="submit" name="add_server" class="btn-add">ADD SERVER</button>
    </form>

    <div class="server-groups-container">
        <?php foreach($grouped_servers as $game_name => $server_list): ?>
            <div class="game-group">
                <button class="game-trigger" onclick="toggleAccordion('<?php echo md5($game_name); ?>')">
                    <span><i class="fa-solid fa-gamepad"></i> &nbsp; <?php echo htmlspecialchars($game_name); ?></span>
                    <i class="fa-solid fa-chevron-down"></i>
                </button>
                <div class="server-list" id="list-<?php echo md5($game_name); ?>">
                    <?php foreach($server_list as $server): ?>
                        <div class="server-item">
                            <span><?php echo htmlspecialchars($server['server_name']); ?></span>
                            <a href="?delete=<?php echo $server['id']; ?>" class="btn-delete" onclick="return confirm('Remove?')">Delete</a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<script>
function toggleAccordion(id) {
    const list = document.getElementById('list-' + id);
    list.style.display = (list.style.display === "block") ? "none" : "block";
}
</script>

<?php include 'includes/footer.php'; ?>