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

{% block title %}{% trans "Doctor Rating Import 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;
    }
    .job-row {
        transition: all 0.2s ease;
        border-left: 3px solid transparent;
    }
    .job-row:hover {
        background: #f8fafc;
        border-left-color: #007bbd;
        transform: translateX(2px);
    }
</style>
{% endblock %}

{% block content %}
<div class="p-8">
    <!-- Header -->
    <div class="page-header-gradient">
        <div class="flex items-center justify-between">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="clock" class="w-5 h-5 text-white"></i>
                </div>
                <div>
                    <div class="flex items-center gap-2 text-sm text-white/80 mb-1">
                        <a href="{% url 'physicians:individual_ratings_list' %}" class="hover:text-white">{% trans "Physician Ratings" %}</a>
                        <i data-lucide="chevron-right" class="w-4 h-4"></i>
                        <span class="font-bold text-white">{% trans "Import History" %}</span>
                    </div>
                    <h1 class="text-2xl font-bold">{% trans "Import History" %}</h1>
                    <p class="text-white/80 text-sm">{% trans "Track doctor rating import jobs" %}</p>
                </div>
            </div>
            <div class="flex items-center gap-3">
                <a href="{% url 'physicians:doctor_rating_fetch' %}"
                   class="px-6 py-3 bg-white/20 text-white rounded-xl font-semibold hover:bg-white/30 transition flex items-center gap-2 border border-white/30">
                    <i data-lucide="cloud-download" class="w-5 h-5"></i>
                    {% trans "Fetch from HIS" %}
                </a>
                <a href="{% url 'physicians:doctor_rating_import' %}" 
                   class="px-6 py-3 bg-white text-[#005696] rounded-xl font-semibold hover:bg-white/90 transition flex items-center gap-2 shadow-lg">
                    <i data-lucide="plus-circle" class="w-5 h-5"></i>
                    {% trans "New Import" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Jobs Table -->
    <section class="section-card">
        <div class="section-header">
            <div class="section-icon bg-blue/10">
                <i data-lucide="list" class="w-5 h-5 text-blue"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">{% trans "Import Jobs" %}</h2>
        </div>
        {% if jobs %}
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-slate-50 border-b border-slate-200">
                    <tr>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Job Name" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Hospital" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Source" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Status" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Progress" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Results" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Created" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    {% for job in jobs %}
                    <tr class="job-row">
                        <td class="py-4 px-6">
                            <p class="text-sm font-semibold text-navy">{{ job.name|truncatechars:40 }}</p>
                        </td>
                        <td class="py-4 px-6">
                            <p class="text-sm text-slate">{{ job.hospital.name }}</p>
                        </td>
                        <td class="py-4 px-6">
                            {% if job.source == 'his_api' %}
                            <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-blue-100 text-blue-700">
                                <i data-lucide="database" class="w-3 h-3 mr-1"></i>
                                {% trans "HIS API" %}
                            </span>
                            {% else %}
                            <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-700">
                                <i data-lucide="upload" class="w-3 h-3 mr-1"></i>
                                {% trans "CSV Upload" %}
                            </span>
                            {% endif %}
                        </td>
                        <td class="py-4 px-6">
                            {% if job.status == 'pending' %}
                            <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-700">
                                <i data-lucide="clock" class="w-3 h-3 mr-1"></i>
                                {% trans "Pending" %}
                            </span>
                            {% elif job.status == 'processing' %}
                            <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-blue-100 text-blue-700">
                                <i data-lucide="loader-2" class="w-3 h-3 mr-1 animate-spin"></i>
                                {% trans "Processing" %}
                            </span>
                            {% elif job.status == 'completed' %}
                            <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-700">
                                <i data-lucide="check-circle" class="w-3 h-3 mr-1"></i>
                                {% trans "Completed" %}
                            </span>
                            {% elif job.status == 'failed' %}
                            <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-red-100 text-red-700">
                                <i data-lucide="x-circle" class="w-3 h-3 mr-1"></i>
                                {% trans "Failed" %}
                            </span>
                            {% elif job.status == 'partial' %}
                            <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-amber-100 text-amber-700">
                                <i data-lucide="alert-circle" class="w-3 h-3 mr-1"></i>
                                {% trans "Partial" %}
                            </span>
                            {% endif %}
                        </td>
                        <td class="py-4 px-6">
                            <div class="flex items-center gap-2">
                                <div class="flex-1 h-2 bg-slate-100 rounded-full overflow-hidden" style="min-width: 80px;">
                                    <div class="h-full {% if job.status == 'failed' %}bg-red-500{% elif job.status == 'completed' %}bg-green-500{% else %}bg-blue-500{% endif %} rounded-full transition-all"
                                         style="width: {{ job.progress_percentage }}%"></div>
                                </div>
                                <span class="text-xs font-semibold text-slate w-10">{{ job.progress_percentage }}%</span>
                            </div>
                        </td>
                        <td class="py-4 px-6">
                            {% if job.is_complete %}
                            <div class="flex items-center gap-3">
                                <span class="inline-flex items-center gap-1 text-xs font-semibold text-green-600">
                                    <i data-lucide="check" class="w-3 h-3"></i>
                                    {{ job.success_count }}
                                </span>
                                {% if job.failed_count > 0 %}
                                <span class="inline-flex items-center gap-1 text-xs font-semibold text-red-600">
                                    <i data-lucide="x" class="w-3 h-3"></i>
                                    {{ job.failed_count }}
                                </span>
                                {% endif %}
                            </div>
                            {% else %}
                            <span class="text-xs text-slate">{{ job.processed_count }} / {{ job.total_records }}</span>
                            {% endif %}
                        </td>
                        <td class="py-4 px-6">
                            <p class="text-sm text-slate">{{ job.created_at|date:"Y-m-d H:i" }}</p>
                        </td>
                        <td class="py-4 px-6">
                            <a href="{% url 'physicians:doctor_rating_job_status' job.id %}"
                               class="inline-flex items-center gap-1.5 px-3 py-1.5 border border-blue-200 text-blue rounded-lg text-xs font-semibold hover:bg-blue-50 transition">
                                <i data-lucide="eye" class="w-3.5 h-3.5"></i>
                                {% trans "View" %}
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <!-- Empty State -->
        <div class="py-16 text-center">
            <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-lg font-bold text-navy mb-2">{% trans "No Import Jobs" %}</h3>
            <p class="text-slate text-sm mb-6">{% trans "No import jobs found. Start by importing doctor ratings." %}</p>
            <a href="{% url 'physicians:doctor_rating_import' %}" 
               class="inline-flex items-center gap-2 px-6 py-3 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition shadow-lg shadow-navy/20">
                <i data-lucide="upload" class="w-5 h-5"></i>
                {% trans "Import Ratings" %}
            </a>
        </div>
        {% endif %}
    </section>
</div>
{% endblock %}

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