{% 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">{{ client.client_name }} — Business Structure</h2>
    <p class="text-sm text-slate-500">Maintain Business Units, Client Branches and statutory registrations under one PAN/legal client.</p></div>
    <a href="/clients/{{ client.id }}" class="rounded-xl border border-slate-300 px-4 py-2 text-sm">Back to Client</a>
  </div>

  {% if can_edit %}
  <div class="grid gap-5 xl:grid-cols-3">
    <form method="post" action="/clients/{{ client.id }}/business-units" class="rounded-2xl bg-white p-5 shadow-soft space-y-3">
      <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
      <h3 class="font-semibold text-slate-900">Add Business Unit</h3>
      <input name="business_code" required placeholder="Business code" class="w-full rounded-xl border px-3 py-2 text-sm">
      <input name="business_name" required placeholder="Business unit name" class="w-full rounded-xl border px-3 py-2 text-sm">
      <input name="trade_name" placeholder="Trade name" class="w-full rounded-xl border px-3 py-2 text-sm">
      <input name="nature_of_business" placeholder="Nature of business" class="w-full rounded-xl border px-3 py-2 text-sm">
      <label class="flex gap-2 text-sm"><input type="checkbox" name="is_primary"> Primary Business Unit</label>
      <button class="rounded-xl bg-brand-600 px-4 py-2 text-sm text-white">Add Business Unit</button>
    </form>

    <form method="post" action="/clients/{{ client.id }}/branches" class="rounded-2xl bg-white p-5 shadow-soft space-y-3">
      <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
      <h3 class="font-semibold text-slate-900">Add Client Branch</h3>
      <select name="business_unit_id" required class="w-full rounded-xl border px-3 py-2 text-sm">
        <option value="">Select Business Unit</option>
        {% for row in businesses if row.is_active %}<option value="{{ row.id }}">{{ row.business_name }}</option>{% endfor %}
      </select>
      <input name="branch_code" required placeholder="Branch code" class="w-full rounded-xl border px-3 py-2 text-sm">
      <input name="branch_name" required placeholder="Client Branch name" class="w-full rounded-xl border px-3 py-2 text-sm">
      <select name="branch_type" class="w-full rounded-xl border px-3 py-2 text-sm">
        <option value="head_office">Head Office</option><option value="branch">Branch</option>
        <option value="warehouse">Warehouse</option><option value="unit">Unit</option>
      </select>
      <div class="grid grid-cols-3 gap-2"><input name="city" placeholder="City" class="rounded-xl border px-3 py-2 text-sm"><input name="state" placeholder="State" class="rounded-xl border px-3 py-2 text-sm"><input name="pincode" placeholder="Pincode" class="rounded-xl border px-3 py-2 text-sm"></div>
      <label class="flex gap-2 text-sm"><input type="checkbox" name="is_primary"> Primary Branch</label>
      <button class="rounded-xl bg-brand-600 px-4 py-2 text-sm text-white">Add Client Branch</button>
    </form>

    <form method="post" action="/clients/{{ client.id }}/registrations" class="rounded-2xl bg-white p-5 shadow-soft space-y-3">
      <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
      <h3 class="font-semibold text-slate-900">Add Registration</h3>
      <select name="business_unit_id" class="w-full rounded-xl border px-3 py-2 text-sm"><option value="">Client level / derive from Branch</option>{% for row in businesses if row.is_active %}<option value="{{ row.id }}">{{ row.business_name }}</option>{% endfor %}</select>
      <select name="client_branch_id" class="w-full rounded-xl border px-3 py-2 text-sm"><option value="">No Client Branch</option>{% for row in branches if row.is_active %}<option value="{{ row.id }}">{{ row.branch_name }}</option>{% endfor %}</select>
      <select name="registration_type_id" required class="w-full rounded-xl border px-3 py-2 text-sm">
        <option value="">Select Registration Type</option>
        {% for row in registration_types %}<option value="{{ row.id }}">{{ row.name }} ({{ row.code }})</option>{% endfor %}
      </select>
      <input name="registration_number" required placeholder="Registration number" class="w-full rounded-xl border px-3 py-2 text-sm">
      <input name="legal_name" placeholder="Legal name" class="w-full rounded-xl border px-3 py-2 text-sm">
      <input name="trade_name" placeholder="Trade / deductor unit name" class="w-full rounded-xl border px-3 py-2 text-sm">
      <input name="state" placeholder="State" class="w-full rounded-xl border px-3 py-2 text-sm">
      <button class="rounded-xl bg-brand-600 px-4 py-2 text-sm text-white">Add Registration</button>
    </form>
  </div>
  {% endif %}

  <section class="rounded-2xl bg-white shadow-soft overflow-hidden">
    <div class="border-b p-4 font-semibold">Business Units</div>
    <table class="min-w-full text-sm"><thead class="bg-slate-50"><tr><th class="p-3 text-left">Code</th><th class="p-3 text-left">Business Unit</th><th class="p-3 text-left">Trade Name</th><th class="p-3 text-left">Nature</th><th class="p-3">Status</th><th></th></tr></thead>
    <tbody>{% for row in businesses %}<tr class="border-t"><td class="p-3">{{ row.business_code }}</td><td class="p-3 font-medium">{{ row.business_name }}{% if row.is_primary %} <span class="text-xs text-brand-700">Primary</span>{% endif %}</td><td class="p-3">{{ row.trade_name or '-' }}</td><td class="p-3">{{ row.nature_of_business or '-' }}</td><td class="p-3 text-center">{{ 'Active' if row.is_active else 'Inactive' }}</td><td class="p-3">{% if can_edit %}<form method="post" action="/clients/{{ client.id }}/business-structure/business/{{ row.id }}/toggle"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><button class="text-xs text-brand-700">{{ 'Deactivate' if row.is_active else 'Activate' }}</button></form>{% endif %}</td></tr>{% else %}<tr><td colspan="6" class="p-6 text-center text-slate-500">No Business Units added.</td></tr>{% endfor %}</tbody></table>
  </section>

  <section class="rounded-2xl bg-white shadow-soft overflow-hidden">
    <div class="border-b p-4 font-semibold">Client Branches</div>
    <table class="min-w-full text-sm"><thead class="bg-slate-50"><tr><th class="p-3 text-left">Code</th><th class="p-3 text-left">Client Branch</th><th class="p-3 text-left">Type</th><th class="p-3 text-left">Location</th><th class="p-3">Status</th><th></th></tr></thead>
    <tbody>{% for row in branches %}<tr class="border-t"><td class="p-3">{{ row.branch_code }}</td><td class="p-3 font-medium">{{ row.branch_name }}{% if row.is_primary %} <span class="text-xs text-brand-700">Primary</span>{% endif %}</td><td class="p-3">{{ row.branch_type|replace('_',' ')|title }}</td><td class="p-3">{{ row.city or '' }}{% if row.city and row.state %}, {% endif %}{{ row.state or '-' }}</td><td class="p-3 text-center">{{ 'Active' if row.is_active else 'Inactive' }}</td><td class="p-3">{% if can_edit %}<form method="post" action="/clients/{{ client.id }}/business-structure/branch/{{ row.id }}/toggle"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><button class="text-xs text-brand-700">{{ 'Deactivate' if row.is_active else 'Activate' }}</button></form>{% endif %}</td></tr>{% else %}<tr><td colspan="6" class="p-6 text-center text-slate-500">No Client Branches added.</td></tr>{% endfor %}</tbody></table>
  </section>

  <section class="rounded-2xl bg-white shadow-soft overflow-hidden">
    <div class="border-b p-4 font-semibold">Registrations</div>
    <table class="min-w-full text-sm"><thead class="bg-slate-50"><tr><th class="p-3 text-left">Type</th><th class="p-3 text-left">Number</th><th class="p-3 text-left">Trade / Unit Name</th><th class="p-3 text-left">State</th><th class="p-3">Status</th><th></th></tr></thead>
    <tbody>{% for row in registrations %}<tr class="border-t"><td class="p-3">{{ registration_type_codes.get(row.id, "-") }}</td><td class="p-3 font-medium">{{ row.registration_number }}</td><td class="p-3">{{ row.trade_name or row.legal_name or '-' }}</td><td class="p-3">{{ row.state or '-' }}</td><td class="p-3 text-center">{{ row.status|replace('_',' ')|title }}</td><td class="p-3">{% if can_edit %}<form method="post" action="/clients/{{ client.id }}/business-structure/registration/{{ row.id }}/toggle"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><button class="text-xs text-brand-700">{{ 'Deactivate' if row.status in ['active','valid','registered'] else 'Activate' }}</button></form>{% endif %}</td></tr>{% else %}<tr><td colspan="6" class="p-6 text-center text-slate-500">No registrations added.</td></tr>{% endfor %}</tbody></table>
  </section>
</div>
{% endblock %}
