{% 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">UDIN Register</h2>
      <p class="text-sm text-slate-500">Auto-generated from final document workflow. No separate manual register is required.</p>
    </div>
    <a href="/documents" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Engagement Documents</a>
  </div>

  <form method="get" action="/documents/udin-register" class="grid gap-3 rounded-2xl bg-white p-4 shadow-soft md:grid-cols-4">
    <div>
      <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Financial Year</label>
      <input type="text" name="financial_year" value="{{ financial_year or '' }}" placeholder="2026-27" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
    </div>
    <div>
      <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">UDIN Status</label>
      <select name="status" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
        <option value="">All</option>
        {% for st in ['pending','generated','not_required','cancelled'] %}
          <option value="{{ st }}" {% if selected_status == st %}selected{% endif %}>{{ st.replace('_',' ').title() }}</option>
        {% endfor %}
      </select>
    </div>
    <div>
      <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Search</label>
      <input type="text" name="q" value="{{ q or '' }}" placeholder="UDIN / title / code" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
    </div>
    <div class="flex items-end">
      <button class="w-full rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Filter</button>
    </div>
  </form>

  <section class="grid gap-4 md:grid-cols-4">
    {% set pending_count = rows|selectattr('udin_status','equalto','pending')|list|length %}
    {% set generated_count = rows|selectattr('udin_status','equalto','generated')|list|length %}
    {% set released_count = rows|selectattr('final_release_status','equalto','released')|list|length %}
    <div class="rounded-2xl bg-white p-5 shadow-soft"><div class="text-xs uppercase tracking-wide text-slate-500">Total Documents</div><div class="mt-2 text-2xl font-semibold text-slate-900">{{ rows|length }}</div></div>
    <div class="rounded-2xl bg-white p-5 shadow-soft"><div class="text-xs uppercase tracking-wide text-slate-500">UDIN Pending</div><div class="mt-2 text-2xl font-semibold text-red-600">{{ pending_count }}</div></div>
    <div class="rounded-2xl bg-white p-5 shadow-soft"><div class="text-xs uppercase tracking-wide text-slate-500">UDIN Generated</div><div class="mt-2 text-2xl font-semibold text-emerald-600">{{ generated_count }}</div></div>
    <div class="rounded-2xl bg-white p-5 shadow-soft"><div class="text-xs uppercase tracking-wide text-slate-500">Final Released</div><div class="mt-2 text-2xl font-semibold text-brand-700">{{ released_count }}</div></div>
  </section>

  <div class="overflow-hidden rounded-2xl bg-white shadow-soft">
    <table class="min-w-full divide-y divide-slate-200">
      <thead class="bg-slate-50">
        <tr>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Date / FY</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Client / Engagement</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Document</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">UDIN</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Release</th>
          <th class="px-4 py-3 text-right text-xs font-semibold uppercase tracking-wide text-slate-500">Action</th>
        </tr>
      </thead>
      <tbody class="divide-y divide-slate-100">
        {% for doc in rows %}
        <tr>
          <td class="px-4 py-3 text-sm text-slate-700"><div>{{ doc.udin_date or '-' }}</div><div class="text-xs text-slate-500">FY {{ doc.udin_financial_year or doc.financial_year }}</div></td>
          <td class="px-4 py-3 text-sm"><div class="font-medium text-slate-900">{{ doc.client.client_name if doc.client else '-' }}</div><div class="text-xs text-slate-500">{{ doc.engagement.catalogue.service_name if doc.engagement and doc.engagement.catalogue else 'Engagement #' ~ doc.engagement_id }}</div></td>
          <td class="px-4 py-3 text-sm"><div class="font-medium text-slate-900">{{ doc.title }}</div><div class="text-xs text-slate-500">{{ doc.document_code }} · {{ doc.document_type.replace('_',' ').title() }}</div></td>
          <td class="px-4 py-3 text-sm"><div><span class="rounded-full px-2 py-1 text-xs {{ 'bg-emerald-100 text-emerald-800' if doc.udin_status == 'generated' else ('bg-red-100 text-red-800' if doc.udin_status == 'pending' else 'bg-slate-100 text-slate-700') }}">{{ (doc.udin_status or 'not_required').replace('_',' ').title() }}</span></div>{% if doc.udin_number %}<div class="mt-1 font-mono text-xs">{{ doc.udin_number }}</div>{% endif %}</td>
          <td class="px-4 py-3 text-sm"><span class="rounded-full px-2 py-1 text-xs {{ 'bg-emerald-100 text-emerald-800' if doc.final_release_status == 'released' else 'bg-slate-100 text-slate-700' }}">{{ (doc.final_release_status or 'draft').replace('_',' ').title() }}</span>{% if doc.released_at_utc %}<div class="mt-1 text-xs text-slate-500">{{ doc.released_at_utc }}</div>{% endif %}</td>
          <td class="px-4 py-3 text-right text-sm"><a href="/documents/engagements/{{ doc.engagement_id }}" class="font-medium text-brand-700 hover:underline">Open</a>{% set latest = doc.versions[0] if doc.versions else None %}{% if latest %}<a href="/documents/{{ doc.id }}/download" class="ml-3 font-medium text-brand-700 hover:underline">Download</a>{% endif %}</td>
        </tr>
        {% else %}
        <tr><td colspan="6" class="px-4 py-8 text-center text-sm text-slate-500">No UDIN records found.</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% endblock %}
