{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
    <div>
      <h1 class="text-2xl font-semibold text-slate-900">Generate Client Dashboard Platform Bills</h1>
      <p class="mt-1 text-sm text-slate-500">Generate draft platform invoices for client compliance dashboard subscriptions. Existing invoice for the same subscription and period will be skipped.</p>
    </div>
    <a href="/platform-billing/client-dashboard-subscriptions" class="rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-medium text-slate-700 shadow-sm">Back to Client Dashboard Billing</a>
  </div>

  {% if generated or skipped or errors %}
  <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
    <div class="text-sm text-slate-700">Generated: <b>{{ generated }}</b> · Skipped: <b>{{ skipped }}</b></div>
    {% if errors %}
    <ul class="mt-3 list-disc space-y-1 pl-5 text-sm text-amber-700">
      {% for error in errors %}<li>{{ error }}</li>{% endfor %}
    </ul>
    {% endif %}
    {% if invoices %}
    <div class="mt-3 flex flex-wrap gap-2">
      {% for invoice in invoices %}<a class="rounded-xl bg-slate-900 px-3 py-2 text-sm font-semibold text-white" href="/platform-billing/invoices/{{ invoice.id }}">{{ invoice.invoice_no }}</a>{% endfor %}
    </div>
    {% endif %}
  </div>
  {% endif %}

  <form method="post" class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
    <div class="grid gap-4 md:grid-cols-3">
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase text-slate-500">Period From</label>
        <input type="date" name="period_from" 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 text-slate-500">Period To</label>
        <input type="date" name="period_to" 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 text-slate-500">Invoice Date</label>
        <input type="date" name="invoice_date" 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 text-slate-500">Due Date</label>
        <input type="date" name="due_date" 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 text-slate-500">Tax Type</label>
        <select name="tax_type" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          {% for tax_type in tax_types %}<option value="{{ tax_type }}">{{ tax_type }}</option>{% endfor %}
        </select>
      </div>
      <label class="mt-6 inline-flex items-center gap-2 text-sm text-slate-700">
        <input type="checkbox" name="include_zero_usage_lines" class="rounded border-slate-300" /> Include zero usage lines
      </label>
    </div>

    <div class="mt-5 grid gap-4 md:grid-cols-3">
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase text-slate-500">Extra PAN Rate</label>
        <input name="pan_rate" value="0" 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 text-slate-500">Extra GSTIN Rate</label>
        <input name="gstin_rate" value="0" 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 text-slate-500">Per Compliance Module Rate</label>
        <input name="module_rate" value="0" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm" />
      </div>
    </div>

    <div class="mt-6 rounded-xl border border-slate-200">
      <div class="border-b border-slate-200 bg-slate-50 px-4 py-3 text-sm font-semibold text-slate-700">Select client dashboard subscriptions</div>
      <div class="max-h-[520px] overflow-auto divide-y divide-slate-100">
        {% for row in rows %}
        {% set sub = row.subscription %}
        <label class="flex cursor-pointer items-start gap-3 px-4 py-3 text-sm hover:bg-slate-50">
          <input type="checkbox" name="subscription_ids" value="{{ sub.id }}" class="mt-1 rounded border-slate-300" />
          <div class="flex-1">
            <div class="font-medium text-slate-900">{{ sub.account.display_name if sub.account else '-' }}</div>
            <div class="text-slate-500">{{ sub.subscription_code }} · {{ sub.plan.name if sub.plan else '-' }} · Base: {{ '%.2f'|format(sub.amount or 0) }} · {{ sub.billing_cycle }}</div>
            <div class="text-xs text-slate-400">PAN {{ row.usage.pan_units }} · GSTIN {{ row.usage.gstin_units }} · Modules {{ row.usage.module_count }}{% if row.usage.modules %} — {{ row.usage.modules|join(', ') }}{% endif %}</div>
          </div>
          <span class="rounded-full bg-slate-100 px-2 py-1 text-xs text-slate-600">{{ sub.status }}</span>
        </label>
        {% else %}
        <div class="px-4 py-6 text-center text-sm text-slate-500">No client dashboard subscriptions available.</div>
        {% endfor %}
      </div>
    </div>

    <div class="mt-5 flex justify-end">
      <button class="rounded-xl bg-brand-600 px-5 py-2.5 text-sm font-semibold text-white shadow-soft">Generate Draft Platform Invoices</button>
    </div>
  </form>
</div>
{% endblock %}
