{% 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="/services/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</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('error') %}<div class="rounded-2xl border border-red-200 bg-red-50 p-4 text-sm text-red-800">Upload failed. Please verify file, requirement 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 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 }}">
      <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>
      <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="4" 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-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-right text-sm">{% if latest %}<a href="/documents/{{ doc.id }}/download" class="text-brand-700 hover:underline">Download</a>{% endif %}</td></tr>
      {% else %}<tr><td colspan="3" 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 %}
