{% extends "layouts/base.html" %}
{% load i18n %}

{% block title %}{% trans "Provisional Accounts" %}{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.section-card {
    background: white;
    border-radius: 1rem;
    border: 2px solid #e2e8f0;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
}
.section-card:hover {
    border-color: #005696;
    box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
}

.section-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e2e8f0;
    background: linear-gradient(to right, #f8fafc, #f1f5f9);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.section-icon {
    width: 40px;
    height: 40px;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>
{% endblock %}

{% block content %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="user-plus" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <a href="{% url 'accounts:onboarding-dashboard' %}" class="inline-flex items-center text-white/80 hover:text-white mb-1 font-medium transition">
                        <i data-lucide="arrow-left" class="w-4 h-4 mr-2"></i>
                        {% trans "Back to Dashboard" %}
                    </a>
                    <h1 class="text-2xl font-bold">
                        {% trans "Provisional Accounts" %}
                    </h1>
                    <p class="text-white/80">{% trans "View accounts pending activation" %}</p>
                </div>
            </div>
            <div class="flex gap-3">
                <button onclick="openCreateModal()" class="bg-white text-navy px-4 py-2 rounded-lg font-medium hover:bg-white/90 transition flex items-center gap-2">
                    <i data-lucide="plus" class="w-4 h-4"></i>
                    {% trans "Create New User" %}
                </button>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
        <div class="bg-white rounded-xl p-6 border-2 border-blue-100 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-blue-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="users" class="w-6 h-6 text-blue-600"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-navy">{{ total_count }}</div>
                    <div class="text-sm text-slate">{% trans "Total Provisional" %}</div>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-xl p-6 border-2 border-emerald-100 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-emerald-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="user-check" class="w-6 h-6 text-emerald-600"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-emerald-600">{{ completed_count }}</div>
                    <div class="text-sm text-slate">{% trans "Completed" %}</div>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-xl p-6 border-2 border-orange-100 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-orange-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="clock" class="w-6 h-6 text-orange-600"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-orange-600">{{ in_progress_count }}</div>
                    <div class="text-sm text-slate">{% trans "In Progress" %}</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Accounts List -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-gradient-to-br from-blue to-purple-500">
                <i data-lucide="users" class="w-5 h-5 text-white"></i>
            </div>
            <h3 class="font-bold text-navy text-lg">{% trans "Pending Accounts" %}</h3>
        </div>
        {% if provisional_accounts %}
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-blue-50">
                    <tr>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">
                            {% trans "User" %}
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">
                            {% trans "Email" %}
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">
                            {% trans "Role" %}
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">
                            {% trans "Invited" %}
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">
                            {% trans "Expires" %}
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">
                            {% trans "Status" %}
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">
                            {% trans "Actions" %}
                        </th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-50">
                    {% for account in provisional_accounts %}
                    <tr class="hover:bg-blue-50/50 transition">
                        <td class="px-6 py-4">
                            <div class="flex items-center gap-3">
                                <div class="flex items-center justify-center w-12 h-12 bg-gradient-to-br from-blue to-navy rounded-full">
                                    <span class="text-white font-bold text-sm">{{ account.first_name|first|default:"U" }}{{ account.last_name|first|default:"" }}</span>
                                </div>
                                <div>
                                    <div class="font-bold text-navy">{{ account.get_full_name|default:account.email }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <div class="text-slate">{{ account.email }}</div>
                        </td>
                        <td class="px-6 py-4">
                            {% if account.groups.first %}
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">
                                {{ account.groups.first.name }}
                            </span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            <span class="text-slate text-sm">{{ account.created_at|date:"M d, Y" }}</span>
                        </td>
                        <td class="px-6 py-4">
                            {% if account.invitation_expires_at %}
                                {% if account.invitation_expires_at < now %}
                                    <span class="text-red-600 font-bold text-sm">{% trans "Expired" %}</span>
                                {% else %}
                                    <span class="text-slate text-sm">{{ account.invitation_expires_at|date:"M d, Y" }}</span>
                                {% endif %}
                            {% else %}
                                <span class="text-slate">-</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            {% if account.is_active %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-emerald-100 text-emerald-700">
                                    <i data-lucide="check" class="w-3 h-3"></i>
                                    {% trans "Active" %}
                                </span>
                            {% elif account.invitation_expires_at and account.invitation_expires_at < now %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-red-100 text-red-700">
                                    <i data-lucide="alert-circle" class="w-3 h-3"></i>
                                    {% trans "Expired" %}
                                </span>
                            {% else %}
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-orange-100 text-orange-700">
                                    {% trans "Pending" %}
                                </span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            <div class="flex gap-2">
                                <a href="{% url 'accounts:provisional-user-progress' account.id %}" class="px-3 py-2 text-emerald-600 bg-emerald-50 hover:bg-emerald-100 rounded-lg transition font-medium text-sm" title="{% trans 'View Progress' %}">
                                    <i data-lucide="eye" class="w-4 h-4"></i>
                                </a>
                                <form method="post" action="{% url 'accounts:bulk-resend-invitations' %}" class="inline" onsubmit="return confirm('{% trans "Resend invitation to this user?" %}')">
                                    {% csrf_token %}
                                    <input type="hidden" name="user_ids" value="{{ account.id }}">
                                    <button type="submit" class="px-3 py-2 text-blue-600 bg-blue-50 hover:bg-blue-100 rounded-lg transition font-medium text-sm" title="{% trans 'Resend Invite' %}">
                                        <i data-lucide="mail" class="w-4 h-4"></i>
                                    </button>
                                </form>
                                <form method="post" action="{% url 'accounts:bulk-deactivate-users' %}" class="inline" onsubmit="return confirm('{% trans "Deactivate this user?" %}')">
                                    {% csrf_token %}
                                    <input type="hidden" name="user_ids" value="{{ account.id }}">
                                    <button type="submit" class="px-3 py-2 text-red-600 bg-red-50 hover:bg-red-100 rounded-lg transition font-medium text-sm" title="{% trans 'Deactivate' %}">
                                        <i data-lucide="user-x" class="w-4 h-4"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="p-12 text-center">
            <div class="inline-flex items-center justify-center w-20 h-20 bg-gradient-to-br from-emerald-100 to-green-100 rounded-full mb-4">
                <i data-lucide="check-circle" class="w-10 h-10 text-emerald-500"></i>
            </div>
            <h3 class="text-xl font-bold text-navy mb-2">{% trans "All Accounts Activated" %}</h3>
            <p class="text-slate">{% trans "There are no pending provisional accounts" %}</p>
        </div>
        {% endif %}
    </div>
</div>

<!-- Create User Modal -->
<div id="createUserModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center">
    <div class="bg-white rounded-2xl w-full max-w-2xl m-4 shadow-2xl">
        <div class="bg-gradient-to-r from-navy to-blue p-6 rounded-t-2xl">
            <div class="flex justify-between items-center">
                <h3 class="text-xl font-bold text-white flex items-center gap-2">
                    <i data-lucide="user-plus" class="w-6 h-6"></i>
                    {% trans "Create New User" %}
                </h3>
                <button onclick="closeCreateModal()" class="text-white/80 hover:text-white transition">
                    <i data-lucide="x" class="w-6 h-6"></i>
                </button>
            </div>
        </div>
        
        <form method="post" class="p-6 space-y-6">
            {% csrf_token %}
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        {% trans "Email" %} <span class="text-red-500">*</span>
                    </label>
                    <input type="email" name="email" required
                        class="w-full px-4 py-3 border-2 border-gray-200 rounded-xl focus:border-navy focus:ring-2 focus:ring-navy/20 transition"
                        placeholder="user@example.com">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        {% trans "Employee ID" %}
                    </label>
                    <input type="text" name="employee_id"
                        class="w-full px-4 py-3 border-2 border-gray-200 rounded-xl focus:border-navy focus:ring-2 focus:ring-navy/20 transition"
                        placeholder="EMP001">
                </div>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        {% trans "First Name" %} <span class="text-red-500">*</span>
                    </label>
                    <input type="text" name="first_name" required
                        class="w-full px-4 py-3 border-2 border-gray-200 rounded-xl focus:border-navy focus:ring-2 focus:ring-navy/20 transition"
                        placeholder="John">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        {% trans "Last Name" %} <span class="text-red-500">*</span>
                    </label>
                    <input type="text" name="last_name" required
                        class="w-full px-4 py-3 border-2 border-gray-200 rounded-xl focus:border-navy focus:ring-2 focus:ring-navy/20 transition"
                        placeholder="Doe">
                </div>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        {% trans "Hospital" %}
                    </label>
                    <select name="hospital" 
                        class="w-full px-4 py-3 border-2 border-gray-200 rounded-xl focus:border-navy focus:ring-2 focus:ring-navy/20 transition bg-white">
                        <option value="">{% trans "Select Hospital" %}</option>
                        {% for hospital in hospitals %}
                        <option value="{{ hospital.id }}">{{ hospital.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        {% trans "Department" %}
                    </label>
                    <select name="department" data-tomselect
                        class="w-full px-4 py-3 border-2 border-gray-200 rounded-xl focus:border-navy focus:ring-2 focus:ring-navy/20 transition bg-white">
                        <option value="">{% trans "Select Department" %}</option>
                        {% for department in departments %}
                        <option value="{{ department.id }}">{{ department.name }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">
                    {% trans "Roles" %} <span class="text-red-500">*</span>
                </label>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-3">
                    {% for role in roles %}
                    <label class="flex items-center gap-2 p-3 border-2 border-gray-200 rounded-xl cursor-pointer hover:border-navy hover:bg-blue-50 transition">
                        <input type="checkbox" name="roles" value="{{ role.name }}" class="w-4 h-4 text-navy rounded focus:ring-navy">
                        <span class="text-sm">{{ role.display_name }}</span>
                    </label>
                    {% endfor %}
                </div>
            </div>
            
            <div class="flex gap-3 pt-4 border-t">
                <button type="button" onclick="closeCreateModal()"
                    class="flex-1 px-6 py-3 border-2 border-gray-300 text-gray-700 rounded-xl font-medium hover:bg-gray-50 transition">
                    {% trans "Cancel" %}
                </button>
                <button type="submit"
                    class="flex-1 px-6 py-3 bg-gradient-to-r from-navy to-blue text-white rounded-xl font-medium hover:shadow-lg transition">
                    {% trans "Create User" %}
                </button>
            </div>
        </form>
    </div>
</div>

<script>
function openCreateModal() {
    document.getElementById('createUserModal').classList.remove('hidden');
    document.getElementById('createUserModal').classList.add('flex');
    lucide.createIcons();
}

function closeCreateModal() {
    document.getElementById('createUserModal').classList.add('hidden');
    document.getElementById('createUserModal').classList.remove('flex');
}

// Close modal when clicking outside
document.getElementById('createUserModal').addEventListener('click', function(e) {
    if (e.target === this) {
        closeCreateModal();
    }
});

document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
});
</script>
{% endblock %}
