{% extends "ui/templates/base/layout.html" %}
{% block content %}
  <div class="flex flex-wrap items-center justify-between gap-3">
    <div>
      <h1 class="text-2xl font-semibold">Financial Years</h1>
      <p class="mt-1 text-sm text-slate-600">Manage active financial year context, year locking and assessment year mapping.</p>
    </div>
    {% if can_manage_fy %}
    <a href="/system-settings/financial-years/new?tenant_id={{ selected_tenant_id }}" class="rounded-xl bg-slate-900 px-4 py-2 text-sm font-medium text-white">Add Financial Year</a>
    {% endif %}
  </div>

  {% if tenants|length > 1 %}
  <form class="mt-5 rounded-2xl border bg-white p-4" method="get">
    <label class="grid max-w-md gap-1">
      <span class="text-sm font-medium text-slate-600">Audit Firm</span>
      <select name="tenant_id" onchange="this.form.submit()" class="rounded-xl border px-3 py-2 text-sm">
        {% for tenant in tenants %}
        <option value="{{ tenant.id }}" {% if tenant.id == selected_tenant_id %}selected{% endif %}>{{ tenant.name }} ({{ tenant.code }})</option>
        {% endfor %}
      </select>
    </label>
  </form>
  {% endif %}

  <div class="mt-5 overflow-hidden rounded-2xl border bg-white">
    <table class="w-full text-sm">
      <thead class="bg-slate-50 text-slate-600">
        <tr>
          <th class="p-3 text-left">Financial Year</th>
          <th class="p-3 text-left">Assessment Year</th>
          <th class="p-3 text-left">Period</th>
          <th class="p-3 text-left">Current</th>
          <th class="p-3 text-left">Locked</th>
          <th class="p-3 text-right">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for fy in financial_years %}
        <tr class="border-t">
          <td class="p-3 font-medium text-slate-900">{{ fy.year_code }}</td>
          <td class="p-3">{{ fy.assessment_year }}</td>
          <td class="p-3">{{ fy.start_date }} to {{ fy.end_date }}</td>
          <td class="p-3">
            {% if fy.is_current %}<span class="rounded-full bg-green-50 px-2 py-1 text-xs font-medium text-green-700">Current</span>{% else %}<span class="text-slate-400">No</span>{% endif %}
          </td>
          <td class="p-3">
            {% if fy.is_locked %}<span class="rounded-full bg-amber-50 px-2 py-1 text-xs font-medium text-amber-700">Locked</span>{% else %}<span class="rounded-full bg-slate-100 px-2 py-1 text-xs text-slate-600">Open</span>{% endif %}
          </td>
          <td class="p-3">
            <div class="flex flex-wrap justify-end gap-2">
              <a href="/system-settings/context/financial-year/{{ fy.year_code }}" class="rounded-lg border px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50">Use</a>
              <a href="/system-settings/financial-years/{{ fy.id }}/backup" class="rounded-lg border px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50">Backup</a>
              {% if can_manage_fy and not fy.is_locked %}
              <a href="/system-settings/financial-years/{{ fy.id }}/edit" class="rounded-lg border px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50">Edit</a>
              {% endif %}
              {% if can_manage_fy and not fy.is_current %}
              <form method="post" action="/system-settings/financial-years/{{ fy.id }}/make-current">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
                <button class="rounded-lg border px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50" type="submit">Make Current</button>
              </form>
              {% endif %}
              {% if can_manage_fy and not fy.is_locked %}
              <form method="post" action="/system-settings/financial-years/{{ fy.id }}/lock">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
                <button class="rounded-lg border border-amber-200 px-3 py-1.5 text-xs font-medium text-amber-700 hover:bg-amber-50" type="submit">Lock</button>
              </form>
              {% elif can_manage_fy and fy.is_locked %}
              <form method="post" action="/system-settings/financial-years/{{ fy.id }}/unlock">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
                <button class="rounded-lg border border-slate-300 px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50" type="submit">Unlock</button>
              </form>
              {% endif %}
            </div>
          </td>
        </tr>
        {% else %}
        <tr><td colspan="6" class="p-6 text-center text-slate-500">No financial years created yet.</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
{% endblock %}
