<?php

use App\Helpers\BadgeHelper;
use App\Helpers\SlaCalculator;
use App\Models\StatusTicketModel;
use App\Models\TeknisiModel;
use App\Models\TicketModel;

/**
 * @var TicketModel $ticket
 * @var StatusTicketModel $availableStatus
 * @var TeknisiModel $daftarTeknisi
 */

$slaStatus = null;
if ($ticket->getDeadline()) {
  $slaStatus = SlaCalculator::getSlaStatus($ticket->getTanggalDibuat(), $ticket->getDeadline());
}

$priorityColor = BadgeHelper::priorityColor($ticket->getNamaPrioritas());
$icon  = BadgeHelper::priorityIcon($ticket->getNamaPrioritas());
$statusColor   = BadgeHelper::statusColor($ticket->getNamaStatus());

$user = $_SESSION['user'];
?>

<div class="d-flex justify-content-between align-items-start mb-4">
  <!-- Left -->
  <div>
    <div class="d-flex align-items-center gap-2 mb-3">
      <a href="<?= url('DAFTAR_TIKET') ?>" class="btn btn-light border btn-sm">
        <i class="bi bi-arrow-left"></i>
        Kembali
      </a>
      <span class="fw-semibold text-primary">
        #<?= $ticket->getNomorTiket() ?>
      </span>
      <span class="badge rounded-pill bg-<?= $statusColor ?>-subtle text-<?= $statusColor ?>">
        <i class="bi bi-kanban me-1"></i>
        <?= htmlspecialchars($ticket->getNamaStatus()) ?>
      </span>
      <?php if ($ticket->getPrioritasId()) : ?>
        <span class="badge rounded-pill bg-<?= $priorityColor ?> d-flex 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">
          <i class="bi <?= $icon ?>"></i> Belum Ditentukan
        </span>
      <?php endif; ?>
      <?php if ($slaStatus) : ?>
        <span class="badge rounded-pill bg-<?= $slaStatus['color'] ?> ">
          <?= $slaStatus["status"] ?>
        </span>
      <?php else: ?>
        <span class="badge rounded-pill bg-secondary-subtle text-secondary-emphasis">
          SLA Tidak Diketahui
        </span>
      <?php endif; ?>
    </div>
    <h3 class="fw-bold mb-0">
      <?= htmlspecialchars($ticket->getJudul()) ?>
    </h3>
  </div>
  <!-- Right -->
  <?php if ($user["role"] == "ADMIN"): ?>
    <div class="d-flex gap-2">
      <?php if ($ticket->getNamaStatus() === 'OPEN'): ?>
        <button
          class="btn btn-outline-primary"
          data-bs-toggle="modal"
          data-bs-target="#assignTicketModal">
          <i class="bi bi-person-plus me-1"></i>
          Assign
        </button>
      <?php endif; ?>
      <?php if ($ticket->getNamaStatus() !== 'SOLVED' && $ticket->getNamaStatus() !== 'CANCELED'): ?>
        <div class="dropdown">
          <button
            class="btn btn-outline-secondary dropdown-toggle"
            data-bs-toggle="dropdown">
            <i class="bi bi-arrow-repeat me-1"></i>
            Update Status
          </button>
          <ul class="dropdown-menu dropdown-menu-end">
            <?php foreach ($availableStatus as $status): ?>
              <li>
                <form
                  action="<?= url('UPDATE_STATUS_TIKET', ['id' => $ticket->getId()]) ?>"
                  method="POST">
                  <input
                    type="hidden"
                    name="userId"
                    value="1">
                  <input
                    type="hidden"
                    name="statusId"
                    value="<?= $status['id_status'] ?>">
                  <button
                    type="submit"
                    class="dropdown-item">
                    <?= htmlspecialchars($status['nama_status']) ?>
                  </button>
                </form>
              </li>
            <?php endforeach; ?>
          </ul>
        </div>
      <?php else: ?>
        <span class="badge text-bg-<?= $statusColor ?> fs-6 px-3 py-2">
          <?= htmlspecialchars($ticket->getNamaStatus()) ?>
        </span>
      <?php endif; ?>
      <?php if ($ticket->getNamaStatus() === 'OPEN' || $ticket->getNamaStatus() === 'ASSIGNED' || $ticket->getNamaStatus() === 'IN PROGRESS') : ?>
        <button type="button"
          class="btn btn-danger btn-sm btn-escalate"
          data-bs-toggle="modal"
          data-bs-target="#escalateModal"
          data-ticket-id="<?= $ticket->getId() ?>">
          <i class="bi bi-exclamation-triangle me-1"></i>
          Eskalasi
        </button>
      <?php endif; ?>
    </div>
  <?php endif; ?>
