{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div class="flex flex-wrap items-center justify-between gap-3">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">Import Preview - {{ preview.import_label }}</h2>
      <p class="text-sm text-slate-500">Review validation results. Rows with error will be skipped during commit.</p>
    </div>
    <a href="/employees/imports" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Back to Imports</a>
  </div>

  {% if errors %}<div class="rounded-xl border border-rose-200 bg-rose-50 p-3 text-sm text-rose-700">{{ errors|join(', ') }}</div>{% endif %}

  <div class="grid gap-4 md:grid-cols-4">
    <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft"><div class="text-xs uppercase text-slate-500">Total Rows</div><div class="text-2xl font-semibold">{{ preview.summary.total }}</div></div>
    <div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4"><div class="text-xs uppercase text-emerald-700">Valid</div><div class="text-2xl font-semibold text-emerald-800">{{ preview.summary.valid }}</div></div>
    <div class="rounded-2xl border border-amber-200 bg-amber-50 p-4"><div class="text-xs uppercase text-amber-700">Warnings</div><div class="text-2xl font-semibold text-amber-800">{{ preview.summary.warning }}</div></div>
    <div class="rounded-2xl border border-rose-200 bg-rose-50 p-4"><div class="text-xs uppercase text-rose-700">Errors</div><div class="text-2xl font-semibold text-rose-800">{{ preview.summary.error }}</div></div>
  </div>

  <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-soft">
    <table class="min-w-full divide-y divide-slate-200 text-sm">
      <thead class="bg-slate-50 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">
        <tr><th class="px-4 py-3">Excel Row</th><th class="px-4 py-3">Status</th><th class="px-4 py-3">Action</th><th class="px-4 py-3">Key Data</th><th class="px-4 py-3">Messages</th></tr>
      </thead>
      <tbody class="divide-y divide-slate-100">
        {% for row in preview.rows %}
        <tr class="hover:bg-slate-50">
          <td class="px-4 py-3 font-semibold">{{ row.row_no }}</td>
          <td class="px-4 py-3"><span class="rounded-full px-2 py-1 text-xs font-semibold {% if row.status == 'valid' %}bg-emerald-50 text-emerald-700{% elif row.status == 'warning' %}bg-amber-50 text-amber-700{% else %}bg-rose-50 text-rose-700{% endif %}">{{ row.status|title }}</span></td>
          <td class="px-4 py-3">{{ row.action|title }}</td>
          <td class="px-4 py-3 text-slate-600">
            {% if preview.import_type == 'employees' %}{{ row.data.employee_code }} - {{ row.data.full_name }}{% elif preview.import_type == 'leave_types' %}{{ row.data.code }} - {{ row.data.name }}{% elif preview.import_type == 'leave_balances' %}{{ row.data.employee_code }} / {{ row.data.leave_code }}{% else %}{{ row.data.employee_code }} from {{ row.data.effective_from }}{% endif %}
          </td>
          <td class="px-4 py-3 text-slate-600">{{ row.messages|join('; ') if row.messages else '-' }}</td>
        </tr>
        {% else %}<tr><td colspan="5" class="px-4 py-6 text-center text-slate-500">No rows found.</td></tr>{% endfor %}
      </tbody>
    </table>
  </div>

  <form method="post" action="/employees/imports/commit" class="flex items-center justify-end gap-3">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
    <a href="/employees/imports" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Cancel</a>
    <button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700" {% if preview.summary.total == 0 or (preview.summary.valid == 0 and preview.summary.warning == 0) %}disabled{% endif %}>Confirm Import</button>
  </form>
</div>
{% endblock %}
