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

{% block title %}{% trans "Employee Signatures" %} - 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 flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-8">
        <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-2xl shadow-lg shadow-blue-200">
                <i data-lucide="users" class="w-6 h-6 text-white"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold text-navy">{% trans "Employee Signatures" %}</h1>
                <p class="text-slate text-sm">{% trans "View all signed acknowledgements" %}</p>
            </div>
        </div>
        <div class="flex gap-3">
            <a href="{% url 'accounts:simple_acknowledgements:admin_export' %}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-white border-2 border-slate-200 text-slate-700 rounded-xl font-semibold hover:bg-slate-50 transition">
                <i data-lucide="download" class="w-4 h-4"></i>
                {% trans "Export CSV" %}
            </a>
            <a href="{% url 'accounts:simple_acknowledgements:admin_list' %}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-gradient-to-r from-blue to-navy text-white rounded-xl font-semibold hover:shadow-lg transition">
                <i data-lucide="clipboard-list" class="w-4 h-4"></i>
                {% trans "Manage Acknowledgements" %}
            </a>
        </div>
    </div>

    {% if acknowledgement %}
    <!-- Filter by Acknowledgement -->
    <div class="bg-blue-50 border border-blue-100 rounded-xl p-4 mb-6">
        <div class="flex items-center justify-between">
            <div class="flex items-center gap-3">
                <i data-lucide="filter" class="w-5 h-5 text-blue"></i>
                <div>
                    <p class="text-sm text-slate">{% trans "Filtered by:" %}</p>
                    <p class="font-semibold text-navy">{{ acknowledgement.title }}</p>
                </div>
            </div>
            <a href="{% url 'accounts:simple_acknowledgements:admin_signatures' %}"
               class="text-sm text-blue hover:underline">
                {% trans "Clear filter" %}
            </a>
        </div>
    </div>
    {% endif %}

    <!-- Stats -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-8">
        <div class="bg-white rounded-2xl shadow-sm border border-slate-200 p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-blue-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="file-signature" class="w-5 h-5 text-blue"></i>
                </div>
                <div>
                    <p class="text-xs text-slate uppercase font-semibold">{% trans "Total Signatures" %}</p>
                    <p class="text-2xl font-bold text-navy">{{ signatures|length }}</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-2xl shadow-sm border border-emerald-100 p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-emerald-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="check-circle" class="w-5 h-5 text-emerald-600"></i>
                </div>
                <div>
                    <p class="text-xs text-slate uppercase font-semibold">{% trans "This Month" %}</p>
                    <p class="text-2xl font-bold text-emerald-600">{{ recent_count|default:0 }}</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-2xl shadow-sm border border-slate-200 p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-purple-100 rounded-xl flex items-center justify-center">
                    <i data-lucide="users" class="w-5 h-5 text-purple-600"></i>
                </div>
                <div>
                    <p class="text-xs text-slate uppercase font-semibold">{% trans "Unique Employees" %}</p>
                    <p class="text-2xl font-bold text-purple-600">{{ unique_employees|default:0 }}</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Signatures Table -->
    <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-slate-50">
            <h2 class="font-bold text-navy">{% trans "All Signatures" %}</h2>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-slate-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Employee" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Acknowledgement" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Status" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Date" %}</th>
                        <th class="px-6 py-3 text-center text-xs font-bold text-slate uppercase tracking-wider">{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    {% for sig in signatures %}
                    <tr class="hover:bg-slate-50 transition">
                        <td class="px-6 py-4">
                            <div class="flex items-center gap-3">
                                <div class="w-8 h-8 bg-blue rounded-lg flex items-center justify-center text-white font-bold text-sm">
                                    {{ sig.employee.first_name|first|default:"U" }}{{ sig.employee.last_name|first|default:"" }}
                                </div>
                                <div>
                                    <div class="font-semibold text-navy">{{ sig.employee.get_full_name|default:sig.employee.email }}</div>
                                    <div class="text-sm text-slate">{{ sig.employee.email }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <span class="font-medium text-navy">{{ sig.acknowledgement.title }}</span>
                        </td>
                        <td class="px-6 py-4">
                            {% if sig.is_signed %}
                                <span class="inline-flex items-center gap-1 px-2.5 py-1 bg-emerald-100 text-emerald-700 rounded-lg text-xs font-bold">
                                    <i data-lucide="check-circle" class="w-3 h-3"></i>
                                    {% trans "Signed" %}
                                </span>
                            {% elif sig.sent_at %}
                                <span class="inline-flex items-center gap-1 px-2.5 py-1 bg-purple-100 text-purple-700 rounded-lg text-xs font-bold">
                                    <i data-lucide="send" class="w-3 h-3"></i>
                                    {% trans "Sent" %}
                                </span>
                            {% else %}
                                <span class="inline-flex items-center gap-1 px-2.5 py-1 bg-slate-100 text-slate-600 rounded-lg text-xs font-bold">
                                    <i data-lucide="circle" class="w-3 h-3"></i>
                                    {% trans "Pending" %}
                                </span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            <div class="text-sm text-slate">
                                {% if sig.is_signed %}
                                    {{ sig.signed_at|date:"M d, Y" }}
                                    <div class="text-xs">{{ sig.signed_at|time:"H:i" }}</div>
                                {% elif sig.sent_at %}
                                    {{ sig.sent_at|date:"M d, Y" }}
                                    <div class="text-xs">{{ sig.sent_at|time:"H:i" }}</div>
                                {% else %}
                                    -
                                {% endif %}
                            </div>
                        </td>
                        <td class="px-6 py-4 text-center">
                            <div class="flex items-center justify-center gap-2">
                                {% if sig.is_signed %}
                                    {% if sig.signed_pdf %}
                                        <a href="{{ sig.signed_pdf.url }}"
                                           class="inline-flex items-center gap-1 px-3 py-1.5 bg-emerald-50 text-emerald-600 rounded-lg text-sm font-semibold hover:bg-emerald-100 transition">
                                            <i data-lucide="download" class="w-4 h-4"></i>
                                            {% trans "Download" %}
                                        </a>
                                    {% else %}
                                        <a href="{% url 'accounts:simple_acknowledgements:admin_upload_pdf' sig.id %}"
                                           class="inline-flex items-center gap-1 px-3 py-1.5 bg-amber-50 text-amber-600 rounded-lg text-sm font-semibold hover:bg-amber-100 transition">
                                            <i data-lucide="upload" class="w-4 h-4"></i>
                                            {% trans "Upload PDF" %}
                                        </a>
                                    {% endif %}
                                {% else %}
                                    <a href="{% url 'accounts:simple_acknowledgements:admin_upload_pdf' sig.id %}"
                                       class="inline-flex items-center gap-1 px-3 py-1.5 bg-amber-50 text-amber-600 rounded-lg text-sm font-semibold hover:bg-amber-100 transition"
                                       title="{% trans 'Upload signed PDF on behalf of employee' %}">
                                        <i data-lucide="upload" class="w-4 h-4"></i>
                                        {% trans "Upload Signed" %}
                                    </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="5" class="px-6 py-12 text-center text-slate">
                            <div class="flex flex-col items-center gap-3">
                                <i data-lucide="file-x" class="w-12 h-12 text-slate-300"></i>
                                <p class="font-medium">{% trans "No signatures yet" %}</p>
                                <p class="text-sm">{% trans "Employees will appear here after signing acknowledgements" %}</p>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

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