<?php
// Core working columns always shown; edge-case statuses (REOPENED, REJECTED) only
// appear if the current filter actually has tickets sitting in them.
$coreStatuses = ['NEW', 'ASSIGNED', 'IN_PROGRESS', 'PENDING_CUSTOMER', 'PENDING_THIRD_PARTY', 'ESCALATED', 'RESOLVED', 'CLOSED'];
$allOrder = array_merge($coreStatuses, ['REOPENED', 'REJECTED']);

$byStatus = [];
foreach ($tickets as $t) { $byStatus[$t['status']][] = $t; }

$columns = array_filter($allOrder, fn($s) => in_array($s, $coreStatuses, true) || !empty($byStatus[$s]));

// Cards can be picked up from these statuses; RESOLVED/CLOSED/REOPENED/REJECTED are
// display-only here — those transitions need fields a drag can't supply (see
// TicketController::kanbanMove).
$draggableFrom = ['NEW', 'ASSIGNED', 'IN_PROGRESS', 'PENDING_CUSTOMER', 'PENDING_THIRD_PARTY', 'ESCALATED'];
$droppableTo = ['NEW', 'ASSIGNED', 'IN_PROGRESS', 'PENDING_CUSTOMER', 'PENDING_THIRD_PARTY', 'ESCALATED'];
?>
<div class="kanban-board" id="kanban-board" data-move-url-template="<?= url('/tickets/__ID__/kanban-move') ?>" data-csrf="<?= e(\App\Core\Csrf::token()) ?>">
  <?php foreach ($columns as $status) : $colTickets = $byStatus[$status] ?? []; ?>
    <div class="kanban-col" data-status="<?= e($status) ?>" data-droppable="<?= in_array($status, $droppableTo, true) ? '1' : '0' ?>">
      <div class="kanban-col-head">
        <span><?= status_label($status) ?></span>
        <span class="count"><?= count($colTickets) ?></span>
      </div>
      <div class="kanban-col-body">
        <?php foreach ($colTickets as $t) : ?>
          <div class="kanban-card" data-ticket-id="<?= $t['id'] ?>" data-status="<?= e($status) ?>" draggable="<?= in_array($status, $draggableFrom, true) ? 'true' : 'false' ?>">
            <div class="flex justify-between items-center mb-0">
              <a href="<?= url('/tickets/' . $t['id']) ?>" class="mono text-sm row-link"><?= e($t['ticket_no']) ?></a>
              <?= priority_badge(['code' => $t['p_code'], 'color' => $t['p_color']]) ?>
            </div>
            <a href="<?= url('/tickets/' . $t['id']) ?>" class="kanban-card-subject"><?= e($t['subject']) ?></a>
            <div class="text-faint text-sm"><?= e($t['customer_name']) ?></div>
            <div class="flex justify-between items-center mt-2">
              <span class="text-sm"><?= e($t['assignee_name'] ?? 'Unassigned') ?></span>
              <?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 text-faint"><?= fmt_remaining($t['resolution_due_at'], false) ?></span>
              <?php endif; ?>
            </div>
            <?php if ($t['is_out_of_entitlement']) : ?><span class="badge badge-oe mt-2" style="display:inline-block">OE</span><?php endif; ?>
          </div>
        <?php endforeach; ?>
        <?php if (empty($colTickets)) : ?><div class="kanban-empty">No tickets</div><?php endif; ?>
      </div>
    </div>
  <?php endforeach; ?>
</div>