</div>

<div
  class="modal fade"
  id="assignTicketModal"
  tabindex="-1"
  aria-labelledby="assignTicketModalLabel"
  aria-hidden="true">
  <div class="modal-dialog">
    <form
      method="POST"
      action="<?= url('ASSIGN_TIKET', ['id' => $ticket->getId()]) ?>">
      <!-- Status ASSIGNED -->
      <input
        type="hidden"
        name="status"
        value="ASSIGNED">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">
            <i class="bi bi-person-check-fill text-primary me-2"></i>
            Assign Tiket
          </h5>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal">
          </button>
        </div>
        <div class="modal-body">
          <div class="alert alert-primary border-0">
            <div class="fw-semibold">
              <?= htmlspecialchars($ticket->getNomorTiket()) ?>
            </div>
            <div class="small mt-1">
              <?= htmlspecialchars($ticket->getJudul()) ?>
            </div>
          </div>
          <div class="mb-3">
            <label class="form-label">
              Teknisi
              <span class="text-danger">*</span>
            </label>
            <select
              name="teknisiId"
              class="form-select"
              required>
              <option value="">
                Pilih Teknisi
              </option>
              <?php foreach ($daftarTeknisi as $teknisi): ?>
                <option value="<?= $teknisi->getId() ?>">
                  <?= htmlspecialchars($teknisi->getNamaUser()) ?>
                  <?php if ($teknisi->getSpesialisasi()): ?>
                    — <?= htmlspecialchars($teknisi->getSpesialisasi()) ?>
                  <?php endif; ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="mb-0">
            <label class="form-label">
              Catatan Penugasan
            </label>
            <textarea
              name="catatan"
              class="form-control"
              rows="4"
              maxlength="1000"
              placeholder="Masukkan catatan penugasan (opsional)..."></textarea>
            <div class="form-text">
              Catatan ini akan disimpan sebagai informasi penugasan teknisi.
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-outline-secondary"
            data-bs-dismiss="modal">
            Batal
          </button>
          <button
            type="submit"
            class="btn btn-primary">
            <i class="bi bi-person-check-fill me-1"></i>
            Assign Teknisi
          </button>
        </div>
      </div>
    </form>
  </div>
</div>

<!-- Modal Eskalasi -->
<div class="modal fade" id="escalateModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <form action="<?= url('ESKALASI_TIKET', ['id' => $ticket->getId()]) ?>" method="POST">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title text-danger">
            <i class="bi bi-arrow-up-right-circle me-2"></i>
            Eskalasi Tiket
          </h5>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal">
          </button>
        </div>
        <div class="modal-body">
          <input
            type="hidden"
            name="status"
            value="ESCALATED">
          <div class="alert alert-warning mb-4">
            <i class="bi bi-exclamation-triangle-fill me-2"></i>
            Tiket akan dialihkan ke teknisi lain untuk penanganan lebih lanjut.
          </div>
          <div class="mb-3">
            <label class="form-label">
              Teknisi Tujuan
            </label>
            <select
              class="form-select"
              name="teknisiId"
              required>
              <option value="">
                -- Pilih Teknisi --
              </option>
              <?php foreach ($daftarTeknisi as $teknisi): ?>
                <option value="<?= $teknisi->getUserId() ?>">
                  <?= htmlspecialchars($teknisi->getNamaUser()) ?>
                  (<?= htmlspecialchars($teknisi->getSpesialisasi()) ?>)
                </option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="mb-3">
            <label class="form-label">
              Alasan Eskalasi
            </label>
            <textarea
              class="form-control"
              name="catatan"
              rows="4"
              maxlength="500"
              placeholder="Contoh: Permasalahan membutuhkan penanganan Network Engineer."
              required></textarea>
            <div class="form-text">
              Jelaskan alasan tiket dialihkan ke teknisi lain.
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary"
            data-bs-dismiss="modal">
            Batal
          </button>
          <button
            type="submit"
            class="btn btn-danger">
            <i class="bi bi-arrow-repeat me-1"></i>
            Eskalasi Tiket
          </button>
        </div>
      </div>
    </form>
  </div>
</div>