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

{% block title %}{% trans "My Acknowledgements" %} - PX360{% 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="clipboard-signature" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">{% trans "My Acknowledgements" %}</h1>
                    <p class="text-white/80 text-sm">{% trans "Review and sign required documents" %}</p>
                </div>
            </div>
            <div class="flex gap-3">
                {% if user.is_px_admin or user.is_staff %}
                <a href="{% url 'accounts:simple_acknowledgements:admin_create' %}"
                   class="inline-flex items-center gap-2 px-4 py-2 bg-white text-emerald-700 rounded-xl font-semibold hover:bg-emerald-50 transition shadow-lg">
                    <i data-lucide="plus-circle" class="w-4 h-4"></i>
                    {% trans "Create New" %}
                </a>
                <a href="{% url 'accounts:simple_acknowledgements:admin_list' %}"
                   class="inline-flex items-center gap-2 px-4 py-2 bg-white/20 border border-white/30 text-white rounded-xl font-semibold hover:bg-white/30 transition">
                    <i data-lucide="settings" class="w-4 h-4"></i>
                    {% trans "Manage" %}
                </a>
                {% endif %}
                <div class="flex items-center gap-2 px-4 py-2 bg-emerald-500 text-white rounded-xl font-semibold">
                    <i data-lucide="check-circle" class="w-4 h-4"></i>
                    {{ signed }} {% trans "Signed" %}
                </div>
                {% if pending > 0 %}
                <div class="flex items-center gap-2 px-4 py-2 bg-orange-500 text-white rounded-xl font-semibold animate-pulse">
                    <i data-lucide="alert-circle" class="w-4 h-4"></i>
                    {{ pending }} {% trans "Pending" %}
                </div>
                {% endif %}
            </div>
        </div>
    </div>

    {% if pending > 0 %}
    <!-- Alert Banner -->
    <div class="section-card mb-6 border-orange-300 bg-gradient-to-r from-orange-500 to-orange-600">
        <div class="p-6 text-white">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                    <i data-lucide="bell" class="w-6 h-6"></i>
                </div>
                <div class="flex-1">
                    <h2 class="text-lg font-bold">{% trans "Action Required" %}</h2>
                    <p class="text-orange-100">
                        {% blocktrans %}
                        You have {{ pending }} acknowledgement(s) waiting for your signature. Please review and sign them below.
                        {% endblocktrans %}
                    </p>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- Progress Card -->
    <div class="section-card mb-6">
        <div class="p-6">
            <div class="flex justify-between items-center mb-3">
                <span class="font-semibold text-navy">{% trans "Completion Progress" %}</span>
                <span class="text-2xl font-bold text-blue">{{ progress }}%</span>
            </div>
            <div class="w-full bg-slate-200 rounded-full h-3 overflow-hidden">
                <div class="bg-gradient-to-r from-emerald-400 to-emerald-500 h-3 rounded-full transition-all duration-500" style="width: {{ progress }}%"></div>
            </div>
        </div>
    </div>

    <!-- Pending Acknowledgements -->
    {% if pending_acks %}
    <div class="section-card mb-6 border-orange-200">
        <div class="section-header border-orange-200 bg-gradient-to-r from-orange-50 to-transparent">
            <div class="section-icon bg-orange-100">
                <i data-lucide="alert-circle" class="w-5 h-5 text-orange-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">{% trans "Pending Signatures" %}</h2>
            <span class="bg-orange-100 text-orange-700 px-2 py-0.5 rounded-full text-sm">{{ pending_acks|length }}</span>
        </div>
        <div class="divide-y divide-orange-50">
            {% for item in pending_acks %}
            <div class="p-6 flex flex-col md:flex-row md:items-center justify-between gap-4 hover:bg-orange-50/30 transition">
                <div class="flex items-start gap-4">
                    <div class="flex-shrink-0 mt-1">
                        <div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center">
                            <i data-lucide="file-signature" class="w-6 h-6 text-orange-500"></i>
                        </div>
                    </div>
                    <div>
                        <h6 class="font-bold text-navy mb-1">{{ item.acknowledgement.title }}</h6>
                        {% if item.acknowledgement.description %}
                            <p class="text-sm text-slate mb-2">{{ item.acknowledgement.description|truncatewords:20 }}</p>
                        {% endif %}
                        <div class="flex items-center gap-3 text-sm">
                            {% if item.sent_at %}
                                <span class="inline-flex items-center gap-1 text-purple-600">
                                    <i data-lucide="send" class="w-3 h-3"></i>
                                    {% trans "Sent" %} {{ item.sent_at|date:"M d, Y" }}
                                </span>
                            {% endif %}
                            {% if item.acknowledgement.is_required %}
                                <span class="inline-flex items-center gap-1 text-red-600">
                                    <i data-lucide="alert-triangle" class="w-3 h-3"></i>
                                    {% trans "Required" %}
                                </span>
                            {% endif %}
                        </div>
                    </div>
                </div>
                <div class="flex gap-2">
                    {% if item.acknowledgement.pdf_document %}
                        <a href="{{ item.acknowledgement.pdf_document.url }}" target="_blank"
                           class="inline-flex items-center gap-2 px-4 py-2 bg-blue-50 text-blue-700 rounded-xl font-semibold hover:bg-blue-100 transition">
                            <i data-lucide="file-text" class="w-4 h-4"></i>
                            {% trans "View PDF" %}
                        </a>
                    {% endif %}
                    <a href="{% url 'accounts:simple_acknowledgements:sign' item.acknowledgement.id %}"
                       class="inline-flex items-center gap-2 px-4 py-2 bg-gradient-to-r from-orange-500 to-orange-600 text-white rounded-xl font-semibold hover:from-orange-600 hover:to-orange-700 transition shadow-lg shadow-orange-200">
                        <i data-lucide="pen-tool" class="w-4 h-4"></i>
                        {% trans "Sign Now" %}
                    </a>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}

    <!-- Completed Acknowledgements -->
    {% if signed_acks %}
    <div class="section-card border-emerald-200">
        <div class="section-header border-emerald-200 bg-gradient-to-r from-emerald-50 to-transparent">
            <div class="section-icon bg-emerald-100">
                <i data-lucide="check-circle" class="w-5 h-5 text-emerald-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">{% trans "Completed" %}</h2>
            <span class="bg-emerald-100 text-emerald-700 px-2 py-0.5 rounded-full text-sm">{{ signed_acks|length }}</span>
        </div>
        <div class="divide-y divide-emerald-50">
            {% for item in signed_acks %}
            <div class="p-6 flex flex-col md:flex-row md:items-center justify-between gap-4 hover:bg-emerald-50/30 transition opacity-75">
                <div class="flex items-start gap-4">
                    <div class="flex-shrink-0 mt-1">
                        <div class="w-10 h-10 bg-emerald-100 rounded-full flex items-center justify-center">
                            <i data-lucide="check" class="w-6 h-6 text-emerald-600"></i>
                        </div>
                    </div>
                    <div>
                        <h6 class="font-bold text-navy mb-1">{{ item.acknowledgement.title }}</h6>
                        {% if item.acknowledgement.description %}
                            <p class="text-sm text-slate mb-2">{{ item.acknowledgement.description|truncatewords:20 }}</p>
                        {% endif %}
                        <div class="flex items-center gap-2 text-sm text-emerald-600">
                            <i data-lucide="calendar-check" class="w-4 h-4"></i>
                            {% trans "Signed on" %} {{ item.signed_at|date:"M d, Y" }}
                            {% if item.signature_name %}
                                <span class="text-slate">• {{ item.signature_name }}</span>
                            {% endif %}
                        </div>
                    </div>
                </div>
                <div class="flex gap-2">
                    {% if item.has_pdf %}
                        <a href="{{ item.signature.signed_pdf.url }}"
                           class="inline-flex items-center gap-2 px-4 py-2 bg-emerald-50 text-emerald-700 rounded-xl font-semibold hover:bg-emerald-100 transition">
                            <i data-lucide="download" class="w-4 h-4"></i>
                            {% trans "Download" %}
                        </a>
                    {% endif %}
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}

    {% if not pending_acks and not signed_acks %}
    <div class="section-card">
        <div class="text-center py-12">
            <div class="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mx-auto mb-4">
                <i data-lucide="clipboard-check" class="w-8 h-8 text-slate-400"></i>
            </div>
            <p class="text-slate font-medium">{% trans "No acknowledgements available at this time." %}</p>
            <p class="text-sm text-slate mt-2">{% trans "You will be notified when new acknowledgements are assigned to you." %}</p>
        </div>
    </div>
    {% endif %}
</div>

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