<?php
$counts = $counts ?? ['active' => 0, 'inactive' => 0, 'all' => 0];
$q = $q ?? '';
$status = $status ?? 'active';
$label = $label ?? account_label();
$summaries = [
    ['Active', (int) $counts['active'], 'bi-buildings', '#16a34a', '#f0fdf4', ''],
    ['Inactive', (int) $counts['inactive'], 'bi-buildings', '#dc2626', '#fef2f2', 'is-inactive'],
];
?>
<div class="account-page">
  <div class="mb-3">
    <h1 class="page-title"><?= e($label) ?></h1>
    <p class="page-sub mb-0"><?= (int) $total ?> in this view · open a card for people, deals, and activity</p>
  </div>

  <div class="task-summary is-pair mb-3">
    <?php foreach ($summaries as $s): ?>
      <div class="task-summary-card <?= e($s[5]) ?>" 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="task-filters is-compact card-soft p-3 mb-3" method="get">
    <div class="task-filter-search">
      <label class="form-label" for="account-q">Search</label>
      <input class="form-control" id="account-q" name="q" value="<?= e($q) ?>" placeholder="Search name, email, phone" autocomplete="off">
    </div>
    <div>
      <label class="form-label" for="account-status">Status</label>
      <select class="form-select" id="account-status" name="status">
        <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
        <option value="inactive" <?= $status === 'inactive' ? 'selected' : '' ?>>Inactive</option>
      </select>
    </div>
    <div class="task-filter-go">
      <button class="btn btn-primary" type="submit"><i class="bi bi-search"></i> Search</button>
    </div>
  </form>

  <?php if (!$rows): ?>
    <div class="card-soft task-empty">
      <div class="task-empty-icon"><i class="bi bi-buildings"></i></div>
      <h2>No accounts here</h2>
      <p>Use <strong>Add New → Account</strong> to add a company.</p>
    </div>
  <?php else: ?>
    <?php foreach ($rows as $r):
      $st = (string) ($r['status'] ?? 'active');
      $due = due_label($r['next_action_at'] ?? null, 'open');
      $tone = $st === 'inactive' ? 'inactive' : match ($due['class'] ?? '') {
          'due-overdue' => 'due',
          'due-today' => 'today',
          default => 'active',
      };
      $owner = trim((string) ($r['owner_name'] ?? ''));
      $territory = trim((string) ($r['territory_name'] ?? ''));
      $industryName = trim((string) ($r['industry'] ?? ''));
      $next = trim((string) ($r['next_action'] ?? ''));
    ?>
      <a class="account-card account-tone-<?= e($tone) ?>" href="<?= e(url('accounts/' . $r['id'])) ?>">
        <div class="account-avatar"><?= e(name_initials($r['name'] ?? '')) ?></div>
        <div class="flex-grow-1 min-w-0">
          <div class="lead-name"><?= e($r['name']) ?></div>
          <div class="lead-sub">
            <?php if ($territory !== ''): ?><span><?= e($territory) ?></span><?php endif; ?>
            <?php if ($industryName !== ''): ?><span class="lead-source"><?= e($industryName) ?></span><?php endif; ?>
            <?php if ($next !== ''): ?><span><?= e($next) ?></span><?php endif; ?>
          </div>
          <div class="lead-contact">
            <?php if (!empty($r['email'])): ?><span><i class="bi bi-envelope"></i> <?= e($r['email']) ?></span><?php endif; ?>
            <?php if (!empty($r['phone'])): ?><span><i class="bi bi-telephone"></i> <?= e($r['phone']) ?></span><?php endif; ?>
            <?php if (!empty($r['city'])): ?><span><i class="bi bi-geo-alt"></i> <?= e($r['city']) ?><?= !empty($r['state']) ? ', ' . e($r['state']) : '' ?></span><?php endif; ?>
          </div>
        </div>
        <div class="task-meta">
          <?php if ($owner !== ''): ?>
            <span class="task-owner-chip" title="<?= e($owner) ?>">
              <span class="task-avatar"><?= e(name_initials($owner)) ?></span>
              <span class="task-owner text-truncate"><?= e($owner) ?></span>
            </span>
          <?php endif; ?>
          <?php if (!empty($due['text'])): ?>
            <span class="due-pill <?= e($due['class']) ?>"><?= e($due['text']) ?></span>
          <?php endif; ?>
          <span class="lead-status account-status-<?= e($st) ?>"><?= e($st === 'active' ? 'Active' : 'Inactive') ?></span>
        </div>
      </a>
    <?php endforeach; ?>
  <?php endif; ?>

  <?php \App\Core\View::partial('pager', compact('page', 'pages')); ?>
</div>
