{% extends 'layouts/base.html' %}
{% load i18n %}
{% load static %}

{% block title %}{% trans "Bulk Survey Jobs" %} - 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 %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="layers" class="w-6 h-6 text-white"></i>
                </div>
                {% trans "Bulk Survey Jobs" %}
            </h1>
            <p class="text-white/80 mt-2">{% trans "Track the status of bulk survey sending operations" %}</p>
        </div>
        <a href="{% url 'surveys:his_patient_import' %}" class="px-4 py-2.5 bg-white text-[#005696] rounded-xl font-semibold hover:bg-white/90 transition flex items-center gap-2 shadow-lg">
            <i data-lucide="upload" class="w-5 h-5"></i> {% trans "Import Patients" %}
        </a>
    </div>
</div>

<!-- Jobs Table Section Card -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon bg-blue-100">
            <i data-lucide="briefcase" class="w-5 h-5 text-blue-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Job List" %}</h3>
    </div>
    
    {% if jobs %}
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead class="bg-slate-50 border-b border-slate-100">
                <tr class="text-xs font-bold text-slate uppercase tracking-wider">
                    <th class="px-6 py-4 text-left">{% trans "Job" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Hospital" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Progress" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Success" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Failed" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Status" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Created" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                {% for job in jobs %}
                <tr class="hover:bg-light/30 transition">
                    <td class="px-6 py-4">
                        <div class="font-semibold text-navy">{{ job.name|truncatechars:40 }}</div>
                        <div class="text-xs text-slate">{{ job.survey_template.name|truncatechars:30 }}</div>
                    </td>
                    <td class="px-6 py-4">
                        <span class="text-sm text-slate">{{ job.hospital.name }}</span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        <div class="flex items-center justify-center gap-2">
                            <div class="w-16 bg-slate-200 rounded-full h-1.5">
                                <div class="bg-blue h-1.5 rounded-full" style="width: {{ job.progress_percentage }}%"></div>
                            </div>
                            <span class="text-xs font-medium">{{ job.progress_percentage }}%</span>
                        </div>
                    </td>
                    <td class="px-6 py-4 text-center">
                        <span class="text-green-600 font-bold">{{ job.success_count }}</span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        {% if job.failed_count > 0 %}
                        <span class="text-red-600 font-bold">{{ job.failed_count }}</span>
                        {% else %}
                        <span class="text-slate">-</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold
                            {% if job.status == 'completed' %}bg-green-100 text-green-700
                            {% elif job.status == 'failed' %}bg-red-100 text-red-700
                            {% elif job.status == 'processing' %}bg-blue-100 text-blue-700
                            {% elif job.status == 'partial' %}bg-yellow-100 text-yellow-700
                            {% else %}bg-slate-100 text-slate-700{% endif %}">
                            {{ job.get_status_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-xs text-slate">{{ job.created_at|date:"Y-m-d H:i" }}</td>
                    <td class="px-6 py-4 text-center">
                        <a href="{% url 'surveys:bulk_job_status' job.id %}" class="p-2 text-navy hover:bg-light rounded-lg transition inline-flex items-center" title="{% trans 'View details' %}">
                            <i data-lucide="eye" class="w-5 h-5"></i>
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <!-- Empty State -->
    <div class="text-center py-16">
        <div class="inline-flex items-center justify-center w-20 h-20 bg-slate-100 rounded-full mb-4">
            <i data-lucide="inbox" class="w-10 h-10 text-slate-400"></i>
        </div>
        <h3 class="text-xl font-bold text-navy mb-2">{% trans "No Jobs Found" %}</h3>
        <p class="text-slate mb-6">{% trans "No bulk survey jobs have been created yet." %}</p>
        <a href="{% url 'surveys:his_patient_import' %}" class="px-4 py-2.5 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition inline-flex items-center gap-2">
            <i data-lucide="upload" class="w-5 h-5"></i> {% trans "Import Patients" %}
        </a>
    </div>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
    lucide.createIcons();
</script>
{% endblock %}
