<?php

use App\Helpers\BadgeHelper;
use App\Models\TicketModel;

/**
 * @var TicketModel $tickets
 */


?>
<div class="mb-4">
  <a href="<?= url('HOME') ?>" class="text-decoration-none">
    <i class="bi bi-arrow-left"></i>
    Kembali
  </a>

  <h2 class="fw-bold mt-3">
    Aktivitas Saya
  </h2>

  <p class="text-muted">
    Lihat riwayat aktivitas dan perubahan yang terjadi pada tiket Anda.
  </p>
</div>

<div class="card shadow-sm border-0">
  <div class="card-body p-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
      <div>
        <h5 class="fw-bold mb-1">
          <i class="bi bi-ticket-perforated me-2">
          </i>
          Tiket Saya
        </h5>
        <p class="text-muted mb-0">
          Daftar tiket yang pernah Anda buat.
        </p>
      </div>
      <a href="<?= url('REQUEST_TIKET') ?>
        " class="btn btn-primary">
        <i class="bi bi-plus-lg me-1">
        </i>
        Buat Tiket
      </a>
    </div>


    <?php if (empty($tickets)): ?>
      <div class="border rounded p-3 mb-3">
        <div class="text-center py-5">
          <i class="bi bi-ticket-perforated display-1 text-secondary opacity-50"></i>
          <h5 class="mt-3">
            Belum ada tiket
          </h5>
          <p class="text-muted">
            Klik tombol <strong>Buat Tiket</strong> untuk membuat tiket baru.
          </p>
        </div>
      </div>
    <?php else: ?>
      <?php foreach ($tickets as $ticket): ?>
        <div class="border rounded p-3 mb-3">
          <?php
          $priorityColor = BadgeHelper::priorityColor($ticket->getNamaPrioritas());
          $icon  = BadgeHelper::priorityIcon($ticket->getNamaPrioritas());
          $statusColor   = BadgeHelper::statusColor($ticket->getNamaStatus()); ?>

          <div class="row align-items-center">
            <!-- Ticket Info -->
            <div class="col-md-5">
              <h6 class="fw-bold mb-1">
                #<?= $ticket->getNomorTiket() ?> - <?= $ticket->getJudul() ?>
              </h6>
              <div class="text-muted">
                <i class="bi bi-calendar me-1"></i>
                <?= $ticket->getTanggalDibuat() ?>
              </div>
            </div>

            <div class="col-md-2">
              <small class="text-muted d-block">
                Status
              </small>
              <span>
                <span class="badge rounded-pill bg-<?= $statusColor ?>-subtle text-<?= $statusColor ?>">
                  <i class="bi bi-kanban me-1"></i>
                  <?= htmlspecialchars($ticket->getNamaStatus()) ?>
                </span>
              </span>
            </div>

            <!-- Category -->
            <div class="col-md-2">
              <small class="text-muted d-block">
                Kategori
              </small>
              <span>
                <?= $ticket->getNamaKategori() ?>
              </span>
            </div>
            <!-- Priority -->
            <div class="col-md-2">
              <small class="text-muted d-block">
                Prioritas
              </small>
              <?php if ($ticket->getNamaPrioritas() != null): ?>
                <span class="badge rounded-pill bg-<?= $priorityColor ?> align-items-center gap-1">
                  <i class="bi <?= $icon ?>"></i>
                  <?= htmlspecialchars($ticket->getNamaPrioritas()) ?>
                </span>
              <?php else: ?>
                <span class="badge rounded-pill bg-secondary-subtle text-secondary-emphasis px-3">
                  -
                </span>
              <?php endif; ?>
            </div>

            <div class="col-md-1 text-end">
              <div class="text-end mt-2">
                <a href="<?= url('USER_TIKET_DETAIL', ['id' => $ticket->getId()]) ?>" class="btn btn-sm btn-outline-primary">
                  Detail
                </a>
              </div>
            </div>
          </div>
        </div>
      <?php endforeach ?>
    <?php endif; ?>


  </div>
</div>