<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}MCLogger{% endblock %} — Panel</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div class="d-flex" id="wrapper">
    <!-- ── Sidebar ─────────────────────────────────────────── -->
    <nav id="sidebar" class="d-flex flex-column p-3">
        <div class="sidebar-brand mb-3 text-center">
            <i class="bi bi-database-fill-gear fs-2 text-success"></i>
            <div class="fw-bold mt-1">MCLogger</div>
            {% if session.get('group_name') %}
            <span class="badge bg-success mt-1">{{ session.group_name }}</span>
            {% endif %}
        </div>

        {% set perms = session.get('permissions', {}) %}
        {% set is_admin = session.get('is_site_admin') or session.get('role') == 'admin' %}

        <ul class="nav flex-column gap-1">
            {% if perms.get('view_dashboard', True) or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.dashboard' }}" href="{{ url_for('panel.dashboard') }}">
                    <i class="bi bi-speedometer2"></i> <span>Dashboard</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_players', True) or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint in ['panel.players','panel.player_detail'] }}" href="{{ url_for('panel.players') }}">
                    <i class="bi bi-people-fill"></i> <span>Players</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_sessions', True) or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.sessions' }}" href="{{ url_for('panel.sessions') }}">
                    <i class="bi bi-clock-history"></i> <span>Sessions</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_chat', True) or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.chat' }}" href="{{ url_for('panel.chat') }}">
                    <i class="bi bi-chat-dots-fill"></i> <span>Chat</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_commands', True) or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.commands' }}" href="{{ url_for('panel.commands') }}">
                    <i class="bi bi-terminal-fill"></i> <span>Commands</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_deaths', True) or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.deaths' }}" href="{{ url_for('panel.deaths') }}">
                    <i class="bi bi-heartbreak-fill"></i> <span>Deaths</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_blocks', True) or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.blocks' }}" href="{{ url_for('panel.blocks') }}">
                    <i class="bi bi-bricks"></i> <span>Block Events</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_proxy') or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.proxy' }}" href="{{ url_for('panel.proxy') }}">
                    <i class="bi bi-diagram-3-fill"></i> <span>Proxy Events</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_server_events') or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.server_events' }}" href="{{ url_for('panel.server_events') }}">
                    <i class="bi bi-server"></i> <span>Server Events</span>
                </a>
            </li>
            {% endif %}
            {% if perms.get('view_perms') or is_admin %}
            <li class="nav-item">
                <a class="nav-link {{ 'active' if request.endpoint == 'panel.perms' }}" href="{{ url_for('panel.perms') }}">
                    <i class="bi bi-shield-lock-fill"></i> <span>Permissions</span>
                </a>
            </li>
            {% endif %}
        </ul>

        <hr class="my-2 sidebar-hide-collapsed">

        <!-- Group switcher -->
        {% if not session.get('is_site_admin') and user_groups and user_groups|length > 1 %}
        <div class="mb-2 sidebar-hide-collapsed">
            <small class="text-muted">Switch group:</small>
            {% for g in user_groups %}
            <form method="post" action="{{ url_for('auth.switch_group', group_id=g.id) }}" class="mt-1">
                <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                <button type="submit"
                        class="btn btn-sm w-100 {{ 'btn-success' if g.id == session.get('group_id') else 'btn-outline-secondary' }}">
                    {{ g.name }}
                </button>
            </form>
            {% endfor %}
        </div>
        {% endif %}

        <!-- Admin-Links -->
        {% if session.get('role') == 'admin' and not session.get('is_site_admin') %}
        <a href="{{ url_for('group_admin.dashboard') }}" class="btn btn-outline-warning btn-sm mb-1">
            <i class="bi bi-gear-fill"></i> <span>Manage Group</span>
        </a>
        {% endif %}
        {% if session.get('is_site_admin') %}
        {% if session.get('admin_viewing') %}
        <form method="post" action="{{ url_for('site_admin.stop_view') }}" class="mb-1">
            <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
            <button type="submit" class="btn btn-warning btn-sm w-100">
                <i class="bi bi-arrow-left"></i> <span>Back to Admin</span>
            </button>
        </form>
        {% else %}
        <a href="{{ url_for('site_admin.dashboard') }}" class="btn btn-outline-danger btn-sm mb-1">
            <i class="bi bi-shield-fill"></i> <span>Site Admin</span>
        </a>
        {% endif %}
        {% endif %}

        <div class="mt-auto">
            <div class="small text-muted mb-1">
                <i class="bi bi-circle-fill text-success me-1" style="font-size:.5rem"></i>
                <span id="online-count">—</span> <span class="sidebar-hide-collapsed">Online</span>
            </div>
            <small class="text-muted d-block mb-1 sidebar-hide-collapsed">{{ session.get('username', '') }}</small>
            <form method="post" action="{{ url_for('auth.logout') }}">
                <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                    <i class="bi bi-box-arrow-right"></i> <span>Logout</span>
                </button>
            </form>
        </div>
    </nav>

    <!-- ── Main content ───────────────────────────────────── -->
    <div id="page-content" class="flex-grow-1 overflow-auto">
        <div class="topbar d-flex align-items-center justify-content-between px-4 py-2">
            <button class="btn btn-sm btn-outline-secondary" id="sidebarToggle"><i class="bi bi-list"></i></button>
            <h6 class="mb-0 fw-semibold">{% block page_title %}{% endblock %}</h6>
            <small class="text-muted" id="client-clock">--:--:--</small>
        </div>

        {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
        <div class="px-4 pt-2">
            {% for cat, msg in messages %}
            <div class="alert alert-{{ cat }} alert-dismissible fade show" role="alert">
                {{ msg }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
            {% endfor %}
        </div>
        {% endif %}
        {% endwith %}

        <main class="px-4 py-3">{% block content %}{% endblock %}</main>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.4/dist/chart.umd.min.js"></script>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
{% block scripts %}{% endblock %}
</body>
</html>
