<?php

use App\Helpers\BadgeHelper;
use App\Helpers\SlaCalculator;

/**
 * @var array $tickets
 * @var array $statusTiket
 * @var array $daftarPrioritas
 * @var array $categories
 * @var array $daftarSla
 */

?>


<div class="container">
  <?php
  $this->partial('ticket/filter', [
    'statusTiket' => $statusTiket,
    'daftarPrioritas' => $daftarPrioritas,
    'categories' => $categories,
    'daftarSla' => $daftarSla
  ])
  ?>
  <div class="card shadow-sm">
    <div class="card-body">
      <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
          <h3 class="mb-0">
            Daftar Tiket
          </h3>
          <small class="text-muted">
            Total <?= count($tickets) ?> tiket
          </small>
        </div>

        <!-- <a
          href="<?= url('TAMBAH_TIKET') ?>"
          class="btn btn-primary">
          <i class="bi bi-plus-lg"></i>
          Buat Tiket
        </a> -->
      </div>
      <?php if (empty($tickets)): ?>
        <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>
        </div>
      <?php endif; ?>
      <?php foreach ($tickets as $ticket): ?>
        <?php


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

        ?>
        <div class="card border mb-3 ticket-card">
          <div class="card-body">
            <div class="d-flex justify-content-between align-items-start mb-2">
              <div class="d-flex gap-2">
                <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; ?>
              </div>
              <small class="text-muted fw-semibold">
                #<?= $ticket->getNomorTiket() ?>
              </small>
            </div>
            <?php
            $slaStatus = null;

            if ($ticket->getDeadline()) {
              if ($ticket->getNamaStatus() === 'OPEN') {
                $slaStatus = SlaCalculator::getSlaStatus(
                  $ticket->getTanggalDibuat(),
                  $ticket->getDeadline()
                );
              }
            }
            ?>

            <?php if ($slaStatus): ?>

              <div class="row">
                <div class="col-md-3">
                  <div class="d-flex align-items-center gap-3 mb-3">

                    <span class="badge rounded-pill bg-<?= $slaStatus['color'] ?> ">
                      <?= $slaStatus['status'] ?>
                    </span>

                    <div class="flex-grow-1">

                      <div
                        class="progress"
                        style="height:10px; border-radius:999px;">

                        <div
                          class="progress-bar bg-<?= $slaStatus['color'] ?>"
                          role="progressbar"
                          style="width: <?= min($slaStatus['percentage'], 100) ?>%;"
                          aria-valuenow="<?= $slaStatus['percentage'] ?>"
                          aria-valuemin="0"
                          aria-valuemax="100">
                        </div>

                      </div>

                    </div>

                    <small class="fw-semibold text-<?= $slaStatus['color'] ?>">
                      <?= $slaStatus['percentage'] ?>%
                    </small>

                  </div>
                </div>
              </div>

            <?php endif; ?>

            <div class="d-flex justify-content-between align-items-start mb-0">
              <div>
                <h5 class="fw-bold mb-1">
                  <?= htmlspecialchars($ticket->getJudul()) ?>
                </h5>
              </div>
              <div class="d-flex flex-wrap justify-content-end gap-4 small">
                <span class="text-muted">
                  <i class="bi bi-tags-fill text-primary me-1"></i>
                  <?= htmlspecialchars($ticket->getNamaKategori()) ?>
                </span>
                <span class="text-muted">
                  <i class="bi bi-person-fill text-success me-1"></i>
                  <?= htmlspecialchars($ticket->getNamaUser()) ?>
                </span>
                <span class="text-muted">
                  <i class="bi bi-calendar-event text-warning me-1"></i>
                  <?= $ticket->getTanggalDibuat() ?>
                </span>
              </div>
            </div>

            <div class="d-flex justify-content-between align-items-center mt-4">
              <div class="d-flex gap-4">
                <small>
                  <i class="bi bi-alarm text-primary"></i>
                  SLA
                  <strong>
                    <?= $ticket->getNamaSla() ?>
                  </strong>
                </small>
                <small>
                  <i class="bi bi-clock-history text-danger"></i>
                  Deadline
                  <strong>
                    <?php if ($ticket->getDeadline()) : ?>
                      <?= date('d M Y H:i', strtotime($ticket->getDeadline())) ?>
                    <?php else: ?>
                      ...
                    <?php endif; ?>
                  </strong>
                </small>
              </div>
              <div class="d-flex justify-content-end gap-2">
                <a
                  href="<?= url('DETAIL_TIKET', ['id' => $ticket->getId()]) ?>"
                  class="btn btn-outline-secondary btn-sm">
                  <i class="bi bi-eye"></i>
                  Detail
                </a>
                <?php if ($ticket->getSlaId() === 0): ?>
                  <button
                    class="btn btn-outline-primary btn-sm btn-atur-sla"
                    data-id="<?= $ticket->getId() ?>"
                    data-bs-toggle="modal"
                    data-bs-target="#modalAturSla">
                    <i class="bi bi-pencil"></i>
                    Atur SLA
                  </button>
                <?php endif; ?>
              </div>
            </div>
          </div>
        </div>
      <?php endforeach; ?>
    </div>
  </div>
</div>


<!-- Modal Atur SLA -->
<div class="modal fade" id="modalAturSla" tabindex="-1" aria-labelledby="modalAturSlaLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <form action="<?= url('UPDATE_SLA_TIKET') ?>
        " method="POST">
        <div class="modal-header">
          <h5 class="modal-title" id="modalAturSlaLabel">
            <i class="bi bi-clock-history">
            </i>
            Atur SLA Tiket
          </h5>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal">
          </button>
        </div>
        <div class="modal-body">
          <!-- ID Ticket -->
          <input
            type="hidden"
            name="id_ticket"
            class="form-control"
            value="<?php if ($ticket !== null) {
                      echo $ticket->getId();
                    } ?>"
            readonly>
          <!-- SLA -->
          <div class="mb-3">
            <label class="form-label">
              SLA
            </label>
            <select
              name="id_sla"
              class="form-select"
              required>
              <option value="">
                -- Pilih SLA --
              </option>
              <?php foreach ($daftarSla as $sla): ?>
                <option value="<?= $sla->getId() ?>">
                  <?= $sla->getNamaSla() ?>
                  (
                  <?= $sla->getWaktuPenyelesaianJam() ?> Jam)
                </option>
              <?php endforeach; ?>
            </select>
          </div>
          <!-- Prioritas -->
          <div class="mb-3">
            <label class="form-label">
              Prioritas
            </label>
            <select
              name="id_prioritas"
              class="form-select"
              required>
              <option value="">
                -- Pilih Prioritas --
              </option>
              <?php foreach ($daftarPrioritas as $prioritas): ?>
                <option value="<?= $prioritas->getId() ?>">
                  <?= $prioritas->getNamaPrioritas() ?>
                </option>
              <?php endforeach; ?>
            </select>
          </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-primary">
            <i class="bi bi-save">
            </i>
            Simpan Perubahan
          </button>
        </div>
      </form>
    </div>
  </div>
</div>