<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Игроки</title>
    <link rel="icon" href="/static/smith.ico" type="image/x-icon">
    <style>
        :root {
            --bg: #0f1115;
            --panel: #171a21;
            --panel-2: #1d222b;
            --border: #2b3240;
            --text: #e8ecf3;
            --muted: #9aa4b2;
            --accent: #4f8cff;
            --accent-hover: #3e78e6;
            --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
            --radius: 16px;
        }

        * { box-sizing: border-box; }

        body {
            margin: 0;
            background: var(--bg);
            color: var(--text);
            font-family: Arial, sans-serif;
        }

        .page {
            max-width: 1500px;
            margin: 0 auto;
            padding: 24px;
        }

        .panel {
            background: var(--panel);
            border: 1px solid var(--border);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 20px;
        }

        .topbar {
            display: flex;
            gap: 12px;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 18px;
            flex-wrap: wrap;
        }

        .title {
            font-size: 24px;
            font-weight: 700;
        }

        .search-form {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        input[type="text"] {
            min-width: 320px;
            min-height: 42px;
            padding: 0 12px;
            border-radius: 10px;
            border: 1px solid var(--border);
            background: var(--panel-2);
            color: var(--text);
        }

        .btn {
            min-height: 42px;
            padding: 0 14px;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-weight: 600;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
        }

        .btn-primary {
            background: var(--accent);
            color: white;
        }

        .btn-secondary {
            background: transparent;
            color: var(--text);
            border: 1px solid var(--border);
        }

        .table-wrap {
            width: 100%;
            overflow-x: auto;
            border-radius: 12px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: var(--panel-2);
            overflow: hidden;
        }

        th, td {
            padding: 10px 12px;
            border-bottom: 1px solid var(--border);
            text-align: left;
            font-size: 14px;
            white-space: nowrap;
        }

        th {
            color: var(--muted);
            font-weight: 600;
        }

        tr:last-child td {
            border-bottom: none;
        }

        .sort-link {
            color: var(--muted);
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .sort-link:hover {
            color: var(--text);
        }

        .sort-indicator {
            color: var(--accent);
            min-width: 12px;
            display: inline-block;
        }

        .id-col {
            width: 70px;
        }

        .photo-col {
            width: 80px;
            text-align: center;
        }

        .photo-check {
            width: 18px;
            height: 18px;
            accent-color: var(--accent);
            cursor: pointer;
        }

        .photo-toggle-form {
            margin: 0;
            display: inline-flex;
            align-items: center;
            justify-content: center;
        }

        .action-col {
            width: 140px;
            text-align: right;
        }

        .empty-box {
            padding: 18px;
            border-radius: 12px;
            background: var(--panel-2);
            color: var(--muted);
            border: 1px dashed var(--border);
        }
    </style>
</head>
<body>
    <div class="page">
        <div class="panel">
            <div class="topbar">
                <div class="title">Игроки</div>

                <form method="get" action="/admin/db/players" class="search-form">
                    <input type="hidden" name="sort" value="{{ sort }}">
                    <input type="hidden" name="direction" value="{{ direction }}">
                    <input type="text" name="q" value="{{ q }}" placeholder="Поиск по ФИО, external_id или команде">
                    <button type="submit" class="btn btn-primary">Найти</button>
                    <a href="/admin/db" class="btn btn-secondary">Назад</a>
                </form>
            </div>

            {% if players %}
            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th class="id-col"><a class="sort-link" href="{{ sort_links.id.url }}">ID <span class="sort-indicator">{{ sort_links.id.indicator }}</span></a></th>
                            <th><a class="sort-link" href="{{ sort_links.full_name.url }}">ФИО <span class="sort-indicator">{{ sort_links.full_name.indicator }}</span></a></th>
                            <th><a class="sort-link" href="{{ sort_links.first_name.url }}">Имя <span class="sort-indicator">{{ sort_links.first_name.indicator }}</span></a></th>
                            <th><a class="sort-link" href="{{ sort_links.last_name.url }}">Фамилия <span class="sort-indicator">{{ sort_links.last_name.indicator }}</span></a></th>
                            <th><a class="sort-link" href="{{ sort_links.external_id.url }}">External ID <span class="sort-indicator">{{ sort_links.external_id.indicator }}</span></a></th>
                            <th><a class="sort-link" href="{{ sort_links.position.url }}">Амплуа <span class="sort-indicator">{{ sort_links.position.indicator }}</span></a></th>
                            <th><a class="sort-link" href="{{ sort_links.team_name.url }}">Название команды <span class="sort-indicator">{{ sort_links.team_name.indicator }}</span></a></th>
                            <th class="photo-col"><a class="sort-link" href="{{ sort_links.photo.url }}">Фото <span class="sort-indicator">{{ sort_links.photo.indicator }}</span></a></th>
                            <th class="action-col"></th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for p in players %}
                        <tr>
                            <td>{{ p.id }}</td>
                            <td>{{ p.full_name }}</td>
                            <td>{{ p.first_name }}</td>
                            <td>{{ p.last_name }}</td>
                            <td>{{ p.external_id }}</td>
                            <td>{{ p.position }}</td>
                            <td>{{ p.team_name }}</td>
                            <td class="photo-col">
                                <form method="post" action="/admin/db/players/{{ p.id }}/photo-enabled" class="photo-toggle-form">
                                    <input type="hidden" name="q" value="{{ q }}">
                                    <input type="hidden" name="sort" value="{{ sort }}">
                                    <input type="hidden" name="direction" value="{{ direction }}">
                                    <input
                                        type="checkbox"
                                        name="photo_enabled"
                                        value="true"
                                        class="photo-check"
                                        title="Использовать фото игрока в JSON/vMix"
                                        onchange="this.form.submit()"
                                        {% if p.photo_enabled %}checked{% endif %}
                                    >
                                </form>
                            </td>
                            <td class="action-col">
                                <a href="/admin/db/players/{{ p.id }}/edit" class="btn btn-secondary">Редактировать</a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="empty-box">Игроки не найдены.</div>
            {% endif %}
        </div>
    </div>
</body>
</html>
