{% 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">Task Documents</h2>
      <p class="text-sm text-slate-500">
        {{ engagement.client.client_name if engagement.client else '-' }} ·
        {{ engagement.catalogue.service_name if engagement.catalogue else '-' }} ·
        {{ task.task_name }} · FY {{ engagement.financial_year }}
      </p>
    </div>
    <div class="flex flex-wrap gap-2">
      <a href="{{ back_url }}" class="rounded-xl border border-brand-200 bg-brand-50 px-4 py-2 text-sm font-semibold text-brand-700 hover:bg-brand-100">Back to Engagement Task</a>
      <a href="/documents/engagements/{{ engagement.id }}" 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>
  </div>

  {% if request.query_params.get('uploaded') %}<div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4 text-sm text-emerald-800">Task document uploaded successfully.</div>{% endif %}
  {% if request.query_params.get('evidence_reviewed') %}<div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4 text-sm text-emerald-800">Evidence review status updated.</div>{% endif %}
  {% if request.query_params.get('udin_saved') %}<div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4 text-sm text-emerald-800">UDIN details saved successfully.</div>{% endif %}
  {% if request.query_params.get('released') %}<div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4 text-sm text-emerald-800">Document released as final.</div>{% endif %}
  {% if request.query_params.get('error') %}<div class="rounded-2xl border border-red-200 bg-red-50 p-4 text-sm text-red-800">Upload/action failed. Please verify file, requirement, UDIN and permission.</div>{% endif %}

  <section class="grid gap-4 md:grid-cols-3">
    <div class="rounded-2xl bg-white p-5 shadow-soft"><div class="text-xs uppercase tracking-wide text-slate-500">Mandatory Pending</div><div class="mt-2 text-2xl font-semibold text-slate-900">{{ requirement_status|selectattr('is_pending_mandatory')|list|length }}</div></div>
    <div class="rounded-2xl bg-white p-5 shadow-soft"><div class="text-xs uppercase tracking-wide text-slate-500">Requirements</div><div class="mt-2 text-2xl font-semibold text-slate-900">{{ requirements|length }}</div></div>
    <div class="rounded-2xl bg-white p-5 shadow-soft"><div class="text-xs uppercase tracking-wide text-slate-500">Uploaded Documents</div><div class="mt-2 text-2xl font-semibold text-slate-900">{{ documents|length }}</div></div>
  </section>

  {% if task.is_aqmm_task %}
  <section class="rounded-2xl border border-indigo-200 bg-indigo-50 p-5">
    <div class="flex flex-wrap items-start justify-between gap-3">
      <div>
        <h3 class="text-sm font-semibold text-indigo-900">AQMM Evidence Container</h3>
        <p class="mt-1 text-sm text-indigo-700">This task is marked as part of AQMM. Documents uploaded here, together with staff notes and review notes, become AQMM evidence.</p>
      </div>
      <span class="rounded-full bg-white px-3 py-1 text-xs font-medium text-indigo-700">{{ task.aqmm_status.replace('_',' ').title() }}</span>
    </div>
    <div class="mt-4 grid gap-3 text-xs md:grid-cols-4">
      <div class="rounded-xl bg-white p-3"><div class="text-slate-500">Evidence Required</div><div class="mt-1 font-semibold text-slate-900">{{ 'Yes' if task.aqmm_evidence_required else 'No' }}</div></div>
      <div class="rounded-xl bg-white p-3"><div class="text-slate-500">Evidence Status</div><div class="mt-1 font-semibold text-slate-900">{{ task.evidence_status.replace('_',' ').title() }}</div></div>
      <div class="rounded-xl bg-white p-3"><div class="text-slate-500">Review Status</div><div class="mt-1 font-semibold text-slate-900">{{ task.aqmm_review_status.replace('_',' ').title() }}</div></div>
      <div class="rounded-xl bg-white p-3"><div class="text-slate-500">Blocks Final Release</div><div class="mt-1 font-semibold text-slate-900">{{ 'Yes' if task.aqmm_blocks_final_release else 'No' }}</div></div>
    </div>
  </section>
  {% endif %}

  {% if can_upload %}
  <section class="rounded-2xl bg-white p-5 shadow-soft">
    <h3 class="text-sm font-semibold text-slate-900">Upload Task Document</h3>
    <form method="post" action="/documents/tasks/{{ task.id }}/upload" enctype="multipart/form-data" class="mt-4 grid gap-4 md:grid-cols-2">
      <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
      <input type="hidden" name="return_to" value="{{ return_to }}">
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Requirement</label>
        <select name="requirement_id" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          <option value="">General task document</option>
          {% for item in requirement_status %}
            {% set req = item.requirement %}
            <option value="{{ req.id }}">{{ req.document_name }}{% if req.is_mandatory %} (Mandatory){% endif %}</option>
          {% endfor %}
        </select>
      </div>
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Existing Document</label>
        <select name="existing_document_id" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          <option value="">Create new document</option>
          {% for doc in documents %}<option value="{{ doc.id }}">Add revision to {{ doc.title }} (v{{ doc.current_version_no }})</option>{% endfor %}
        </select>
      </div>
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Title</label>
        <input type="text" name="title" placeholder="Leave blank to use requirement name" 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">Document Type</label>
        <select name="document_type" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          {% for item in document_types %}<option value="{{ item }}">{{ item.replace('_',' ').title() }}</option>{% endfor %}
        </select>
      </div>
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">File</label>
        <input type="file" name="file" required class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      {% if task.is_aqmm_task %}
      <div class="md:col-span-2 rounded-xl border border-indigo-200 bg-indigo-50 p-3 text-sm text-indigo-900">
        <div class="font-semibold">AQMM Evidence Details</div>
        <p class="mt-1 text-xs text-indigo-800">This upload will be automatically marked as AQMM evidence because the task is part of AQMM.</p>
        <div class="mt-3 grid gap-3 md:grid-cols-2">
          <input type="text" name="evidence_type" placeholder="Evidence type, e.g. Working Paper / Confirmation" class="rounded-xl border border-indigo-200 px-3 py-2 text-sm">
          <input type="text" name="evidence_description" placeholder="Evidence description" class="rounded-xl border border-indigo-200 px-3 py-2 text-sm">
        </div>
      </div>
      {% endif %}
      <div class="md:col-span-2 rounded-xl border border-amber-200 bg-amber-50 p-3 text-sm text-amber-900">
        <label class="inline-flex items-center gap-2 font-semibold">
          <input type="checkbox" name="udin_required" value="1" class="rounded border-amber-300">
          UDIN required before final release
        </label>
        <p class="mt-1 text-xs text-amber-800">Use this for final certificates, reports or signed outputs prepared from this task.</p>
      </div>
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Revision Remarks</label>
        <input type="text" name="remarks" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div class="md:col-span-2">
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Description</label>
        <textarea name="description" rows="2" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm"></textarea>
      </div>
      <div class="md:col-span-2"><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Upload</button></div>
    </form>
  </section>
  {% endif %}

  <section class="overflow-hidden rounded-2xl bg-white shadow-soft">
    <div class="border-b border-slate-100 px-5 py-4"><h3 class="text-sm font-semibold text-slate-900">Requirement Status</h3></div>
    <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 text-slate-500">Requirement</th><th class="px-4 py-3 text-left text-xs font-semibold uppercase text-slate-500">Type</th><th class="px-4 py-3 text-left text-xs font-semibold uppercase text-slate-500">Status</th><th class="px-4 py-3 text-left text-xs font-semibold uppercase text-slate-500">Documents</th></tr></thead>
      <tbody class="divide-y divide-slate-100">
      {% for item in requirement_status %}
        {% set req = item.requirement %}
        <tr>
          <td class="px-4 py-3 text-sm"><div class="font-medium text-slate-900">{{ req.document_name }}</div>{% if req.instructions %}<div class="text-xs text-slate-500">{{ req.instructions }}</div>{% endif %}</td>
          <td class="px-4 py-3 text-sm text-slate-700">{{ req.document_type.replace('_',' ').title() }}{% if req.allowed_file_types %}<div class="text-xs text-slate-500">{{ req.allowed_file_types }}</div>{% endif %}</td>
          <td class="px-4 py-3 text-sm">{% if item.is_uploaded %}<span class="rounded-full bg-emerald-100 px-2 py-1 text-xs text-emerald-800">Uploaded</span>{% elif req.is_mandatory %}<span class="rounded-full bg-red-100 px-2 py-1 text-xs text-red-800">Pending Mandatory</span>{% else %}<span class="rounded-full bg-slate-100 px-2 py-1 text-xs text-slate-600">Optional Pending</span>{% endif %}</td>
          <td class="px-4 py-3 text-xs text-slate-600">{% for doc in item.documents %}<div><a href="/documents/{{ doc.id }}/download" class="text-brand-700 hover:underline">{{ doc.title }}</a> · v{{ doc.current_version_no }}</div>{% else %}-{% endfor %}</td>
        </tr>
      {% else %}<tr><td colspan="5" class="px-4 py-8 text-center text-sm text-slate-500">No document requirements configured for this task template.</td></tr>{% endfor %}
      </tbody>
    </table>
  </section>

  <section class="overflow-hidden rounded-2xl bg-white shadow-soft">
    <div class="border-b border-slate-100 px-5 py-4"><h3 class="text-sm font-semibold text-slate-900">All Task Documents</h3></div>
    <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 text-slate-500">Document</th><th class="px-4 py-3 text-left text-xs font-semibold uppercase text-slate-500">Latest Version</th><th class="px-4 py-3 text-left text-xs font-semibold uppercase text-slate-500">AQMM Evidence</th><th class="px-4 py-3 text-left text-xs font-semibold uppercase text-slate-500">UDIN / Release</th><th class="px-4 py-3 text-right text-xs font-semibold uppercase text-slate-500">Action</th></tr></thead>
      <tbody class="divide-y divide-slate-100">
      {% for doc in documents %}
        {% set latest = doc.versions[0] if doc.versions else None %}
        <tr>
          <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_type.replace('_',' ').title() }}{% if doc.document_requirement %} · {{ doc.document_requirement.document_name }}{% endif %}</div></td>
          <td class="px-4 py-3 text-xs text-slate-600">{% if latest %}v{{ latest.version_no }} · {{ latest.original_filename }}{% else %}-{% endif %}</td>
          <td class="px-4 py-3 text-xs text-slate-600 align-top">
            {% if doc.is_aqmm_evidence %}
              <div><span class="rounded-full bg-indigo-100 px-2 py-1 text-indigo-800">AQMM Evidence</span></div>
              <div class="mt-2">Status: <span class="font-semibold">{{ (doc.evidence_status or 'uploaded').replace('_',' ').title() }}</span></div>
              {% if doc.evidence_type %}<div class="mt-1">{{ doc.evidence_type }}</div>{% endif %}
              {% if doc.evidence_review_note %}<div class="mt-1 text-slate-500">{{ doc.evidence_review_note }}</div>{% endif %}
            {% elif task.is_aqmm_task %}
              <span class="rounded-full bg-slate-100 px-2 py-1 text-slate-600">Not marked</span>
            {% else %}-{% endif %}
          </td>
          <td class="px-4 py-3 text-xs text-slate-600">
            <div><span class="rounded-full px-2 py-1 {{ '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></div>
            <div class="mt-2">UDIN: <span class="font-semibold">{{ (doc.udin_status or 'not_required').replace('_',' ').title() }}</span></div>
            {% if doc.udin_number %}<div class="font-mono text-[11px]">{{ doc.udin_number }}</div>{% endif %}
            {% if doc.udin_required and not doc.udin_number %}<div class="mt-1 text-red-600">UDIN pending before final release.</div>{% endif %}
          </td>
          <td class="px-4 py-3 text-right text-sm align-top">
            {% if latest %}<a href="/documents/{{ doc.id }}/download" class="text-brand-700 hover:underline">Download</a>{% endif %}
            {% if task.is_aqmm_task and can_upload %}
            <details class="mt-3 text-left">
              <summary class="cursor-pointer text-sm font-semibold text-indigo-700">Evidence Review</summary>
              <form method="post" action="/documents/tasks/{{ task.id }}/documents/{{ doc.id }}/evidence-review" class="mt-3 grid gap-2 rounded-xl border border-indigo-100 bg-indigo-50 p-3">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <select name="evidence_status" class="rounded-lg border border-indigo-200 px-2 py-1 text-xs">
                  {% for status_code, status_label in [('uploaded','Uploaded'),('accepted','Accepted'),('rejected','Rejected / Insufficient'),('not_required','Not Required')] %}
                    <option value="{{ status_code }}" {% if doc.evidence_status == status_code %}selected{% endif %}>{{ status_label }}</option>
                  {% endfor %}
                </select>
                <input type="text" name="evidence_type" value="{{ doc.evidence_type or doc.document_type }}" placeholder="Evidence type" class="rounded-lg border border-indigo-200 px-2 py-1 text-xs">
                <input type="text" name="evidence_description" value="{{ doc.evidence_description or '' }}" placeholder="Evidence description" class="rounded-lg border border-indigo-200 px-2 py-1 text-xs">
                <input type="text" name="evidence_review_note" value="{{ doc.evidence_review_note or '' }}" placeholder="Evidence review note" class="rounded-lg border border-indigo-200 px-2 py-1 text-xs">
                <button class="rounded-lg bg-indigo-600 px-3 py-1.5 text-xs font-semibold text-white">Save Evidence Review</button>
              </form>
            </details>
            {% endif %}
            {% if 'udin.manage' in current_user_permissions %}
            <details class="mt-3 text-left">
              <summary class="cursor-pointer text-sm font-semibold text-brand-700">UDIN / Release</summary>
              <div class="mt-3 space-y-3 rounded-xl border border-slate-200 bg-slate-50 p-3">
                <form method="post" action="/documents/{{ doc.id }}/udin" class="grid gap-2">
                  <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                  <input type="hidden" name="redirect_to" value="/documents/tasks/{{ task.id }}">
                  <label class="inline-flex items-center gap-2 text-xs font-semibold text-slate-700"><input type="checkbox" name="udin_required" value="1" {% if doc.udin_required %}checked{% endif %}> UDIN required</label>
                  <input type="text" name="udin_number" value="{{ doc.udin_number or '' }}" placeholder="UDIN number" class="rounded-lg border border-slate-300 px-2 py-1 text-xs">
                  <div class="grid grid-cols-2 gap-2">
                    <input type="date" name="udin_date" value="{{ doc.udin_date or '' }}" class="rounded-lg border border-slate-300 px-2 py-1 text-xs">
                    <input type="date" name="udin_document_date" value="{{ doc.udin_document_date or '' }}" class="rounded-lg border border-slate-300 px-2 py-1 text-xs">
                  </div>
                  <input type="text" name="udin_document_type" value="{{ doc.udin_document_type or doc.document_type.replace('_',' ').title() }}" placeholder="Document type" class="rounded-lg border border-slate-300 px-2 py-1 text-xs">
                  <div class="grid grid-cols-2 gap-2"><input type="text" name="udin_financial_year" value="{{ doc.udin_financial_year or doc.financial_year }}" class="rounded-lg border border-slate-300 px-2 py-1 text-xs"><input type="text" name="udin_amount" value="{{ doc.udin_amount or '' }}" placeholder="Amount" class="rounded-lg border border-slate-300 px-2 py-1 text-xs"></div>
                  <input type="text" name="udin_notes" value="{{ doc.udin_notes or '' }}" placeholder="UDIN notes" class="rounded-lg border border-slate-300 px-2 py-1 text-xs">
                  <button class="rounded-lg bg-slate-800 px-3 py-1.5 text-xs font-semibold text-white">Save UDIN</button>
                </form>
                {% if doc.final_release_status != 'released' %}
                <form method="post" action="/documents/{{ doc.id }}/final-release" onsubmit="return confirm('Release this document as final?');" class="grid gap-2">
                  <input type="hidden" name="csrf_token" value="{{ csrf_token }}"><input type="hidden" name="redirect_to" value="/documents/tasks/{{ task.id }}">
                  <input type="text" name="release_notes" placeholder="Release notes" class="rounded-lg border border-slate-300 px-2 py-1 text-xs">
                  <button class="rounded-lg bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white">Release Final</button>
                </form>
                {% endif %}
              </div>
            </details>
            {% endif %}
          </td>
        </tr>
      {% else %}<tr><td colspan="5" class="px-4 py-8 text-center text-sm text-slate-500">No task documents uploaded yet.</td></tr>{% endfor %}
      </tbody>
    </table>
  </section>
</div>
{% endblock %}
