{% set lang = request.cookies.get('panel_lang', 'es') %}
<!DOCTYPE html>
<html lang="{{ lang }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ "Administradores - Bots de Traducción" | translate(lang) }}</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/dashboard">
                <i class="bi bi-translate"></i> {{ "Bots de Traducción" | translate(lang) }}
            </a>
            <div class="d-flex align-items-center">
                <div class="dropdown me-3">
                    <button class="btn btn-outline-light btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
                        <i class="bi bi-translate"></i>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item {{ 'active' if lang == 'es' }}" href="/set-lang/es">Español</a></li>
                        <li><a class="dropdown-item {{ 'active' if lang == 'en' }}" href="/set-lang/en">English</a></li>
                        <li><a class="dropdown-item {{ 'active' if lang == 'pt' }}" href="/set-lang/pt">Português</a></li>
                    </ul>
                </div>
                <a href="/dashboard" class="btn btn-outline-light btn-sm me-2">{{ "Dashboard" | translate(lang) }}</a>
                <a href="/logout" class="btn btn-outline-light btn-sm">{{ "Cerrar Sesión" | translate(lang) }}</a>
            </div>
        </div>
    </nav>

    <div class="container mt-5">
        <h2 class="mb-4"><i class="bi bi-people"></i> {{ "Gestión de Administradores" | translate(lang) }}</h2>
        
        <div class="row">
            <div class="col-md-4">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">{{ "Añadir Administrador" | translate(lang) }}</h5>
                    </div>
                    <div class="card-body">
                        <form method="post" action="/admins/add">
                            <div class="mb-3">
                                <label class="form-label">{{ "Usuario" | translate(lang) }}</label>
                                <input type="text" name="username" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">{{ "Contraseña" | translate(lang) }}</label>
                                <input type="password" name="password" class="form-control" required>
                            </div>
                            <button type="submit" class="btn btn-primary w-100">{{ "Añadir" | translate(lang) }}</button>
                        </form>
                    </div>
                </div>
            </div>
            
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">{{ "Lista de Administradores" | translate(lang) }}</h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>{{ "ID" | translate(lang) }}</th>
                                    <th>{{ "Usuario" | translate(lang) }}</th>
                                    <th>{{ "Fecha Creación" | translate(lang) }}</th>
                                    <th>{{ "Acciones" | translate(lang) }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for admin in admins %}
                                <tr>
                                    <td>{{ admin.id }}</td>
                                    <td>{{ admin.username }}</td>
                                    <td>{{ admin.created_at }}</td>
                                    <td>
                                        <button type="button" class="btn btn-warning btn-sm" 
                                                data-bs-toggle="modal" 
                                                data-bs-target="#updateModal" 
                                                data-admin-id="{{ admin.id }}"
                                                data-admin-user="{{ admin.username }}">
                                            <i class="bi bi-pencil"></i>
                                        </button>
                                        <form method="post" action="/admins/delete" style="display: inline;">
                                            <input type="hidden" name="admin_id" value="{{ admin.id }}">
                                            <button type="submit" class="btn btn-danger btn-sm" 
                                                    onclick="return confirm('{{ "¿Estás seguro?" | translate(lang) }}')">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal para actualización -->
    <div class="modal fade" id="updateModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="post" action="/admins/update">
                    <div class="modal-header">
                        <h5 class="modal-title">{{ "Actualizar Contraseña" | translate(lang) }}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <p>{{ "Usuario:" | translate(lang) }} <strong id="modalUser"></strong></p>
                        <input type="hidden" name="admin_id" id="modalAdminId">
                        <div class="mb-3">
                            <label class="form-label">{{ "Nueva Contraseña" | translate(lang) }}</label>
                            <input type="password" name="new_password" class="form-control" required minlength="6">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ "Cerrar" | translate(lang) }}</button>
                        <button type="submit" class="btn btn-primary">{{ "Guardar Cambios" | translate(lang) }}</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        const updateModal = document.getElementById('updateModal');
        updateModal.addEventListener('show.bs.modal', function (event) {
            const button = event.relatedTarget;
            const adminId = button.getAttribute('data-admin-id');
            const adminUser = button.getAttribute('data-admin-user');
            
            document.getElementById('modalAdminId').value = adminId;
            document.getElementById('modalUser').textContent = adminUser;
        });
    </script>
</body>
</html>
