<h1>Sales Requests</h1>

<% if (error) { %><div class="alert alert-danger"><%= error %></div><% } %>

<% if (forbidden) { %>
  <div class="alert alert-danger">Your role does not have permission to view sales requests.</div>
<% } else { %>

<p style="color:var(--muted);max-width:640px">
  Inbound requests from JurisLink LPMS's public "Contact Sales" form (docs/SPEC.md §17.3) —
  Enterprise prospects who submitted firm size/needs instead of self-serve signup. Configure a
  negotiated plan in Plan Builder, then have the firm's designated admin self-register (Tenants &gt;
  Firms &gt; Approve once it lands in Pending Review) — marking a request "Converted" here only
  records the outcome; it does not itself create the firm (no direct admin-console
  firm-creation endpoint exists yet, see docs/AUDIT.md).
</p>

<form method="get" action="/signup-requests" class="card" style="display:flex;gap:12px;align-items:flex-end">
  <div><label>Status</label>
    <select name="status" onchange="this.form.submit()">
      <option value="" <%= !query.status ? 'selected' : '' %>>All</option>
      <% for (const s of ['new','contacted','converted','declined']) { %>
      <option value="<%= s %>" <%= query.status === s ? 'selected' : '' %>><%= s %></option>
      <% } %>
    </select>
  </div>
</form>

<div class="card">
  <table>
    <thead><tr><th>Firm</th><th>Contact</th><th>Staff</th><th>Needs</th><th>Status</th><th>Submitted</th><th></th></tr></thead>
    <tbody>
      <% for (const r of requests) { %>
      <tr>
        <td><%= r.firm_name %></td>
        <td><%= r.contact_name %><br><span style="color:var(--muted);font-size:12px"><%= r.contact_email %><% if (r.contact_phone) { %> &middot; <%= r.contact_phone %><% } %></span></td>
        <td><%= r.staff_count_range || '—' %></td>
        <td style="max-width:260px;white-space:normal"><%= r.needs_notes || '—' %></td>
        <td><span class="badge <%= { new: 'badge-warn', contacted: 'badge-ok', converted: 'badge-ok', declined: 'badge-danger' }[r.status] %>"><%= r.status %></span></td>
        <td><%= new Date(r.created_at).toLocaleString() %></td>
        <td>
          <% if (r.status !== 'converted' && r.status !== 'declined') { %>
          <form method="post" action="/signup-requests/<%= r.id %>" style="display:flex;gap:4px">
            <% if (r.status === 'new') { %>
            <button class="btn" type="submit" name="status" value="contacted">Mark Contacted</button>
            <% } %>
            <button class="btn" type="submit" name="status" value="converted">Mark Converted</button>
            <button class="btn" type="submit" name="status" value="declined">Decline</button>
          </form>
          <% } else { %>
          <span style="color:var(--muted);font-size:12px">Handled<% if (r.handled_by_admin_email) { %> by <%= r.handled_by_admin_email %><% } %></span>
          <% } %>
        </td>
      </tr>
      <% } %>
      <% if (!requests.length) { %><tr><td colspan="7" style="color:var(--muted)">No sales requests match.</td></tr><% } %>
    </tbody>
  </table>
</div>

<% } %>
