<?php
$leads = $leads ?? [];
$owners = $owners ?? [];
$territories = $territories ?? [];
$waiting = count($leads);
$withTerr = 0;
foreach ($leads as $l) {
    if (trim((string) ($l['territory_name'] ?? '')) !== '') {
        $withTerr++;
    }
}
$summaries = [
    ['Waiting', $waiting, 'bi-inbox-fill', '#ea580c', '#fff7ed'],
    ['With territory', $withTerr, 'bi-geo-alt-fill', '#2563eb', '#eff6ff'],
    ['No territory', $waiting - $withTerr, 'bi-question-circle-fill', '#7c3aed', '#f3e8ff'],
    ['Salespeople', count($owners), 'bi-people-fill', '#16a34a', '#f0fdf4'],
];
$methods = [
    ['manual', 'Pick a person', 'bi-person-check', 'Choose who gets these leads'],
    ['territory', 'By territory', 'bi-geo-alt', 'First salesperson in that region'],
    ['round_robin', 'Next in line', 'bi-arrow-repeat', 'Rotate through the sales team'],
];
?>
<div class="asg-page">
  <div class="mb-3">
    <h1 class="page-title">Lead assignment</h1>
    <p class="page-sub mb-0">Give new leads to the right salesperson — pick a person, use territory, or next in line.</p>
  </div>

  <div class="task-summary mb-3">
    <?php foreach ($summaries as $s): ?>
      <div class="task-summary-card" style="--task-accent: <?= e($s[3]) ?>; --task-tint: <?= e($s[4]) ?>">
        <div class="task-summary-icon"><i class="bi <?= e($s[2]) ?>"></i></div>
        <div>
          <div class="task-summary-n"><?= (int) $s[1] ?></div>
          <div class="task-summary-l"><?= e($s[0]) ?></div>
        </div>
      </div>
    <?php endforeach; ?>
  </div>

  <form class="ajax-form" action="<?= e(url('assignment')) ?>" method="post">
    <?= csrf_field() ?>
    <div class="asg-bar">
      <div class="asg-methods">
        <?php foreach ($methods as $i => $m): ?>
          <label class="asg-method">
            <input class="visually-hidden" type="radio" name="method" value="<?= e($m[0]) ?>" <?= $i === 0 ? 'checked' : '' ?>>
            <i class="bi <?= e($m[2]) ?>"></i>
            <span>
              <strong><?= e($m[1]) ?></strong>
              <em><?= e($m[3]) ?></em>
            </span>
          </label>
        <?php endforeach; ?>
      </div>
      <div class="asg-fields">
        <div class="asg-user" id="userPick">
          <label class="form-label">Salesperson</label>
          <select class="form-select" name="user_id">
            <option value="">Select</option>
            <?php foreach ($owners as $o): ?><option value="<?= (int) $o['id'] ?>"><?= e($o['first_name'] . ' ' . $o['last_name']) ?></option><?php endforeach; ?>
          </select>
        </div>
        <div class="asg-territory" id="territoryPick">
          <label class="form-label">Territory</label>
          <select class="form-select" name="territory_id">
            <option value="">Select</option>
            <?php foreach ($territories as $t): ?><option value="<?= (int) $t['id'] ?>"><?= e($t['name']) ?></option><?php endforeach; ?>
          </select>
        </div>
        <button class="btn btn-primary asg-submit" type="submit"><i class="bi bi-send-check"></i> Assign selected</button>
      </div>
    </div>

    <?php if (!$leads): ?>
      <div class="card-soft task-empty">
        <div class="task-empty-icon"><i class="bi bi-check2-circle"></i></div>
        <h2>No unassigned new leads</h2>
        <p>New leads without an owner will show up here for you to hand off.</p>
      </div>
    <?php else: ?>
      <div class="asg-toolbar">
        <label class="asg-select-all">
          <input type="checkbox" id="asgSelectAll">
          Select all
        </label>
        <span class="text-muted"><?= (int) $waiting ?> waiting</span>
      </div>
      <?php foreach ($leads as $l):
          $name = trim($l['first_name'] . ' ' . $l['last_name']);
          $company = trim((string) ($l['company'] ?? ''));
          $source = trim((string) ($l['source'] ?? ''));
          $territory = trim((string) ($l['territory_name'] ?? ''));
      ?>
        <label class="lead-card lead-tone-new asg-card">
          <input class="asg-check" type="checkbox" name="lead_ids[]" value="<?= (int) $l['id'] ?>">
          <div class="lead-avatar"><?= e(name_initials($name)) ?></div>
          <div class="flex-grow-1 min-w-0">
            <div class="lead-name"><?= e($name !== '' ? $name : 'Untitled lead') ?></div>
            <div class="lead-sub">
              <?php if ($company !== ''): ?><span><?= e($company) ?></span><?php endif; ?>
              <?php if ($source !== ''): ?><span class="lead-source"><?= e($source) ?></span><?php endif; ?>
              <?php if ($territory !== ''): ?><span><i class="bi bi-geo-alt"></i> <?= e($territory) ?></span><?php endif; ?>
            </div>
            <div class="lead-contact">
              <?php if (!empty($l['email'])): ?><span><i class="bi bi-envelope"></i> <?= e($l['email']) ?></span><?php endif; ?>
              <?php if (!empty($l['phone'])): ?><span><i class="bi bi-telephone"></i> <?= e($l['phone']) ?></span><?php endif; ?>
            </div>
          </div>
          <div class="task-meta">
            <span class="lead-status lead-status-new">Unassigned</span>
            <span class="lead-date"><?= e(dt_fmt($l['created_at'], 'M j')) ?></span>
          </div>
        </label>
      <?php endforeach; ?>
    <?php endif; ?>
  </form>
</div>
