{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div>
    <h2 class="text-xl font-semibold text-slate-900">Bulk Imports</h2>
    <p class="text-sm text-slate-500">Download Excel templates, fill data, and upload to configure services faster.</p>
  </div>

  <div class="grid gap-6 lg:grid-cols-2">
    {% if can_client_assignment %}
    <section class="rounded-2xl bg-white p-5 shadow-soft">
      <h3 class="text-sm font-semibold text-slate-900">Bulk assign services to clients</h3>
      <p class="mt-1 text-sm text-slate-500">For Firm Admin / Partner. Services must already be enabled for the active audit firm. Use expiry_date for renewal-before-expiry services like DSC renewal.</p>
      <a href="/services/bulk-imports/templates/engagement-assignments.xlsx" class="mt-4 inline-flex rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Download Template</a>
      <form method="post" action="/services/bulk-imports/engagement-assignments" enctype="multipart/form-data" class="mt-4 space-y-3">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <input type="file" name="file" accept=".xlsx" required class="block w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
        <label class="inline-flex items-center gap-2 text-sm text-slate-700"><input type="checkbox" name="update_existing" checked> Update existing subscriptions</label>
        <div><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Upload Assignments</button></div>
      </form>
    </section>
    {% endif %}

    {% if can_due_date_extensions %}
    <section class="rounded-2xl bg-white p-5 shadow-soft">
      <h3 class="text-sm font-semibold text-slate-900">Import due date extensions</h3>
      <p class="mt-1 text-sm text-slate-500">Imports government deadline extensions for the active audit firm and updates matching unlocked engagements.</p>
      <a href="/services/bulk-imports/templates/due-date-extensions.xlsx" class="mt-4 inline-flex rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Download Template</a>
      <form method="post" action="/services/bulk-imports/due-date-extensions" enctype="multipart/form-data" class="mt-4 space-y-3">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <input type="file" name="file" accept=".xlsx" required class="block w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
        <label class="inline-flex items-center gap-2 text-sm text-slate-700"><input type="checkbox" name="update_existing" checked> Update duplicate extension rows</label>
        <p class="text-xs text-slate-500">Multiple extensions are stored as separate history rows. Duplicate rows are detected by service, rule, FY/AY, period, extended date and notification reference.</p>
        <div><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Upload Extensions</button></div>
      </form>
    </section>
    {% endif %}

    {% if can_firm_tasks %}
    <section class="rounded-2xl bg-white p-5 shadow-soft">
      <h3 class="text-sm font-semibold text-slate-900">Import firm task templates</h3>
      <p class="mt-1 text-sm text-slate-500">For Firm Admin. Service must be enabled for active audit firm.</p>
      <a href="/services/bulk-imports/templates/firm-task-templates.xlsx" class="mt-4 inline-flex rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Download Template</a>
      <form method="post" action="/services/bulk-imports/firm-task-templates" enctype="multipart/form-data" class="mt-4 space-y-3">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <input type="file" name="file" accept=".xlsx" required class="block w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
        <label class="inline-flex items-center gap-2 text-sm text-slate-700"><input type="checkbox" name="update_existing" checked> Update existing sequence rows</label>
        <div><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Upload Firm Tasks</button></div>
      </form>
    </section>
    {% endif %}

    {% if can_system_import %}
    <section class="rounded-2xl bg-white p-5 shadow-soft">
      <h3 class="text-sm font-semibold text-slate-900">Import system service master</h3>
      <p class="mt-1 text-sm text-slate-500">System Admin only. Creates/updates service categories, catalogue services, and optional due date rules/extensions in the workbook.</p>
      <a href="/services/bulk-imports/templates/service-master.xlsx" class="mt-4 inline-flex rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Download Template</a>
      <form method="post" action="/services/bulk-imports/service-master" enctype="multipart/form-data" class="mt-4 space-y-3">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <input type="file" name="file" accept=".xlsx" required class="block w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
        <label class="inline-flex items-center gap-2 text-sm text-slate-700"><input type="checkbox" name="update_existing" checked> Update existing services/rules</label>
        <p class="text-xs text-slate-500">Template contains optional sheets: service_master, due_date_rules, due_date_extensions. Due date rules support renewal_based using renewal_days_before_expiry.</p>
        <div><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Upload Service Master</button></div>
      </form>
    </section>

    <section class="rounded-2xl bg-white p-5 shadow-soft">
      <h3 class="text-sm font-semibold text-slate-900">Import system default tasks</h3>
      <p class="mt-1 text-sm text-slate-500">System Admin only. Creates/updates default task templates by service code and sequence no.</p>
      <a href="/services/bulk-imports/templates/system-default-tasks.xlsx" class="mt-4 inline-flex rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Download Template</a>
      <form method="post" action="/services/bulk-imports/system-default-tasks" enctype="multipart/form-data" class="mt-4 space-y-3">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <input type="file" name="file" accept=".xlsx" required class="block w-full rounded-xl border border-slate-300 px-4 py-2 text-sm">
        <label class="inline-flex items-center gap-2 text-sm text-slate-700"><input type="checkbox" name="update_existing" checked> Update existing sequence rows</label>
        <div><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Upload Default Tasks</button></div>
      </form>
    </section>
    {% endif %}
  </div>
</div>
{% endblock %}
