{% extends 'base.html' %}

{% block content %}
<div class="row my-4">
    <div class="col-12">
        <div class="users-header">
            <div>
                <h1 class="mb-1">Управление пользователями</h1>
                <p style="color: var(--text-muted); margin-bottom: 0;">CRUD-приложение · Лабораторная работа №4</p>
            </div>
            {% if current_user.is_authenticated %}
            <a href="{{ url_for('user_create') }}" class="btn btn-primary">
                <i class="fas fa-user-plus"></i> Создать пользователя
            </a>
            {% endif %}
        </div>

        <div class="data-card">
            {% if users %}
            <div class="table-responsive">
                <table class="data-table w-100">
                    <thead>
                        <tr>
                            <th style="width: 56px;">#</th>
                            <th>ФИО</th>
                            <th>Роль</th>
                            <th style="width: 220px;">Действия</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td>{{ loop.index }}</td>
                            <td>
                                <div class="d-flex align-items-center" style="gap: 0.75rem;">
                                    <div class="user-avatar-sm">
                                        {{ (user.first_name[0] if user.first_name else '?') | upper }}
                                    </div>
                                    <span style="color: var(--text-primary); font-weight: 500;">
                                        {{ user.get_full_name() or user.login }}
                                    </span>
                                </div>
                            </td>
                            <td>
                                {% if user.role %}
                                <span class="role-badge">{{ user.role.name }}</span>
                                {% else %}
                                <span style="color: var(--text-muted);">—</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="action-buttons">
                                    <a href="{{ url_for('user_view', user_id=user.id) }}"
                                       class="btn-action btn-view">
                                        <i class="fas fa-eye"></i> Просмотр
                                    </a>
                                    {% if current_user.is_authenticated %}
                                    <a href="{{ url_for('user_edit', user_id=user.id) }}"
                                       class="btn-action btn-edit"
                                       title="Редактировать">
                                        <i class="fas fa-pen"></i>
                                    </a>
                                    <button type="button"
                                            class="btn-action btn-delete"
                                            data-user-id="{{ user.id }}"
                                            data-user-name="{{ user.get_full_name() or user.login }}"
                                            title="Удалить">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                    {% endif %}
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="empty-state">
                <div class="empty-icon">
                    <i class="fas fa-users"></i>
                </div>
                <p>Пользователи не найдены</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>

{% if current_user.is_authenticated %}
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content custom-modal">
            <div class="modal-header custom-modal-header">
                <div class="modal-title-wrapper">
                    <div class="modal-icon">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                    <h5 class="modal-title" id="deleteModalLabel">Подтверждение удаления</h5>
                </div>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body custom-modal-body">
                <p id="deleteMessage" style="color: var(--text-secondary); margin: 0;"></p>
            </div>
            <div class="modal-footer custom-modal-footer">
                <button type="button" class="btn btn-outline" data-bs-dismiss="modal">
                    <i class="fas fa-times"></i> Нет
                </button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    <button type="submit" class="btn-danger-custom">
                        <i class="fas fa-trash"></i> Да, удалить
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    var deleteButtons = document.querySelectorAll('.btn-delete');
    var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));

    deleteButtons.forEach(function (btn) {
        btn.addEventListener('click', function () {
            var userId = this.dataset.userId;
            var userName = this.dataset.userName;
            document.getElementById('deleteMessage').textContent =
                'Вы уверены, что хотите удалить пользователя ' + userName + '?';
            document.getElementById('deleteForm').action = '/users/' + userId + '/delete';
            deleteModal.show();
        });
    });
});
</script>
{% endif %}
{% endblock %}
