{% extends "base.html" %}

{% block title %}User Management - {{ app_name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    {% from "_components.html" import page_header %}
    <div class="row">
        <div class="col-12">
            {% set actions %}
            <a href="{{ url_for('admin.create_user') }}" class="btn btn-outline-light btn-sm">
                <i class="fas fa-user-plus"></i> New User
            </a>
            {% endset %}
            {{ page_header('fas fa-users', 'User Management', 'Manage users • ' ~ (users|length) ~ ' total', actions) }}
        </div>
    </div>

    <!-- User Statistics -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body">
                    <i class="fas fa-users fa-2x text-primary mb-2"></i>
                    <h4 class="text-primary">{{ stats.total_users }}</h4>
                    <p class="text-muted mb-0">Total Users</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body">
                    <i class="fas fa-user-check fa-2x text-success mb-2"></i>
                    <h4 class="text-success">{{ stats.active_users }}</h4>
                    <p class="text-muted mb-0">Active Users</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body">
                    <i class="fas fa-user-shield fa-2x text-warning mb-2"></i>
                    <h4 class="text-warning">{{ stats.admin_users }}</h4>
                    <p class="text-muted mb-0">Admin Users</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body">
                    <i class="fas fa-clock fa-2x text-info mb-2"></i>
                    <h4 class="text-info">{{ "%.1f"|format(stats.total_hours) }}h</h4>
                    <p class="text-muted mb-0">Total Hours</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Users List -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-white py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <h6 class="m-0 font-weight-bold text-primary">
                            <i class="fas fa-list me-2"></i>All Users
                        </h6>
                        <div class="d-flex gap-2">
                            <div class="input-group input-group-sm" style="width: 280px;">
                                <span class="input-group-text bg-light border-end-0">
                                    <i class="fas fa-search text-muted"></i>
                                </span>
                                <input type="text" class="form-control border-start-0" id="searchInput" placeholder="Search users...">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="card-body p-0">
                    {% if users %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0" id="usersTable">
                            <thead>
                                <tr>
                                    <th>User</th>
                                    <th>Role</th>
                                    <th>Status</th>
                                    <th>Created</th>
                                    <th>Last Login</th>
                                    <th>Total Hours</th>
                                    <th class="text-center">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for user in users %}
                                <tr>
                                    <td>
                                        <div>
                                            <strong>{{ user.display_name }}</strong>
                                            {% if user.active_timer %}
                                            <br><small class="text-warning">
                                                <i class="fas fa-clock"></i> Timer Running
                                            </small>
                                            {% endif %}
                                        </div>
                                    </td>
                                    <td>
                                        {% if user.role == 'admin' %}
                                        <span class="badge bg-warning">Admin</span>
                                        {% else %}
                                        <span class="badge bg-secondary">User</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if user.is_active %}
                                        <span class="badge bg-success">Active</span>
                                        {% else %}
                                        <span class="badge bg-danger">Inactive</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ user.created_at.strftime('%Y-%m-%d') }}</td>
                                    <td>
                                        {% if user.last_login %}
                                        {{ user.last_login.strftime('%Y-%m-%d %H:%M') }}
                                        {% else %}
                                        <span class="text-muted">Never</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <strong>{{ "%.1f"|format(user.total_hours) }}h</strong>
                                    </td>
                                    <td class="text-center actions-cell">
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{{ url_for('admin.edit_user', user_id=user.id) }}" 
                                               class="btn btn-sm btn-action btn-action--edit" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            {% if user.id != current_user.id %}
                                            <button type="button" class="btn btn-sm btn-action btn-action--danger" title="Delete" 
                                                    onclick="showDeleteUserModal('{{ user.id }}', '{{ user.username }}')">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <div class="empty-state">
                            <i class="fas fa-users fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">No users found</h5>
                            <p class="text-muted mb-4">Create your first user to get started with administration.</p>
                            <a href="{{ url_for('admin.create_user') }}" class="btn btn-primary">
                                <i class="fas fa-user-plus me-2"></i> Create First User
                            </a>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete User Modal -->
<div class="modal fade" id="deleteUserModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fas fa-trash me-2 text-danger"></i>Delete User
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    <strong>Warning:</strong> This action cannot be undone.
                </div>
                <p>Are you sure you want to delete the user <strong id="deleteUserName"></strong>?</p>
                <p class="text-muted mb-0">This will permanently remove the user and all their data cannot be recovered.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="fas fa-times me-1"></i>Cancel
                </button>
                <form method="POST" id="deleteUserForm" class="d-inline">
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-trash me-2"></i>Delete User
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
// Function to show delete user modal
function showDeleteUserModal(userId, username) {
    document.getElementById('deleteUserName').textContent = username;
    document.getElementById('deleteUserForm').action = "{{ url_for('admin.delete_user', user_id=0) }}".replace('0', userId);
    new bootstrap.Modal(document.getElementById('deleteUserModal')).show();
}

// Add loading state to delete user form and wire table search
document.addEventListener('DOMContentLoaded', function() {
    const deleteUserForm = document.getElementById('deleteUserForm');
    if (deleteUserForm) {
        deleteUserForm.addEventListener('submit', function() {
            const submitBtn = this.querySelector('button[type="submit"]');
            if (submitBtn) {
                submitBtn.innerHTML = '<div class="loading-spinner me-2"></div>Deleting...';
                submitBtn.disabled = true;
            }
        });
    }

    const searchInput = document.getElementById('searchInput');
    const table = document.getElementById('usersTable');
    if (searchInput && table) {
        const tbody = table.querySelector('tbody');
        searchInput.addEventListener('keyup', function() {
            const query = this.value.toLowerCase();
            Array.from(tbody.querySelectorAll('tr')).forEach(function(row) {
                const text = row.textContent.toLowerCase();
                row.style.display = text.includes(query) ? '' : 'none';
            });
        });
    }
});
</script>
{% endblock %}
