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

{% block title %}{% trans "Staff Management" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    /* Page Header Gradient */
    .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 Cards */
    .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;
    }

    /* Stats Cards */
    .stat-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        padding: 1.5rem;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;
    }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        border-color: #005696;
    }

    /* Filter Card */
    .filter-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        overflow: hidden;
        margin-bottom: 1.5rem;
        transition: all 0.3s ease;
    }
    .filter-card:hover {
        border-color: #005696;
    }

    .filter-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;
    }

    /* Table Row Hover */
    .table-row-hover {
        transition: all 0.2s ease;
        border-left: 3px solid transparent;
    }
    .table-row-hover:hover {
        background: #f8fafc;
        border-left-color: #007bbd;
    }

    /* Status Badge */
    .status-badge {
        display: inline-flex;
        align-items: center;
        padding: 0.375rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.025em;
    }
</style>
{% endblock %}

{% block content %}
<div class="p-6">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold mb-1">{% trans "Staff Management" %}</h1>
                <p class="text-blue-100 text-sm">{% trans "Manage hospital staff and their user accounts" %}</p>
            </div>
            {% if user.is_px_admin or user.is_hospital_admin %}
            <div class="flex items-center gap-2">
                <a href="{% url 'organizations:staff_import' %}" class="inline-flex items-center px-4 py-2.5 bg-white/20 text-white font-medium rounded-xl hover:bg-white/30 transition">
                    <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Import" %}
                </a>
                <a href="{% url 'organizations:staff_create' %}" class="inline-flex items-center px-4 py-2.5 bg-white text-navy font-medium rounded-xl hover:bg-blue-50 transition">
                    <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "Add New Staff" %}
                </a>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
        <div class="stat-card">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-12 h-12 bg-navy/10 rounded-xl flex items-center justify-center">
                        <i data-lucide="users" class="w-6 h-6 text-navy"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Total Staff" %}</h6>
                    <h2 class="text-2xl font-bold text-gray-800">{{ staff.paginator.count|default:0 }}</h2>
                </div>
            </div>
        </div>
        <div class="stat-card">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-12 h-12 bg-green-500/10 rounded-xl flex items-center justify-center">
                        <i data-lucide="user-check" class="w-6 h-6 text-green-600"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Active" %}</h6>
                    <h2 class="text-2xl font-bold text-gray-800">{{ staff.paginator.count|default:0 }}</h2>
                </div>
            </div>
        </div>
        <div class="stat-card">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-12 h-12 bg-blue-500/10 rounded-xl flex items-center justify-center">
                        <i data-lucide="link" class="w-6 h-6 text-blue"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Linked Users" %}</h6>
                    <h2 class="text-2xl font-bold text-gray-800">{{ staff.paginator.count|default:0 }}</h2>
                </div>
            </div>
        </div>
        <div class="stat-card">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-12 h-12 bg-yellow-500/10 rounded-xl flex items-center justify-center">
                        <i data-lucide="crown" class="w-6 h-6 text-yellow-600"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Dept Heads" %}</h6>
                    <h2 class="text-2xl font-bold text-gray-800">{{ staff.paginator.count|default:0 }}</h2>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="filter-card">
        <div class="filter-header">
            <div class="section-icon bg-navy/10">
                <i data-lucide="filter" class="w-5 h-5 text-navy"></i>
            </div>
            <h5 class="text-base font-semibold text-gray-800">{% trans "Filters" %}</h5>
        </div>
        <div class="p-5">
            <form method="get" class="flex flex-wrap gap-4 items-end">
                <div class="flex items-center gap-2">
                    <label class="text-xs font-bold text-slate uppercase">{% trans "Status" %}</label>
                    <select name="status" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                        <option value="">{% trans "All" %}</option>
                        <option value="active" {% if request.GET.status == 'active' %}selected{% endif %}>{% trans "Active" %}</option>
                        <option value="inactive" {% if request.GET.status == 'inactive' %}selected{% endif %}>{% trans "Inactive" %}</option>
                    </select>
                </div>
                <div class="flex items-center gap-2">
                    <label class="text-xs font-bold text-slate uppercase">{% trans "Type" %}</label>
                    <select name="department_type" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                        <option value="">{% trans "All" %}</option>
                        <option value="nursing" {% if request.GET.department_type == 'nursing' %}selected{% endif %}>{% trans "Nursing" %}</option>
                        <option value="medical" {% if request.GET.department_type == 'medical' %}selected{% endif %}>{% trans "Medical" %}</option>
                        <option value="non_medical" {% if request.GET.department_type == 'non_medical' %}selected{% endif %}>{% trans "Non-Medical" %}</option>
                        <option value="support_services" {% if request.GET.department_type == 'support_services' %}selected{% endif %}>{% trans "Support Services" %}</option>
                    </select>
                </div>
                <div class="flex items-center gap-2">
                    <label class="text-xs font-bold text-slate uppercase">{% trans "Department" %}</label>
                    <select name="department" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy" data-tomselect data-ts-compact>
                        <option value="">{% trans "All" %}</option>
                        {% for dept in departments %}
                        <option value="{{ dept.id }}" {% if request.GET.department == dept.id|stringformat:"s" %}selected{% endif %}>{{ dept.get_localized_name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="flex items-center gap-2">
                    <label class="text-xs font-bold text-slate uppercase">{% trans "Section" %}</label>
                    <select name="section" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                        <option value="">{% trans "All" %}</option>
                        {% for sec_value, sec_label in sections %}
                        <option value="{{ sec_value }}" {% if request.GET.section == sec_value %}selected{% endif %}>{{ sec_label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="flex items-center gap-2">
                    <label class="text-xs font-bold text-slate uppercase">{% trans "Search" %}</label>
                    <input type="text" name="search" class="px-3 py-1.5 border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy w-40" placeholder="{% trans 'Name, ID...' %}" value="{{ request.GET.search|default:'' }}">
                </div>
                <div class="flex items-center gap-2">
                    <button type="submit" class="px-4 py-1.5 bg-navy text-white rounded-lg text-xs font-bold hover:bg-blue transition">{% trans "Apply" %}</button>
                    <a href="{% url 'organizations:staff_list' %}" class="px-4 py-1.5 border border-slate-200 rounded-lg text-xs font-semibold text-slate hover:bg-light transition">{% trans "Clear" %}</a>
                </div>
            </form>
        </div>
    </div>

    <!-- Staff List Section -->
    <div class="section-card">
        <div class="section-header flex justify-between items-center">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-navy/10">
                    <i data-lucide="users" class="w-5 h-5 text-navy"></i>
                </div>
                <h5 class="text-lg font-semibold text-gray-800">{% trans "Staff List" %}</h5>
            </div>
            <div class="text-xs text-slate font-medium">
                {% trans "Showing" %} <span class="font-bold text-navy">{{ staff.start_index|default:0 }}-{{ staff.end_index|default:0 }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ staff.paginator.count|default:0 }}</span>
            </div>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-slate-50 border-b border-slate-200">
                    <tr class="text-xs font-bold text-slate uppercase tracking-wider">
                        <th class="px-5 py-3 text-left">{% trans "Name" %}</th>
                        <th class="px-5 py-3 text-left">{% trans "Type" %}</th>
                        <th class="px-5 py-3 text-left">{% trans "Job Title" %}</th>
                        <th class="px-5 py-3 text-left">{% trans "Department" %}</th>
                        <th class="px-5 py-3 text-left">{% trans "Manager" %}</th>
                        <th class="px-5 py-3 text-left">{% trans "User Account" %}</th>
                        <th class="px-5 py-3 text-left">{% trans "Status" %}</th>
                        <th class="px-5 py-3 text-left">{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody class="text-sm divide-y divide-slate-100">
                    {% for staff_member in staff.object_list %}
                    <tr class="table-row-hover cursor-pointer" onclick="window.location.href='{% url 'organizations:staff_detail' staff_member.id %}'">
                        <td class="px-5 py-4">
                            <div class="flex items-center gap-2">
                                <div class="w-8 h-8 bg-navy rounded-full flex items-center justify-center text-white font-bold text-xs">
                                    {{ staff_member.get_localized_name.0 }}
                                </div>
                                <div>
                                    <div class="font-bold text-navy">
                                        {{ staff_member.get_localized_name }}
                                    </div>
                                    {% if staff_member.license_number %}
                                    <div class="text-xs text-slate">{{ staff_member.license_number }}</div>
                                    {% endif %}
                                </div>
                                {% if staff_member.is_head %}
                                <span class="ml-2 bg-yellow-100 text-yellow-700 text-[10px] px-2 py-0.5 rounded-full font-bold" title="Department Head">
                                    <i data-lucide="crown" class="w-3 h-3 inline"></i>
                                </span>
                                {% endif %}
                            </div>
                        </td>
                        <td class="px-5 py-4">
                            <span class="px-2 py-1 rounded-lg text-xs font-bold bg-light text-navy">
                                {{ staff_member.get_department_type_display }}
                            </span>
                        </td>
                        <td class="px-5 py-4 text-slate-800">{{ staff_member.get_localized_job_title|default:"-" }}</td>
                        <td class="px-5 py-4">
                            {% if staff_member.department %}
                                <span class="px-2 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-600">{{ staff_member.department.get_localized_name }}</span>
                            {% else %}
                                <span class="text-slate text-xs">-</span>
                            {% endif %}
                        </td>
                        <td class="px-5 py-4">
                            {% if staff_member.report_to %}
                            <a href="{% url 'organizations:staff_detail' staff_member.report_to.id %}" class="text-blue hover:underline text-xs" onclick="event.stopPropagation();">
                                {{ staff_member.report_to.get_localized_name }}
                            </a>
                            {% else %}
                            <span class="text-slate text-xs">-</span>
                            {% endif %}
                        </td>
                        <td class="px-5 py-4" onclick="event.stopPropagation();">
                            {% if staff_member.user %}
                            <span class="px-2 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-600 flex items-center gap-1 w-fit">
                                <i data-lucide="check" class="w-3 h-3"></i> {% trans "Linked" %}
                            </span>
                            {% else %}
                            <span class="px-2 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-500">
                                {% trans "None" %}
                            </span>
                            {% endif %}
                        </td>
                        <td class="px-5 py-4">
                            {% if staff_member.status == 'active' %}
                            <span class="status-badge bg-green-100 text-green-600">{% trans "Active" %}</span>
                            {% else %}
                            <span class="status-badge bg-slate-100 text-slate-500">{% trans "Inactive" %}</span>
                            {% endif %}
                        </td>
                        <td class="px-5 py-4" onclick="event.stopPropagation();">
                            <div class="flex gap-1">
                                <a href="{% url 'organizations:staff_detail' staff_member.id %}" class="p-1.5 text-navy hover:bg-light rounded-lg transition" title="{% trans 'View' %}">
                                    <i data-lucide="eye" class="w-4 h-4"></i>
                                </a>
                                {% if user.is_px_admin or user.is_hospital_admin or user.is_department_manager and staff_member.department == user.department %}
                                {% if not staff_member.user and staff_member.email %}
                                <button type="button" class="p-1.5 text-green-600 hover:bg-green-50 rounded-lg transition" onclick="createUserAccount('{{ staff_member.id }}', '{{ staff_member.get_full_name }}')" title="{% trans 'Create User' %}">
                                    <i data-lucide="user-plus" class="w-4 h-4"></i>
                                </button>
                                {% endif %}
                                {% if staff_member.user %}
                                <button type="button" class="p-1.5 text-blue hover:bg-blue-50 rounded-lg transition" onclick="sendInvitation('{{ staff_member.id }}', '{{ staff_member.get_full_name }}')" title="{% trans 'Send Invite' %}">
                                    <i data-lucide="mail" class="w-4 h-4"></i>
                                </button>
                                {% endif %}
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="8" class="px-5 py-12 text-center">
                            <div class="flex flex-col items-center justify-center">
                                <div class="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mb-4">
                                    <i data-lucide="users" class="w-8 h-8 text-slate-300"></i>
                                </div>
                                <p class="text-slate font-medium mb-2">{% trans "No staff members found" %}</p>
                                <p class="text-slate text-sm mb-4">{% trans "Add your first staff member to get started" %}</p>
                                <a href="{% url 'organizations:staff_create' %}" 
                                   class="inline-flex items-center px-4 py-2 bg-navy text-white font-medium rounded-lg hover:bg-blue transition">
                                    <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "Add Staff" %}
                                </a>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        {% if staff.has_other_pages %}
        <div class="bg-slate-50 px-6 py-4 flex items-center justify-between border-t border-slate-100">
            <div class="flex items-center gap-4">
                <span class="text-xs text-slate font-medium">
                    {% trans "Showing" %} <span class="font-bold text-navy">{{ staff.start_index }}-{{ staff.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ staff.paginator.count }}</span> {% trans "entries" %}
                </span>
                <!-- Page Size Selector -->
                <form method="get" class="flex items-center gap-2" id="pageSizeForm">
                    {% for key, value in request.GET.items %}
                        {% if key != 'page_size' and key != 'page' %}
                        <input type="hidden" name="{{ key }}" value="{{ value }}">
                        {% endif %}
                    {% endfor %}
                    <label class="text-xs text-slate">{% trans "Show" %}</label>
                    <select name="page_size" onchange="document.getElementById('pageSizeForm').submit()" class="px-2 py-1 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                        <option value="10" {% if staff.paginator.per_page == 10 %}selected{% endif %}>10</option>
                        <option value="25" {% if staff.paginator.per_page == 25 %}selected{% endif %}>25</option>
                        <option value="50" {% if staff.paginator.per_page == 50 %}selected{% endif %}>50</option>
                        <option value="100" {% if staff.paginator.per_page == 100 %}selected{% endif %}>100</option>
                    </select>
                </form>
            </div>
            <div class="flex gap-1">
                {% if staff.has_previous %}
                <a href="?page={{ staff.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
                   class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-left" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-left" class="w-4 h-4"></i>
                </span>
                {% endif %}
                
                {% for num in staff.paginator.page_range %}
                    {% if num == staff.number %}
                    <span class="w-8 h-8 flex items-center justify-center rounded-lg bg-navy text-white text-xs font-bold">{{ num }}</span>
                    {% elif num > staff.number|add:'-3' and num < staff.number|add:'3' %}
                    <a href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
                       class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                        {{ num }}
                    </a>
                    {% elif num == 1 or num == staff.paginator.num_pages %}
                    <a href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
                       class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                        {{ num }}
                    </a>
                    {% elif num == staff.number|add:'-3' or num == staff.number|add:'3' %}
                    <span class="w-8 h-8 flex items-center justify-center text-xs text-slate">...</span>
                    {% endif %}
                {% endfor %}
                
                {% if staff.has_next %}
                <a href="?page={{ staff.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
                   class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-right" class="w-4 h-4"></i>
                </span>
                {% endif %}
            </div>
        </div>
        {% endif %}
    </div>
</div>

<!-- Create User Modal -->
<div class="hidden fixed inset-0 bg-black/50 flex items-center justify-center z-50" id="createUserModal">
    <div class="bg-white rounded-2xl shadow-2xl max-w-md w-full mx-4 overflow-hidden">
        <div class="bg-navy text-white px-6 py-4 flex justify-between items-center">
            <h5 class="font-bold text-lg">{% trans "Create User Account" %}</h5>
            <button type="button" class="text-white hover:bg-blue rounded-full p-1 transition" onclick="closeModal('createUserModal')">
                <i data-lucide="x" class="w-5 h-5"></i>
            </button>
        </div>
        <div class="p-6">
            <p class="text-slate mb-2">{% trans "Create a user account for" %} <strong class="text-navy" id="createUserName"></strong>?</p>
            <p class="text-slate text-sm">{% trans "Credentials will be emailed to the staff member." %}</p>
        </div>
        <div class="px-6 py-4 bg-slate-50 flex justify-end gap-3">
            <button type="button" class="px-4 py-2 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-white transition" onclick="closeModal('createUserModal')">{% trans "Cancel" %}</button>
            <button type="button" class="px-4 py-2 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition" onclick="confirmCreateUser()">{% trans "Create" %}</button>
        </div>
    </div>
</div>

<!-- Send Invitation Modal -->
<div class="hidden fixed inset-0 bg-black/50 flex items-center justify-center z-50" id="sendInvitationModal">
    <div class="bg-white rounded-2xl shadow-2xl max-w-md w-full mx-4 overflow-hidden">
        <div class="bg-navy text-white px-6 py-4 flex justify-between items-center">
            <h5 class="font-bold text-lg">{% trans "Send Invitation" %}</h5>
            <button type="button" class="text-white hover:bg-blue rounded-full p-1 transition" onclick="closeModal('sendInvitationModal')">
                <i data-lucide="x" class="w-5 h-5"></i>
            </button>
        </div>
        <div class="p-6">
            <p class="text-slate mb-2">{% trans "Send invitation email to" %} <strong class="text-navy" id="sendInvitationName"></strong>?</p>
            <p class="text-slate text-sm">{% trans "A secure password reset link will be emailed." %}</p>
        </div>
        <div class="px-6 py-4 bg-slate-50 flex justify-end gap-3">
            <button type="button" class="px-4 py-2 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-white transition" onclick="closeModal('sendInvitationModal')">{% trans "Cancel" %}</button>
            <button type="button" class="px-4 py-2 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition" onclick="confirmSendInvitation()">{% trans "Send" %}</button>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_js %}
<script>
const CSRF_TOKEN = '{{ csrf_token }}';
let currentStaffId = null;
let currentStaffName = null;

function closeModal(modalId) {
    document.getElementById(modalId).classList.add('hidden');
}

function showModal(modalId) {
    document.getElementById(modalId).classList.remove('hidden');
}

function createUserAccount(staffId, staffName) {
    currentStaffId = staffId;
    currentStaffName = staffName;
    document.getElementById('createUserName').textContent = staffName;
    showModal('createUserModal');
}

function confirmCreateUser() {
    const btn = event.target;
    btn.disabled = true;
    btn.innerHTML = '<i data-lucide="loader-2" class="w-4 h-4 animate-spin inline"></i> {% trans "Creating..." %}';
    
    fetch(`/organizations/api/staff/${currentStaffId}/create_user_account/`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': CSRF_TOKEN
        },
        body: JSON.stringify({})
    })
    .then(response => response.json())
    .then(data => {
        closeModal('createUserModal');
        if (data.message) alert(data.message);
        location.reload();
    })
    .catch(error => {
        alert('Error: ' + error.message);
        btn.disabled = false;
        btn.innerHTML = '{% trans "Create" %}';
    });
}

function sendInvitation(staffId, staffName) {
    currentStaffId = staffId;
    currentStaffName = staffName;
    document.getElementById('sendInvitationName').textContent = staffName;
    showModal('sendInvitationModal');
}

function confirmSendInvitation() {
    const btn = event.target;
    btn.disabled = true;
    btn.innerHTML = '<i data-lucide="loader-2" class="w-4 h-4 animate-spin inline"></i> {% trans "Sending..." %}';
    
    fetch(`/organizations/api/staff/${currentStaffId}/send_invitation/`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': CSRF_TOKEN
        },
        body: JSON.stringify({})
    })
    .then(response => response.json())
    .then(data => {
        closeModal('sendInvitationModal');
        if (data.message) alert(data.message);
        location.reload();
    })
    .catch(error => {
        alert('Error: ' + error.message);
        btn.disabled = false;
        btn.innerHTML = '{% trans "Send" %}';
    });
}

// Re-initialize Lucide icons after modal content changes
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
});
</script>
{% endblock %}
