<?php

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

/**
 * @var array $slaResponse
 * @var array $slaResolution
 * @var TicketModel $ticket
 * @var UserModel $requester
 * @var UserModel $teknisi
 */
$icon  = BadgeHelper::priorityIcon($ticket->getNamaPrioritas());
$statusColor   = BadgeHelper::statusColor($ticket->getNamaStatus());
$priorityColor = BadgeHelper::priorityColor($ticket->getNamaPrioritas());

$slaResponseColor = $slaResponse ? $slaResponse['color'] : 'secondary';
$responsePercent = $slaResponse ? $slaResponse['responsePercent'] : 0;
$slaResponseStatus = $slaResponse ? $slaResponse['status'] : 'Tidak diketahui';

$slaResolutionColor = $slaResolution ? $slaResolution['color'] : 'secondary';
$resolutionPercent = $slaResolution ? $slaResolution['resolutionPercent'] : 0;
$slaResolutionStatus = $slaResolution ? $slaResolution['status'] : 'tidak diketahui';
$resolutionRemaining = $slaResolution ? $slaResolution['remainingText'] : 'Tidak diketahui';

$slaResolutionMinutes = $slaResolution ? $slaResolution['slaMinutes'] / 60  : 0;
?>

<!-- Sidebar -->
<div class="col-lg-4">
  <!-- SLA -->
  <div class="card shadow-sm border-0 mb-4">
    <div class="card-header bg-white">
      <h6 class="mb-0">
        SLA Status
      </h6>
    </div>
    <div class="card-body">
      <div class="mb-4">
        <div class="d-flex justify-content-between mb-1">
          <small class="fw-semibold">
            Response SLA
          </small>
          <small class="text-<?= $slaResponseColor ?>">
            <?= $responsePercent ?>%
          </small>
        </div>
        <div class="progress" style="height:8px;">
          <div
            class="progress-bar bg-<?= $slaResponseColor ?>"
            style="width: <?= $responsePercent ?>%">
          </div>
        </div>
        <small class="text-muted">
          <span class="text-<?= $slaResponseColor ?>">
            <?= $slaResponseStatus ?>
          </span>
        </small>
      </div>
      <div>
        <div class="d-flex justify-content-between mb-1">
          <small class="fw-semibold">
            Resolution SLA
          </small>
          <small class="text-<?= $slaResolutionColor ?>">
            <?= $resolutionPercent ?>%
          </small>
        </div>
        <div class="progress" style="height:8px;">
          <div
            class="progress-bar bg-<?= $slaResolutionColor ?>"
            style="width: <?= $resolutionPercent ?>%">
          </div>
        </div>
        <small class="text-muted">
          <?php if ($ticket->isClosed()): ?>

            <span class="text-<?= $slaResolutionColor ?>">
              <?= $slaResolutionStatus ?>
            </span>

          <?php else: ?>

            <span class="text-muted">
              Sisa waktu <?= $resolutionRemaining ?>
            </span>

          <?php endif; ?>
        </small>
      </div>
    </div>
  </div>
  <!-- Informasi Ticket -->
  <div class="card shadow-sm border-0 mb-4">
    <div class="card-header bg-white">
      <h6 class="mb-0">
        Informasi Tiket
      </h6>
    </div>
    <div class="card-body">
      <table class="table table-borderless table-sm mb-0">
        <tr>
          <th width="40%">Status</th>
          <td>
            <span class="badge rounded-pill bg-<?= $statusColor ?>-subtle text-<?= $statusColor ?>">
              <i class="bi bi-kanban me-1"></i>
              <?= htmlspecialchars($ticket->getNamaStatus()) ?>
            </span>
          </td>
        </tr>
        <tr>
          <th>Prioritas</th>
          <td>
            <span class="badge rounded-pill bg-<?= $priorityColor ?>">
              <i class="bi <?= $icon ?>"></i>
              <?= htmlspecialchars($ticket->getNamaPrioritas()) ?>
            </span>
          </td>
        </tr>
        <tr>
          <th>Kategori</th>
          <td><?= $ticket->getNamaKategori() ?></td>
        </tr>
        <tr>
          <th>SLA</th>
          <td><?= $slaResolutionMinutes ?> Jam</td>
        </tr>
        <tr>
          <th>Dibuat</th>
          <td><?= $ticket->getTanggalDibuat() ?></td>
        </tr>
        <tr>
          <th>Deadline</th>
          <td><?= $ticket->getDeadline() ?></td>
        </tr>
      </table>
    </div>
  </div>
  <!-- Requester -->
  <div class="card shadow-sm border-0 mb-4">
    <div class="card-header bg-white">
      <h6 class="mb-0">
        Requester
      </h6>
    </div>
    <div class="card-body">
      <div class="d-flex align-items-center">
        <div
          class="rounded-circle bg-primary text-white d-flex justify-content-center align-items-center me-3"
          style="width:48px;height:48px;">
          <strong>
            <?= strtoupper(substr($requester->getNama(), 0, 1)); ?>
          </strong>
        </div>
        <div>
          <div class="fw-semibold">
            <?= $requester->getNama() ?>
          </div>
          <small class="text-muted">
            <?= $requester->getEmail() ?>
          </small>
          <div class="small text-muted">
            <?= $requester->getNamaDivisi() ?? "Divisi Tidak Diketahui" ?>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- Teknisi -->
  <?php if ($teknisi): ?>
    <div class="card shadow-sm border-0">
      <div class="card-header bg-white d-flex justify-content-between align-items-center">
        <h6 class="mb-0">
          Teknisi
        </h6>
      </div>
      <div class="card-body">
        <div class="d-flex align-items-center">
          <div
            class="rounded-circle bg-success text-white d-flex justify-content-center align-items-center me-3"
            style="width:48px;height:48px;">
            <strong>
              <?= strtoupper(substr($teknisi->getNama(), 0, 1)); ?>
            </strong>
          </div>
          <div>
            <div class="fw-semibold">
              <?= $teknisi->getNama() ?>
            </div>
            <small class="text-muted">
              <?= $teknisi->getEmail() ?>
            </small>
            <div class="small text-muted">
              <?= $teknisi->getNamaDivisi() ?? "Divisi Tidak Diketahui" ?>
            </div>
          </div>
        </div>
      </div>
    </div>
  <?php endif; ?>
</div>