<?php
$baseQuery = array_filter([
    'q' => $filters['search'], 'status' => $filters['status'], 'priority' => $filters['priority'], 'queue' => $filters['queue'],
], fn($v) => $v !== '' && $v !== null);
$listUrl = url(\App\Core\Request::path()) . '?' . http_build_query($baseQuery + ['view' => 'list']);
$boardUrl = url(\App\Core\Request::path()) . '?' . http_build_query($baseQuery + ['view' => 'board']);
?>
<div class="toolbar">
  <form class="filter-bar" method="get">
    <input type="hidden" name="view" value="<?= e($view) ?>">
    <input type="text" name="q" placeholder="Search ticket #, subject, customer…" value="<?= e($filters['search']) ?>">
    <select name="status" onchange="this.form.submit()">
      <option value="">All statuses</option>
      <?php foreach ($statuses as $s) : ?>
        <option value="<?= e($s) ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>><?= status_label($s) ?></option>
      <?php endforeach; ?>
    </select>
    <select name="priority" onchange="this.form.submit()">
      <option value="">All priorities</option>
      <?php foreach ($priorities as $p) : ?>
        <option value="<?= $p['id'] ?>" <?= (string)$filters['priority'] === (string)$p['id'] ? 'selected' : '' ?>><?= e($p['code']) ?> — <?= e($p['name']) ?></option>
      <?php endforeach; ?>
    </select>
    <select name="queue" onchange="this.form.submit()">
      <option value="">All queues</option>
      <?php foreach ($queues as $q) : ?>
        <option value="<?= $q['id'] ?>" <?= (string)$filters['queue'] === (string)$q['id'] ? 'selected' : '' ?>><?= e($q['name']) ?></option>
      <?php endforeach; ?>
    </select>
    <button class="btn btn-sm" type="submit">Filter</button>
    <?php if ($filters['status'] || $filters['priority'] || $filters['queue'] || $filters['search']) : ?>
      <a href="<?= $view === 'board' ? $boardUrl : $listUrl ?>" class="btn btn-sm btn-ghost">Clear</a>
    <?php endif; ?>
  </form>
  <div class="flex gap-2">
    <div class="pill-tabs">
      <a href="<?= $listUrl ?>" class="pill-tab <?= $view === 'list' ? 'active' : '' ?>">☰ List</a>
      <a href="<?= $boardUrl ?>" class="pill-tab <?= $view === 'board' ? 'active' : '' ?>">▦ Board</a>
    </div>
    <a href="<?= url('/tickets/new') ?>" class="btn btn-primary">+ Log a ticket</a>
  </div>
</div>

<?php if ($view === 'board') : ?>
  <?php \App\Core\View::render('tickets/_board', ['tickets' => $tickets], null); ?>
<?php else : ?>
  <div class="card">
    <div class="table-wrap">
      <table class="table">
        <thead>
          <tr><th>Ticket No.</th><th>Customer</th><th>Subject</th><th>Pri</th><th>Queue</th><th>Assignee</th><th>SLA</th><th>Status</th></tr>
        </thead>
        <tbody>
        <?php if (empty($tickets)) : ?>
          <tr><td colspan="8"><div class="empty-state"><div class="icon">🗂️</div>No tickets match this view.</div></td></tr>
        <?php endif; ?>
        <?php foreach ($tickets as $t) : ?>
          <tr>
            <td><a class="row-link mono" href="<?= url('/tickets/' . $t['id']) ?>"><?= e($t['ticket_no']) ?></a>
              <?php if ($t['is_out_of_entitlement']) : ?><span class="badge badge-oe" title="Out of entitlement">OE</span><?php endif; ?>
            </td>
            <td><?= e($t['customer_name']) ?></td>
            <td style="max-width:260px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?= e($t['subject']) ?></td>
            <td><?= priority_badge(['code' => $t['p_code'], 'color' => $t['p_color']]) ?></td>
            <td class="text-sm"><?= e($t['queue_name'] ?? '—') ?></td>
            <td class="text-sm"><?= e($t['assignee_name'] ?? 'Unassigned') ?></td>
            <td>
              <?php if (in_array($t['status'], ['RESOLVED', 'CLOSED', 'REJECTED'])) : ?>
                <span class="text-faint text-sm">—</span>
              <?php elseif ($t['is_resolution_breached']) : ?>
                <span class="badge badge-breach">BREACHED</span>
              <?php else : ?>
                <span class="text-sm"><?= fmt_remaining($t['resolution_due_at'], false) ?></span>
              <?php endif; ?>
            </td>
            <td><span class="<?= status_badge_class($t['status']) ?>"><?= status_label($t['status']) ?></span></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </div>
<?php endif; ?>
