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

{% block title %}{% trans "Send Acknowledgement" %} - PX360{% endblock %}

{% block content %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <!-- Header -->
    <div class="flex items-center gap-3 mb-8">
        <div class="flex items-center justify-center w-12 h-12 bg-gradient-to-br from-purple-500 to-purple-600 rounded-2xl shadow-lg shadow-purple-200">
            <i data-lucide="send" class="w-6 h-6 text-white"></i>
        </div>
        <div>
            <h1 class="text-2xl font-bold text-navy">{% trans "Send Acknowledgement" %}</h1>
            <p class="text-slate text-sm">{% trans "Select staff members to send this acknowledgement to" %}</p>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Main Form -->
        <div class="lg:col-span-2">
            <div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden">
                <div class="px-6 py-4 border-b border-slate-100 bg-gradient-to-r from-purple-50 to-transparent">
                    <h2 class="font-bold text-navy flex items-center gap-2">
                        <i data-lucide="users" class="w-5 h-5 text-purple-600"></i>
                        {{ acknowledgement.title }}
                    </h2>
                </div>

                <form method="post" class="p-6 space-y-6">
                    {% csrf_token %}

                    <!-- Staff Selection -->
                    <div>
                        <div class="flex justify-between items-center mb-4">
                            <label class="block text-sm font-bold text-navy">
                                {% trans "Select Staff Members" %}
                            </label>
                            <div class="flex gap-2">
                                <button type="button" onclick="selectAll()"
                                        class="text-sm text-purple-600 hover:text-purple-700 font-semibold">
                                    {% trans "Select All" %}
                                </button>
                                <span class="text-slate">|</span>
                                <button type="button" onclick="deselectAll()"
                                        class="text-sm text-slate hover:text-navy font-semibold">
                                    {% trans "Deselect All" %}
                                </button>
                            </div>
                        </div>

                        <div class="border border-slate-200 rounded-xl overflow-hidden">
                            <div class="bg-slate-50 px-4 py-2 border-b border-slate-200 flex items-center gap-2">
                                <i data-lucide="search" class="w-4 h-4 text-slate"></i>
                                <input type="text" id="staffSearch" placeholder="{% trans 'Search staff...' %}"
                                       class="bg-transparent border-none focus:outline-none text-sm w-full"
                                       onkeyup="filterStaff()">
                            </div>
                            <div class="max-h-96 overflow-y-auto" id="staffList">
                                {% for staff in staff_list %}
                                <label class="staff-item flex items-center gap-3 p-3 hover:bg-slate-50 transition cursor-pointer border-b border-slate-100 last:border-0 {% if staff.id in already_sent_ids %}bg-purple-50/50{% endif %}">
                                    <input type="checkbox" name="employees" value="{{ staff.id }}"
                                           {% if staff.id in already_sent_ids %}checked disabled{% endif %}
                                           class="w-5 h-5 text-purple-500 rounded focus:ring-purple-500">
                                    <div class="flex-1">
                                        <div class="font-semibold text-navy">
                                            {{ staff.get_full_name|default:staff.email }}
                                            {% if staff.id in already_sent_ids %}
                                                <span class="text-xs bg-purple-100 text-purple-700 px-2 py-0.5 rounded-full ml-2">{% trans "Already sent" %}</span>
                                            {% endif %}
                                        </div>
                                        <div class="text-sm text-slate">{{ staff.email }}</div>
                                        {% if staff.department %}
                                            <div class="text-xs text-slate mt-1">{{ staff.department.name }}</div>
                                        {% endif %}
                                    </div>
                                </label>
                                {% empty %}
                                <div class="p-6 text-center text-slate">
                                    <i data-lucide="users-x" class="w-12 h-12 mx-auto mb-3 text-slate-300"></i>
                                    <p>{% trans "No staff members found" %}</p>
                                </div>
                                {% endfor %}
                            </div>
                        </div>
                        <p class="text-xs text-slate mt-2">
                            <span id="selectedCount">0</span> {% trans "staff selected" %}
                            {% if already_sent_ids|length > 0 %}
                                • {{ already_sent_ids|length }} {% trans "already sent" %}
                            {% endif %}
                        </p>
                    </div>

                    <!-- Email Notification -->
                    <div class="bg-purple-50 border border-purple-100 rounded-xl p-4">
                        <label class="flex items-start gap-3 cursor-pointer">
                            <input type="checkbox" name="send_email" checked
                                   class="w-5 h-5 text-purple-500 rounded focus:ring-purple-500 mt-0.5">
                            <div>
                                <span class="font-semibold text-navy">{% trans "Send Email Notification" %}</span>
                                <p class="text-xs text-slate mt-1">{% trans "Staff will receive an email with a link to sign the acknowledgement" %}</p>
                            </div>
                        </label>
                    </div>

                    <!-- Actions -->
                    <div class="flex gap-3 pt-4 border-t border-slate-100">
                        <a href="{% url 'accounts:simple_acknowledgements:admin_list' %}"
                           class="inline-flex items-center justify-center gap-2 px-6 py-3 bg-slate-100 text-slate-700 rounded-xl font-semibold hover:bg-slate-200 transition">
                            <i data-lucide="x" class="w-5 h-5"></i>
                            {% trans "Cancel" %}
                        </a>
                        <button type="submit"
                                class="flex-1 inline-flex items-center justify-center gap-2 px-6 py-3 bg-gradient-to-r from-purple-500 to-purple-600 text-white rounded-xl font-semibold hover:from-purple-600 hover:to-purple-700 transition shadow-lg shadow-purple-200">
                            <i data-lucide="send" class="w-5 h-5"></i>
                            {% trans "Send to Selected Staff" %}
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Info Sidebar -->
        <div class="lg:col-span-1">
            <div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden sticky top-6">
                <div class="px-6 py-4 border-b border-slate-100 bg-gradient-to-r from-blue to-navy">
                    <h3 class="font-bold text-white flex items-center gap-2">
                        <i data-lucide="info" class="w-5 h-5"></i>
                        {% trans "How it works" %}
                    </h3>
                </div>
                <div class="p-6 space-y-4">
                    <div class="flex items-start gap-3">
                        <div class="w-8 h-8 bg-purple-100 rounded-lg flex items-center justify-center flex-shrink-0">
                            <span class="font-bold text-purple-600 text-sm">1</span>
                        </div>
                        <div>
                            <p class="font-semibold text-navy text-sm">{% trans "Select Staff" %}</p>
                            <p class="text-xs text-slate mt-1">{% trans "Choose which employees need to sign this acknowledgement" %}</p>
                        </div>
                    </div>

                    <div class="flex items-start gap-3">
                        <div class="w-8 h-8 bg-purple-100 rounded-lg flex items-center justify-center flex-shrink-0">
                            <span class="font-bold text-purple-600 text-sm">2</span>
                        </div>
                        <div>
                            <p class="font-semibold text-navy text-sm">{% trans "Send Notification" %}</p>
                            <p class="text-xs text-slate mt-1">{% trans "Employees receive email with a link to review and sign" %}</p>
                        </div>
                    </div>

                    <div class="flex items-start gap-3">
                        <div class="w-8 h-8 bg-purple-100 rounded-lg flex items-center justify-center flex-shrink-0">
                            <span class="font-bold text-purple-600 text-sm">3</span>
                        </div>
                        <div>
                            <p class="font-semibold text-navy text-sm">{% trans "Track Progress" %}</p>
                            <p class="text-xs text-slate mt-1">{% trans "Monitor who has signed in the signatures page" %}</p>
                        </div>
                    </div>

                    <div class="pt-4 border-t border-slate-100">
                        <a href="{% url 'accounts:simple_acknowledgements:admin_signatures' %}"
                           class="inline-flex items-center gap-2 text-sm text-blue hover:text-navy transition">
                            <i data-lucide="arrow-right" class="w-4 h-4"></i>
                            {% trans "View Signatures" %}
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function selectAll() {
    document.querySelectorAll('input[name="employees"]:not(:disabled)').forEach(cb => cb.checked = true);
    updateCount();
}

function deselectAll() {
    document.querySelectorAll('input[name="employees"]:not(:disabled)').forEach(cb => cb.checked = false);
    updateCount();
}

function updateCount() {
    const count = document.querySelectorAll('input[name="employees"]:checked:not(:disabled)').length;
    document.getElementById('selectedCount').textContent = count;
}

function filterStaff() {
    const search = document.getElementById('staffSearch').value.toLowerCase();
    document.querySelectorAll('.staff-item').forEach(item => {
        const text = item.textContent.toLowerCase();
        item.style.display = text.includes(search) ? '' : 'none';
    });
}

// Update count on checkbox change
document.querySelectorAll('input[name="employees"]').forEach(cb => {
    cb.addEventListener('change', updateCount);
});

// Initialize count
updateCount();

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