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

{% block title %}{% trans "Upload Signed PDF" %} - 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-amber-500 to-amber-600 rounded-2xl shadow-lg shadow-amber-200">
            <i data-lucide="upload" class="w-6 h-6 text-white"></i>
        </div>
        <div>
            <h1 class="text-2xl font-bold text-navy">{% trans "Upload Signed PDF" %}</h1>
            <p class="text-slate text-sm">{% trans "Upload a signed acknowledgement on behalf of an employee" %}</p>
        </div>
    </div>

    <div class="max-w-2xl">
        <div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden">
            <!-- Employee Info -->
            <div class="px-6 py-4 border-b border-slate-100 bg-gradient-to-r from-amber-50 to-transparent">
                <div class="flex items-center gap-4">
                    <div class="w-12 h-12 bg-blue rounded-xl flex items-center justify-center text-white font-bold text-lg">
                        {{ employee.first_name|first|default:"U" }}{{ employee.last_name|first|default:"" }}
                    </div>
                    <div>
                        <h2 class="font-bold text-navy">{{ employee.get_full_name|default:employee.email }}</h2>
                        <p class="text-sm text-slate">{{ employee.email }}</p>
                    </div>
                </div>
            </div>

            <div class="px-6 py-4 border-b border-slate-100 bg-slate-50">
                <h3 class="font-semibold text-navy">{{ emp_ack.acknowledgement.title }}</h3>
                {% if emp_ack.acknowledgement.description %}
                    <p class="text-sm text-slate mt-1">{{ emp_ack.acknowledgement.description|truncatewords:30 }}</p>
                {% endif %}
            </div>

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

                <!-- PDF Upload -->
                <div>
                    <label class="block text-sm font-bold text-navy mb-2">
                        {% trans "Signed PDF Document" %} <span class="text-red-500">*</span>
                    </label>
                    <div class="border-2 border-dashed border-amber-300 rounded-xl p-8 text-center hover:border-amber-500 hover:bg-amber-50/30 transition cursor-pointer"
                         onclick="document.getElementById('pdf-upload').click()">
                        <input type="file" id="pdf-upload" name="signed_pdf" accept=".pdf" required class="hidden"
                               onchange="handleFileSelect(this)">
                        <div id="upload-placeholder">
                            <i data-lucide="file-signature" class="w-12 h-12 text-amber-400 mx-auto mb-3"></i>
                            <p class="text-slate font-medium">{% trans "Click to upload signed PDF" %}</p>
                            <p class="text-sm text-slate mt-1">{% trans "or drag and drop here" %}</p>
                        </div>
                        <div id="upload-preview" class="hidden">
                            <i data-lucide="file-check" class="w-12 h-12 text-emerald-500 mx-auto mb-3"></i>
                            <p class="font-medium text-navy" id="filename"></p>
                            <p class="text-sm text-emerald-600 mt-1">{% trans "Click to change file" %}</p>
                        </div>
                    </div>
                    <p class="text-xs text-slate mt-2">{% trans "Upload the PDF with the employee's signature" %}</p>
                </div>

                <!-- Signature Info -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-bold text-navy mb-2">
                            {% trans "Signed By (Name)" %}
                        </label>
                        <input type="text" name="signature_name"
                               value="{{ employee.get_full_name }}"
                               class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-transparent transition"
                               placeholder="{% trans 'Name on signature' %}">
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-navy mb-2">
                            {% trans "Employee ID" %}
                        </label>
                        <input type="text" name="signature_employee_id"
                               value="{{ employee.employee_id|default:'' }}"
                               class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-transparent transition"
                               placeholder="{% trans 'Employee ID' %}">
                    </div>
                </div>

                <!-- Notes -->
                <div>
                    <label class="block text-sm font-bold text-navy mb-2">
                        {% trans "Notes" %}
                    </label>
                    <textarea name="notes" rows="3"
                              class="w-full px-4 py-3 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-transparent transition resize-none"
                              placeholder="{% trans 'Any notes about this signature (optional)' %}"></textarea>
                </div>

                <!-- Actions -->
                <div class="flex gap-3 pt-4 border-t border-slate-100">
                    <a href="{% url 'accounts:simple_acknowledgements:admin_signatures' %}"
                       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-amber-500 to-amber-600 text-white rounded-xl font-semibold hover:from-amber-600 hover:to-amber-700 transition shadow-lg shadow-amber-200">
                        <i data-lucide="upload" class="w-5 h-5"></i>
                        {% trans "Upload Signed PDF" %}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function handleFileSelect(input) {
    const placeholder = document.getElementById('upload-placeholder');
    const preview = document.getElementById('upload-preview');
    const filename = document.getElementById('filename');

    if (input.files && input.files[0]) {
        placeholder.classList.add('hidden');
        preview.classList.remove('hidden');
        filename.textContent = input.files[0].name;
    }
}

// Drag and drop support
const dropZone = document.querySelector('.border-dashed');

dropZone.addEventListener('dragover', (e) => {
    e.preventDefault();
    dropZone.classList.add('border-amber-500', 'bg-amber-50');
});

dropZone.addEventListener('dragleave', () => {
    dropZone.classList.remove('border-amber-500', 'bg-amber-50');
});

dropZone.addEventListener('drop', (e) => {
    e.preventDefault();
    dropZone.classList.remove('border-amber-500', 'bg-amber-50');

    const files = e.dataTransfer.files;
    if (files.length > 0 && files[0].type === 'application/pdf') {
        document.getElementById('pdf-upload').files = files;
        handleFileSelect(document.getElementById('pdf-upload'));
    } else {
        alert('{% trans "Please upload a PDF file" %}');
    }
});

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