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

{% block title %}{% trans "Review Import Summary" %} - PX360{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 mb-6">
    <div>
        <div class="flex items-center gap-2 mb-2">
            <a href="{% url 'surveys:his_patient_import' %}" class="text-blue hover:text-navy transition flex items-center gap-1">
                <i data-lucide="arrow-left" class="w-5 h-5"></i>
                <span class="text-sm font-medium">{% trans "Back to Import" %}</span>
            </a>
        </div>
        <h1 class="text-2xl font-bold text-navy flex items-center gap-3">
            <i data-lucide="clipboard-check" class="w-7 h-7 text-blue"></i>
            {% trans "Review Import Summary" %}
        </h1>
        <p class="text-slate mt-1 flex items-center gap-2">
            <i data-lucide="building-2" class="w-4 h-4"></i>
            {{ hospital.name }}
        </p>
    </div>
</div>

<!-- Errors (if any) -->
{% if errors %}
<div class="bg-red-50 border border-red-200 rounded-2xl p-6 mb-6 shadow-sm">
    <h3 class="text-sm font-bold text-red-800 mb-3 flex items-center gap-2">
        <i data-lucide="alert-triangle" class="w-5 h-5"></i>
        {% trans "Import Errors" %} ({{ errors|length }})
    </h3>
    <div class="max-h-40 overflow-y-auto space-y-1">
        {% for error in errors %}
        <p class="text-xs text-red-700 flex items-start gap-2">
            <span class="mt-1.5 w-1 h-1 bg-red-600 rounded-full flex-shrink-0"></span>
            {{ error }}
        </p>
        {% endfor %}
    </div>
</div>
{% endif %}

<form method="post" data-loading data-loading-text="{% trans 'Creating Patients...' %}">
    {% csrf_token %}
    
    <!-- Summary Cards -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
        <!-- Total Records -->
        <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate mb-1">{% trans "Total Records" %}</p>
                    <p class="text-3xl font-bold text-navy">{{ total_count|default:"0" }}</p>
                </div>
                <div class="w-12 h-12 bg-blue/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="database" class="w-6 h-6 text-blue"></i>
                </div>
            </div>
        </div>

        <!-- New Patients -->
        <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate mb-1">{% trans "New Patients" %}</p>
                    <p class="text-3xl font-bold text-green-600">{{ new_count|default:"0" }}</p>
                </div>
                <div class="w-12 h-12 bg-green/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="user-plus" class="w-6 h-6 text-green-600"></i>
                </div>
            </div>
            <p class="text-xs text-slate mt-2">{% trans "Will be created" %}</p>
        </div>

        <!-- Existing Patients -->
        <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate mb-1">{% trans "Existing Patients" %}</p>
                    <p class="text-3xl font-bold text-yellow-600">{{ existing_count|default:"0" }}</p>
                </div>
                <div class="w-12 h-12 bg-yellow/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="user-check" class="w-6 h-6 text-yellow-600"></i>
                </div>
            </div>
            <p class="text-xs text-slate mt-2">{% trans "Will be updated" %}</p>
        </div>

        <!-- With Mobile -->
        <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate mb-1">{% trans "With Mobile" %}</p>
                    <p class="text-3xl font-bold text-blue">{{ has_mobile|default:"0" }}</p>
                </div>
                <div class="w-12 h-12 bg-blue/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="phone" class="w-6 h-6 text-blue"></i>
                </div>
            </div>
            {% if missing_mobile > 0 %}
            <p class="text-xs text-red-500 mt-2">{{ missing_mobile }} {% trans "missing mobile" %}</p>
            {% else %}
            <p class="text-xs text-green-600 mt-2">{% trans "All have mobile" %}</p>
            {% endif %}
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-6">
        <!-- Visit Types Breakdown -->
        <div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
            <h3 class="text-lg font-bold text-navy mb-4 flex items-center gap-2">
                <i data-lucide="pie-chart" class="w-5 h-5 text-blue"></i>
                {% trans "Visit Types" %}
            </h3>
            <div class="space-y-3">
                {% for visit_type, count in visit_types %}
                <div class="flex items-center justify-between p-3 bg-slate-50 rounded-xl">
                    <div class="flex items-center gap-3">
                        {% if visit_type == "EMERGENCY" or visit_type == "Emergency" %}
                        <span class="w-3 h-3 bg-red-500 rounded-full"></span>
                        {% elif visit_type == "INPATIENT" or visit_type == "Inpatient" %}
                        <span class="w-3 h-3 bg-blue-500 rounded-full"></span>
                        {% elif visit_type == "OUTPATIENT" or visit_type == "Outpatient" %}
                        <span class="w-3 h-3 bg-green-500 rounded-full"></span>
                        {% else %}
                        <span class="w-3 h-3 bg-slate-400 rounded-full"></span>
                        {% endif %}
                        <span class="text-sm font-medium text-navy">{{ visit_type }}</span>
                    </div>
                    <span class="text-sm font-bold text-slate">{{ count }}</span>
                </div>
                {% empty %}
                <p class="text-sm text-slate text-center py-4">{% trans "No visit type data available" %}</p>
                {% endfor %}
            </div>
        </div>

        <!-- Sample Preview -->
        <div class="lg:col-span-2 bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-bold text-navy flex items-center gap-2">
                    <i data-lucide="eye" class="w-5 h-5 text-blue"></i>
                    {% trans "Sample Preview" %}
                </h3>
                <span class="text-xs text-slate bg-slate-100 px-3 py-1 rounded-full">
                    {% trans "Showing" %} {{ sample_count }} {% trans "of" %} {{ total_count }}
                </span>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-slate-50 border-b border-slate-200">
                        <tr class="text-xs font-bold text-slate uppercase">
                            <th class="px-4 py-3 text-left">{% trans "File Number" %}</th>
                            <th class="px-4 py-3 text-left">{% trans "Patient Name" %}</th>
                            <th class="px-4 py-3 text-left">{% trans "Visit Type" %}</th>
                            <th class="px-4 py-3 text-left">{% trans "Status" %}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        {% for patient in sample_patients %}
                        <tr class="hover:bg-light/30">
                            <td class="px-4 py-3 font-mono text-xs text-slate">{{ patient.file_number }}</td>
                            <td class="px-4 py-3">
                                <div class="font-medium text-navy">{{ patient.patient_name }}</div>
                            </td>
                            <td class="px-4 py-3">
                                {% if patient.visit_type == 'EMERGENCY' %}
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-bold bg-red-100 text-red-800">
                                    {{ patient.visit_type|default:"-" }}
                                </span>
                                {% elif patient.visit_type == 'INPATIENT' %}
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-bold bg-blue-100 text-blue-800">
                                    {{ patient.visit_type|default:"-" }}
                                </span>
                                {% elif patient.visit_type %}
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-bold bg-slate-100 text-slate-800">
                                    {{ patient.visit_type }}
                                </span>
                                {% else %}
                                <span class="text-slate-400">-</span>
                                {% endif %}
                            </td>
                            <td class="px-4 py-3">
                                {% if patient.exists %}
                                <span class="inline-flex items-center gap-1 text-xs font-bold text-yellow-600">
                                    <i data-lucide="user-check" class="w-3 h-3"></i>
                                    {% trans "Exists" %}
                                </span>
                                {% else %}
                                <span class="inline-flex items-center gap-1 text-xs font-bold text-green-600">
                                    <i data-lucide="user-plus" class="w-3 h-3"></i>
                                    {% trans "New" %}
                                </span>
                                {% endif %}
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="4" class="px-8 py-8 text-center text-slate">
                                <p class="text-sm">{% trans "No patients found in import data." %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% if total_count > sample_count %}
            <p class="text-xs text-slate mt-3 text-center">
                ... {% trans "and" %} {{ total_count|add:"-"|add:sample_count }} {% trans "more records" %}
            </p>
            {% endif %}
        </div>
    </div>

    <!-- Actions -->
    <div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
        <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
            <div>
                <p class="text-sm font-medium text-navy mb-1">{% trans "Ready to Import" %}</p>
                <p class="text-xs text-slate">
                    {% trans "All" %} {{ total_count }} {% trans "records will be processed. New patients will be created and existing ones updated." %}
                </p>
            </div>
            <div class="flex gap-3 w-full sm:w-auto">
                <button type="submit" name="action" value="cancel" class="flex-1 sm:flex-none px-6 py-3 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-light transition flex items-center justify-center gap-2">
                    <i data-lucide="x" class="w-5 h-5"></i>
                    {% trans "Cancel" %}
                </button>
                <button type="submit" name="action" value="create" class="flex-1 sm:flex-none px-6 py-3 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition flex items-center justify-center gap-2 shadow-sm">
                    <i data-lucide="user-plus" class="w-5 h-5"></i>
                    {% trans "Create Patients & Continue" %}
                </button>
            </div>
        </div>
    </div>
</form>

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