{% 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">{{ lead.lead_no }} - {{ lead.lead_name }}</h1><p class="text-sm text-slate-500">{{ lead.service_requested }} • {{ lead.status }}</p></div>
    <a href="/marketplace/leads" class="rounded-xl border px-4 py-2 text-sm">Back</a>
  </div>
  <div class="grid gap-5 lg:grid-cols-3">
    <div class="lg:col-span-2 rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
      <h2 class="font-semibold">Lead Details</h2>
      <dl class="mt-4 grid gap-3 text-sm md:grid-cols-2">
        <div><dt class="text-slate-500">Business</dt><dd class="font-medium">{{ lead.business_name or '-' }}</dd></div>
        <div><dt class="text-slate-500">Contact</dt><dd>{{ lead.mobile or '-' }} / {{ lead.email or '-' }}</dd></div>
        <div><dt class="text-slate-500">Location</dt><dd>{{ lead.city or '-' }}, {{ lead.state or '-' }}</dd></div>
        <div><dt class="text-slate-500">Category</dt><dd>{{ lead.service_category or '-' }}</dd></div>
        <div><dt class="text-slate-500">Priority</dt><dd>{{ lead.priority }}</dd></div>
        <div><dt class="text-slate-500">Estimated Value</dt><dd>{{ lead.estimated_value }}</dd></div>
        <div><dt class="text-slate-500">Assigned Audit Firm ID</dt><dd>{{ lead.assigned_tenant_id or '-' }}</dd></div>
        <div><dt class="text-slate-500">Assigned Partner User ID</dt><dd>{{ lead.assigned_partner_user_id or '-' }}</dd></div>
        <div class="md:col-span-2"><dt class="text-slate-500">Message</dt><dd class="whitespace-pre-line">{{ lead.message or '-' }}</dd></div>
      </dl>
    </div>
    <div class="space-y-5">
      {% if can_update %}
      <form method="post" action="/marketplace/leads/{{ lead.id }}/status" class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <h2 class="font-semibold">Update Status</h2>
        <select name="status" class="mt-3 w-full rounded-xl border px-3 py-2 text-sm">{% for s in lead_statuses %}<option value="{{ s }}" {% if lead.status==s %}selected{% endif %}>{{ s }}</option>{% endfor %}</select>
        <button class="mt-3 w-full rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white">Update</button>
      </form>
      {% endif %}
      {% if can_assign %}
      <form method="post" action="/marketplace/leads/{{ lead.id }}/assign" class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <h2 class="font-semibold">Assign Lead</h2>
        <label class="mt-3 block text-sm">Audit Firm<select name="tenant_id" required class="mt-1 w-full rounded-xl border px-3 py-2"><option value="">Select</option>{% for t in audit_firms %}<option value="{{ t.id }}" {% if lead.assigned_tenant_id==t.id %}selected{% endif %}>{{ t.name }}</option>{% endfor %}</select></label>
        <label class="mt-3 block text-sm">Branch<select name="branch_id" class="mt-1 w-full rounded-xl border px-3 py-2"><option value="0">Optional</option>{% for b in branches %}<option value="{{ b.id }}" {% if lead.assigned_branch_id==b.id %}selected{% endif %}>{{ b.name }}</option>{% endfor %}</select></label>
        <label class="mt-3 block text-sm">Partner/User<select name="partner_user_id" class="mt-1 w-full rounded-xl border px-3 py-2"><option value="0">Optional</option>{% for p in partners %}<option value="{{ p.id }}" {% if lead.assigned_partner_user_id==p.id %}selected{% endif %}>{{ p.full_name or p.email }}</option>{% endfor %}</select></label>
        <label class="mt-3 block text-sm">Notes<textarea name="notes" rows="2" class="mt-1 w-full rounded-xl border px-3 py-2"></textarea></label>
        <button class="mt-3 w-full rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white">Assign</button>
      </form>
      {% endif %}
      {% if can_convert and not lead.converted_client_id and lead.assigned_tenant_id %}
      <form method="post" action="/marketplace/leads/{{ lead.id }}/convert-client" class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <h2 class="font-semibold">Convert to Client</h2>
        <input type="hidden" name="tenant_id" value="{{ lead.assigned_tenant_id or '' }}">
        <label class="mt-3 block text-sm">Branch ID<input name="branch_id" value="{{ lead.assigned_branch_id or '' }}" required class="mt-1 w-full rounded-xl border px-3 py-2"></label>
        <label class="mt-3 block text-sm">Partner User ID<input name="partner_user_id" value="{{ lead.assigned_partner_user_id or '' }}" class="mt-1 w-full rounded-xl border px-3 py-2"></label>
        <label class="mt-3 block text-sm">Client Code<input name="client_code" value="LEAD-{{ '%05d'|format(lead.id) }}" class="mt-1 w-full rounded-xl border px-3 py-2"></label>
        <button class="mt-3 w-full rounded-xl border border-brand-200 bg-white px-4 py-2 text-sm font-semibold text-brand-700">Create Client</button>
      </form>
      {% elif can_convert and not lead.converted_client_id and not lead.assigned_tenant_id %}
      <div class="rounded-2xl border border-amber-200 bg-amber-50 p-5 text-sm text-amber-800">Assign this lead to an Audit Firm before converting it to a client.</div>
      {% elif lead.converted_client_id %}
      <div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-5 text-sm text-emerald-800">Converted to Client ID {{ lead.converted_client_id }}</div>
      {% endif %}
    </div>
  </div>
</div>
{% endblock %}
