{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="mx-auto max-w-3xl rounded-2xl bg-white p-6 shadow-soft">
  <div class="flex items-center justify-between">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">Edit System Default Task</h2>
      <p class="text-sm text-slate-500">{{ service.service_code }} · {{ service.service_name }}</p>
    </div>
    <a href="/services/catalogue/{{ service.id }}/defaults" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Back</a>
  </div>

  <form method="post" class="mt-6 grid gap-5 md:grid-cols-2">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">

    <div>
      <label class="mb-2 block text-sm font-medium text-slate-700">Task Name</label>
      <input type="text" name="task_name" value="{{ task.task_name }}" required class="w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
    </div>

    <div>
      <label class="mb-2 block text-sm font-medium text-slate-700">Default Assignee Role</label>
      <select name="default_role_name" class="w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
        <option value="">Automatic / existing fallback</option>
        {% for role in task_execution_roles %}
        <option value="{{ role }}" {% if (task.default_role_name or '') == role %}selected{% endif %}>{{ role }}</option>
        {% endfor %}
      </select>
      <p class="mt-1 text-xs text-slate-500">This role receives the task first. If unavailable, another eligible role can be used.</p>
    </div>

    <div class="md:col-span-2 rounded-xl border border-blue-200 bg-blue-50 p-4">
      <div class="text-sm font-semibold text-blue-950">Eligible Roles</div>
      <p class="mt-1 text-xs text-blue-800">Select every role allowed to perform this task. For Manager or Partner, select both.</p>
      {% set eligible_text = task.eligible_role_names or task.default_role_name or '' %}
      <div class="mt-3 flex flex-wrap gap-5 text-sm text-slate-700">
        {% for role in task_execution_roles %}
        <label class="inline-flex items-center gap-2">
          <input type="checkbox" name="eligible_roles" value="{{ role }}" {% if role in eligible_text.split(',') or role == task.default_role_name %}checked{% endif %}>
          {{ role }}
        </label>
        {% endfor %}
      </div>
    </div>

    <div>
      <label class="mb-2 block text-sm font-medium text-slate-700">Sequence No</label>
      <input type="number" min="1" name="sequence_no" value="{{ task.sequence_no }}" required class="w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
    </div>

    <div class="flex items-center gap-6 pt-7 text-sm text-slate-700">
      <label class="inline-flex items-center gap-2">
        <input type="checkbox" name="is_mandatory" {% if task.is_mandatory %}checked{% endif %}>
        Mandatory
      </label>
      <label class="inline-flex items-center gap-2">
        <input type="checkbox" name="requires_review" {% if task.requires_review %}checked{% endif %}>
        Review
      </label>
      <label class="inline-flex items-center gap-2">
        <input type="checkbox" name="is_active" {% if task.is_active %}checked{% endif %}>
        Active
      </label>
    </div>

    <div class="md:col-span-2">
      <label class="mb-2 block text-sm font-medium text-slate-700">Description</label>
      <textarea name="description" rows="4" class="w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">{{ task.description or '' }}</textarea>
    </div>

    <div class="md:col-span-2 rounded-2xl border border-emerald-200 bg-emerald-50 p-4">
      <div class="text-sm font-semibold text-emerald-950">Structured Checklist Response</div>
      <p class="mt-1 text-xs text-emerald-800">Configure this task as an auditable checklist item. Normal tasks can keep Response Type as None.</p>
      <div class="mt-3 grid gap-4 md:grid-cols-2">
        <div>
          <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-emerald-800">Task Category</label>
          <input type="text" name="task_category" value="{{ task.task_category or '' }}" class="w-full rounded-xl border border-emerald-200 px-3 py-2 text-sm" placeholder="e.g. Accounting Verification">
        </div>
        <div>
          <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-emerald-800">Response Type</label>
          <select name="response_type" class="w-full rounded-xl border border-emerald-200 px-3 py-2 text-sm">
            {% for value, label in [('NONE','None'),('YES_NO_NA','Yes / No / NA'),('YES_NO','Yes / No'),('TEXT','Text'),('NUMBER','Number'),('DATE','Date')] %}
              <option value="{{ value }}" {% if (task.response_type or 'NONE') == value %}selected{% endif %}>{{ label }}</option>
            {% endfor %}
          </select>
        </div>
      </div>
      <div class="mt-4 grid gap-3 text-sm text-slate-700 md:grid-cols-3">
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="response_required" {% if task.response_required %}checked{% endif %}> Response required</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="evidence_required" {% if task.evidence_required %}checked{% endif %}> Evidence required</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="remarks_required_if_no" {% if task.remarks_required_if_no %}checked{% endif %}> Remarks required if No</label>
      </div>
    </div>

    <div class="md:col-span-2 rounded-2xl border border-indigo-200 bg-indigo-50 p-4">
      <div class="text-sm font-semibold text-indigo-950">AQMM / Quality Control Tagging</div>
      <p class="mt-1 text-xs text-indigo-800">Use these only for tasks that should become part of the assurance engagement quality checklist. Non-assurance engagements will continue using the normal task flow.</p>
      <div class="mt-3 grid gap-3 md:grid-cols-2 text-sm text-slate-700">
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="is_aqmm_task" {% if task.is_aqmm_task %}checked{% endif %}> Part of AQMM / quality checklist</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="aqmm_mandatory" {% if task.aqmm_mandatory %}checked{% endif %}> Mandatory for AQMM completion</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="aqmm_evidence_required" {% if task.aqmm_evidence_required %}checked{% endif %}> Evidence upload required</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="aqmm_manager_review_required" {% if task.aqmm_manager_review_required %}checked{% endif %}> Manager review required</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="aqmm_partner_review_required" {% if task.aqmm_partner_review_required %}checked{% endif %}> Partner review required</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="aqmm_review_partner_required" {% if task.aqmm_review_partner_required %}checked{% endif %}> Review partner review required</label>
        <label class="inline-flex items-center gap-2"><input type="checkbox" name="aqmm_blocks_final_release" {% if task.aqmm_blocks_final_release %}checked{% endif %}> Block final release until completed</label>
        <div>
          <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-indigo-800">AQMM Reference / Clause</label>
          <input type="text" name="aqmm_reference" value="{{ task.aqmm_reference or '' }}" class="w-full rounded-xl border border-indigo-200 px-3 py-2 text-sm" placeholder="e.g. Acceptance / Planning / Review">
        </div>
      </div>
    </div>

    <div class="md:col-span-2 rounded-xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-800">
      To deactivate this system default task, untick <strong>Active</strong> and save. Existing firm-copied tasks will not be automatically changed.
    </div>

    <div class="md:col-span-2 flex justify-end gap-3">
      <a href="/services/catalogue/{{ service.id }}/defaults" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Cancel</a>
      <button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Save Changes</button>
    </div>
  </form>
</div>
{% endblock %}