{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div class="flex flex-col gap-3 md:flex-row md:items-start md:justify-between">
    <div>
      <h1 class="text-2xl font-semibold text-slate-900">Create GST Invoice</h1>
      <p class="mt-1 text-sm text-slate-500">Prepare a professional tax invoice with SAC, GST breakup, place of supply and firm billing defaults.</p>
      <p class="mt-1 text-xs text-slate-400">Invoice will be tagged to active FY: <span class="font-semibold text-slate-600">{{ active_financial_year or 'Current FY' }}</span></p>
    </div>
    <a href="/billing/settings" class="af-btn af-btn-secondary">Billing Settings</a>
  </div>

  <form method="post" class="space-y-6 af-card">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />

    <section class="space-y-4">
      <div class="af-panel-header">
        <div>
          <h2 class="text-base font-semibold text-slate-900">Invoice Header</h2>
          <p class="text-xs text-slate-500">Client, date, GST treatment and billing period.</p>
        </div>
        <span class="af-badge af-badge-info">{{ settings.invoice_title or 'Tax Invoice' }}</span>
      </div>
      <div class="grid gap-4 md:grid-cols-3">
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Client</span>
          <select name="client_id" required class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
            <option value="">Select client</option>
            {% for client in clients %}
            <option value="{{ client.id }}">{{ client.client_code }} - {{ client.client_name }}</option>
            {% endfor %}
          </select>
        </label>
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Invoice Date</span>
          <input type="date" name="invoice_date" value="{{ today }}" required class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm" />
        </label>
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Due Date</span>
          <input type="date" name="due_date" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm" />
        </label>
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Billing Period From</span>
          <input type="date" name="billing_period_from" value="{{ default_billing_period_from or '' }}" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm" />
        </label>
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Billing Period To</span>
          <input type="date" name="billing_period_to" value="{{ default_billing_period_to or '' }}" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm" />
        </label>
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Tax Type</span>
          <select name="tax_type" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
            {% for tax_type in tax_types %}<option value="{{ tax_type }}" {% if settings.default_tax_type == tax_type %}selected{% endif %}>{{ tax_type }}</option>{% endfor %}
          </select>
        </label>
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Place of Supply</span>
          <input name="place_of_supply" placeholder="State / Union Territory" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm" />
        </label>
        <label class="block">
          <span class="text-sm font-medium text-slate-700">Client State Code</span>
          <input name="client_state_code" maxlength="2" placeholder="e.g. 33" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm" />
        </label>
        <label class="mt-7 inline-flex items-center gap-2 text-sm text-slate-700">
          <input type="checkbox" name="reverse_charge" value="yes" class="rounded border-slate-300" />
          Reverse charge applicable
        </label>
      </div>
    </section>

    <section class="space-y-3">
      <div class="af-panel-header">
        <div>
          <h2 class="text-base font-semibold text-slate-900">Invoice Lines</h2>
          <p class="text-xs text-slate-500">SAC defaults to billing settings if left blank. Blank description rows are ignored.</p>
        </div>
      </div>
      <div class="overflow-x-auto rounded-xl border border-slate-200">
        <table class="min-w-full text-sm">
          <thead class="bg-slate-50 text-left text-xs font-semibold uppercase text-slate-500">
            <tr>
              <th class="px-3 py-2">Service</th>
              <th class="px-3 py-2">Description</th>
              <th class="px-3 py-2">SAC</th>
              <th class="px-3 py-2">Qty</th>
              <th class="px-3 py-2">Rate</th>
              <th class="px-3 py-2">Discount</th>
              <th class="px-3 py-2">GST %</th>
            </tr>
          </thead>
          <tbody class="divide-y divide-slate-100">
            {% for i in range(1, 8) %}
            <tr>
              <td class="px-3 py-2">
                <select name="line_service_id" class="w-48 rounded-lg border border-slate-300 px-2 py-1.5">
                  <option value="">No service</option>
                  {% for service in services %}<option value="{{ service.id }}">{{ service.service_code }} - {{ service.service_name }}</option>{% endfor %}
                </select>
              </td>
              <td class="px-3 py-2"><input name="line_description" class="w-80 rounded-lg border border-slate-300 px-2 py-1.5" placeholder="Professional fees / service description" /></td>
              <td class="px-3 py-2"><input name="line_sac_code" value="{{ settings.default_sac_code or '' }}" class="w-24 rounded-lg border border-slate-300 px-2 py-1.5" /></td>
              <td class="px-3 py-2"><input name="line_quantity" value="1" class="w-20 rounded-lg border border-slate-300 px-2 py-1.5" /></td>
              <td class="px-3 py-2"><input name="line_rate" value="0" class="w-28 rounded-lg border border-slate-300 px-2 py-1.5" /></td>
              <td class="px-3 py-2"><input name="line_discount" value="0" class="w-28 rounded-lg border border-slate-300 px-2 py-1.5" /></td>
              <td class="px-3 py-2"><input name="line_gst_rate" value="{{ settings.default_gst_rate or 18 }}" class="w-20 rounded-lg border border-slate-300 px-2 py-1.5" /></td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </section>

    <div class="grid gap-4 md:grid-cols-2">
      <label class="block"><span class="text-sm font-medium text-slate-700">Declaration / Notes</span><textarea name="notes" rows="3" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{{ settings.declaration or '' }}</textarea></label>
      <label class="block"><span class="text-sm font-medium text-slate-700">Terms & Conditions</span><textarea name="terms" rows="3" class="mt-1 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{{ settings.terms or '' }}</textarea></label>
    </div>

    <div class="flex justify-end gap-3">
      <a href="/billing" class="af-btn af-btn-secondary">Cancel</a>
      <button class="af-btn af-btn-primary">Save Draft Invoice</button>
    </div>
  </form>
</div>
{% endblock %}
