<section class="project-form-shell">
  <div class="admin-header">
    <h1>Send Emails &middot; <%= stageLabel %></h1>
    <a class="button button-secondary" href="/admin/crm">&larr; Back to pipeline</a>
  </div>

  <p class="hint">
    Targeting is automatic - each prospect's sector decides which service line they're pitched (see <code>SECTOR_CATEGORY_MAP</code> in <code>crm.controller.js</code> to change the mapping), and how many touches they've already had decides which one they're due for.
    Hitting Send below queues emails as <strong>already approved</strong> - nothing needs individual review here, since the template wording itself was already reviewed in <a href="/admin/crm/templates">Templates</a>. They still go out gradually via Outlook, not all at once.
  </p>

  <% if (!eligibleCount) { %>
    <div class="dash-empty-state">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m22 6-10 7L2 6"></path><rect x="2" y="4" width="20" height="16" rx="2"></rect></svg>
      <p>Nothing eligible in this stage right now.</p>
    </div>
  <% } else { %>
    <div class="form-section">
      <h3 class="form-section-title">What this batch will send</h3>
      <table class="admin-table">
        <thead><tr><th>Category</th><th>Touch</th><th>Prospects</th><th>Template</th></tr></thead>
        <tbody>
          <% preview.forEach(function(e) { %>
            <tr<%= !e.templateId ? ' style="opacity:0.5;"' : '' %>>
              <td><%= e.category %></td>
              <td>Touch <%= e.touchNumber %></td>
              <td><%= e.count %></td>
              <td>
                <% if (e.templateId) { %>
                  <a href="/admin/crm/templates/<%= e.templateId %>/edit"><%= e.templateName %></a>
                <% } else { %>
                  No matching template yet - these will be skipped
                <% } %>
              </td>
            </tr>
          <% }) %>
        </tbody>
      </table>
      <% if (sendableCount < eligibleCount) { %>
        <p class="hint"><%= eligibleCount - sendableCount %> prospect(s) above have no matching template for their category/touch yet, so they'll be skipped this run - add the missing template(s) first if you want them included.</p>
      <% } %>
    </div>

    <% if (sendableCount) { %>
      <form method="POST" action="/admin/crm/stage/<%= stage %>/send">
        <div class="form-section">
          <label>
            How many to send now
            <input type="number" name="batchSize" value="<%= defaultBatchSize %>" min="1" max="<%= maxBatchSize %>" required>
            <span class="hint">Capped at <%= maxBatchSize %> per click. Never-contacted and longest-waiting prospects go first; click Send again later for the next batch of unsent ones.</span>
          </label>
        </div>
        <button type="submit" class="button button-primary">Send now</button>
      </form>
    <% } %>
  <% } %>
</section>
