{% extends "layouts/admin.twig" %}

{% block title %}{{ ta.users|default('Gebruikers') }} - {{ ta.admin_title|default('CodePress Admin') }}{% endblock %}

{% block content %}
<h2 class="mb-4"><i class="bi bi-people"></i> {{ ta.users|default('Gebruikers') }}</h2>

<div class="row g-4">
    <div class="col-md-7">
        <div class="card shadow-sm">
            <div class="card-header">
                <i class="bi bi-list"></i> {{ ta.users|default('Gebruikers') }}
            </div>
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>{{ ta.username|default('Gebruikersnaam') }}</th>
                            <th>{{ ta.col_role|default('Rol') }}</th>
                            <th>{{ ta.col_created|default('Aangemaakt') }}</th>
                            <th>{{ ta.col_actions|default('Acties') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for username, data in users %}
                        <tr>
                            <td>
                                <i class="bi bi-person-circle"></i>
                                {{ username }}
                                {% if username == user.username %}
                                    <span class="badge bg-info">{{ ta.you|default('Jij') }}</span>
                                {% endif %}
                            </td>
                            <td>
                                <span class="badge bg-{{ data.role == 'admin' ? 'danger' : (data.role == 'content-manager' ? 'primary' : (data.role == 'bi-manager' ? 'success' : 'warning')) }}">
                                    {{ data.role_label|default(data.role) }}
                                </span>
                            </td>
                            <td class="text-muted">{{ data.created|default(ta.unknown|default('Onbekend')) }}</td>
                            <td>
                                {% if username != user.username %}
                                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#roleModal-{{ username }}">
                                    <i class="bi bi-person-gear"></i> {{ ta.col_role|default('Rol') }}
                                </button>
                                <form method="POST" action="/admin/users" class="d-inline" onsubmit="return confirm('{{ ta.confirm_delete_user|default('Weet je zeker dat je deze gebruiker wilt verwijderen?') }}')">
                                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                    <input type="hidden" name="action" value="delete">
                                    <input type="hidden" name="delete_username" value="{{ username }}">
                                    <button type="submit" class="btn btn-sm btn-outline-danger">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>

                                <!-- Role change modal -->
                                <div class="modal fade" id="roleModal-{{ username }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <form method="POST" action="/admin/users">
                                                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                                <input type="hidden" name="action" value="change_role">
                                                <input type="hidden" name="role_username" value="{{ username }}">
                                                <div class="modal-header">
                                                    <h5 class="modal-title"><i class="bi bi-person-gear"></i> {{ ta.change_role|default('Rol wijzigen: ') }}{{ username }}</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="mb-3">
                                                        <label class="form-label">{{ ta.current_role|default('Huidige rol') }}</label>
                                                        <p><span class="badge bg-secondary">{{ data.role_label|default(data.role) }}</span></p>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label for="new_role-{{ username }}" class="form-label">{{ ta.new_role|default('Nieuwe rol') }}</label>
                                                        <select class="form-select" id="new_role-{{ username }}" name="new_role">
                                                            {% for roleKey, roleLabel in roles %}
                                                            <option value="{{ roleKey }}" {{ data.role == roleKey ? 'selected' : '' }}>{{ roleLabel }}</option>
                                                            {% endfor %}
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ ta.cancel|default('Annuleren') }}</button>
                                                    <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg"></i> {{ ta.change|default('Wijzigen') }}</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                                {% else %}
                                <button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#profileModal">
                                    <i class="bi bi-pencil"></i> {{ ta.edit_profile|default('Profiel bewerken') }}
                                </button>
                                {% endif %}
                            </td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="4" class="text-muted text-center py-4">{{ ta.no_users|default('Geen gebruikers gevonden.') }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <div class="col-md-5">
        <!-- Profile edit card for current user -->
        <div class="card shadow-sm mb-4">
            <div class="card-header">
                <i class="bi bi-person-circle"></i> {{ ta.my_profile|default('Mijn profiel') }}
            </div>
            <div class="card-body">
                <form method="POST" action="/admin/users">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                    <input type="hidden" name="action" value="profile">
                    <input type="hidden" name="profile_username" value="{{ user.username }}">
                    <div class="mb-3">
                        <label class="form-label">{{ ta.username|default('Gebruikersnaam') }}</label>
                        <input type="text" class="form-control" value="{{ user.username }}" disabled>
                    </div>
                    <div class="mb-3">
                        <label for="profile_email" class="form-label">{{ ta.login_email|default('Login e-mail') }}</label>
                        <input type="email" class="form-control" id="profile_email" name="profile_email" value="{{ user.email|default('') }}">
                    </div>
                    <div class="mb-3">
                        <label for="profile_author_name" class="form-label">{{ ta.author_name|default('Auteur naam') }}</label>
                        <input type="text" class="form-control" id="profile_author_name" name="profile_author_name" value="{{ user.author_name|default('') }}">
                        <small class="form-text text-muted">{{ ta.author_name_help|default('Getoond als auteur op de website.') }}</small>
                    </div>
                    <div class="mb-3">
                        <label for="profile_author_email" class="form-label">{{ ta.author_email|default('Auteur e-mail') }}</label>
                        <input type="email" class="form-control" id="profile_author_email" name="profile_author_email" value="{{ user.author_email|default('') }}">
                    </div>
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-check-lg"></i> {{ ta.save|default('Opslaan') }}
                    </button>
                </form>
            </div>
        </div>

        <!-- New user card -->
        <div class="card shadow-sm">
            <div class="card-header">
                <i class="bi bi-plus-circle"></i> {{ ta.new_user|default('Nieuwe gebruiker') }}
            </div>
            <div class="card-body">
                <form method="POST" action="/admin/users">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                    <input type="hidden" name="action" value="add">
                    <div class="mb-3">
                        <label for="new_username" class="form-label">{{ ta.username|default('Gebruikersnaam') }}</label>
                        <input type="text" class="form-control" id="new_username" name="new_username" required autofocus>
                    </div>
                    <div class="mb-3">
                        <label for="new_password" class="form-label">{{ ta.password|default('Wachtwoord') }}</label>
                        <input type="password" class="form-control" id="new_password" name="new_password" required>
                        <small class="form-text text-muted">{{ ta.password_help|default('Minimaal 8 tekens.') }}</small>
                    </div>
                    <div class="mb-3">
                        <label for="new_email" class="form-label">{{ ta.login_email|default('Login e-mail') }}</label>
                        <input type="email" class="form-control" id="new_email" name="new_email">
                    </div>
                    <div class="mb-3">
                        <label for="new_author_name" class="form-label">{{ ta.author_name|default('Auteur naam') }}</label>
                        <input type="text" class="form-control" id="new_author_name" name="new_author_name">
                    </div>
                    <div class="mb-3">
                        <label for="new_author_email" class="form-label">{{ ta.author_email|default('Auteur e-mail') }}</label>
                        <input type="email" class="form-control" id="new_author_email" name="new_author_email">
                    </div>
                    <div class="mb-3">
                        <label for="new_role" class="form-label">{{ ta.col_role|default('Rol') }}</label>
                        <select class="form-select" id="new_role" name="new_role">
                            {% for roleKey, roleLabel in roles %}
                            <option value="{{ roleKey }}" {{ roleKey == 'content-manager' ? 'selected' : '' }}>{{ roleLabel }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-check-lg"></i> {{ ta.add_user|default('Gebruiker toevoegen') }}
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}