<?php

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

/**
 * @var array $summary
 * @var TicketModel $tickets
 */


$within = $summary["withinSla"];
$near = $summary["nearSla"];
$breached = $summary["overSla"];

$total = $within + $near + $breached; // 8
$greenEnd = 0;
$yellowEnd = 0;
$redEnd = 0;
$slaRate = 0;

if ($within > 0) {
  $greenEnd = ($within / $total) * 360;
  $yellowEnd = $greenEnd + (($near / $total) * 360);
  $redEnd = 360;
  $slaRate = round(($within / $total) * 100);
}

?>
<div class="row">

  <!-- DONUT -->
  <div class="col-lg-3">
    <div class="card shadow-sm border-0 mb-4">
      <div class="card-header bg-white">
        <h5 class="mb-0">SLA Rate</h5>
      </div>

      <div class="card-body">



        <div class="d-flex text-center justify-content-center">

          <div
            class="sla-chart"
            style="background:conic-gradient(
                #198754 0deg <?= $greenEnd ?>deg,
                #ffc107 <?= $greenEnd ?>deg <?= $yellowEnd ?>deg,
                #dc3545 <?= $yellowEnd ?>deg <?= $redEnd ?>deg
            );">
            <div class="sla-chart-center">
              <h2><?= $slaRate ?>%</h2>
              <small>SLA Rate</small>
            </div>
          </div>

        </div>

        <hr>

        <div class="d-flex justify-content-between mb-2">
          <span>
            <i class="bi bi-square-fill text-success"></i>
            Within SLA
          </span>

          <strong>
            <?= $within ?>
          </strong>
        </div>

        <div class="d-flex justify-content-between mb-2">
          <span>
            <i class="bi bi-square-fill text-warning"></i>
            Near SLA
          </span>

          <strong>
            <?= $near ?>
          </strong>
        </div>

        <div class="d-flex justify-content-between">
          <span>
            <i class="bi bi-square-fill text-danger"></i>
            Over SLA
          </span>

          <strong>
            <?= $breached ?>
          </strong>
        </div>

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


  <!-- TABLE -->
  <div class="col-lg-9">

    <div class="card shadow-sm border-0">

      <div class="card-header bg-white">
        <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">

          <div>
            <h5 class="mb-0">
              Monitoring Tiket Aktif
            </h5>

            <small class="text-muted">
              Menampilkan <?= count((array) $tickets) ?> tiket aktif
            </small>
          </div>

        </div>
      </div>

      <div class="table-responsive">

        <table class="table table-hover align-middle mb-0">

          <thead class="table-light">

            <tr>

              <th>No. Tiket</th>

              <th>Judul</th>

              <th>Prioritas</th>

              <th>Status</th>

              <th>SLA</th>

              <th>Requester</th>

              <th>Deadline</th>

              <th width="70"></th>

            </tr>

          </thead>

          <tbody>

            <?php foreach ($tickets as $ticket) : ?>
              <tr>

                <td>

                  <div class="fw-semibold text-primary">
                    #<?= $ticket->getNomorTiket() ?>
                  </div>

                  <small class="text-muted">
                    <?= hours_ago($ticket->getTanggalDibuat()); ?> lalu
                  </small>

                </td>

                <td>

                  <div class="fw-semibold">
                    <?= $ticket->getJudul() ?>
                  </div>

                  <small class="text-muted">
                    <?= $ticket->getNamaKategori() ?>
                  </small>

                </td>

                <td>

                  <?php
                  $icon   = BadgeHelper::priorityIcon($ticket->getNamaPrioritas());
                  $priorityColor   = BadgeHelper::priorityColor($ticket->getNamaPrioritas());
                  ?>
                  <span class="badge bg-<?= $priorityColor ?>-subtle text-<?= $priorityColor ?>">
                    <?php if ($ticket->getPrioritasId() > 0): ?>
                      <i class="bi bi-<?= $icon ?> me-1"></i>
                      <?= $ticket->getNamaPrioritas() ?>
                    <?php else: ?>
                      <i class="bi bi-<?= $icon ?> me-1"></i>
                      Belum Ditentukan
                    <?php endif; ?>
                  </span>

                </td>

                <td>

                  <?php
                  $statusColor   = BadgeHelper::statusColor($ticket->getNamaStatus());
                  ?>
                  <span class="badge bg-<?= $statusColor ?>-subtle text-<?= $statusColor ?>">
                    <i class="bi bi-kanban text-<?= $statusColor ?> me-1"></i>

                    <span class="fw-semibold">
                      <?= $ticket->getNamaStatus() ?>
                    </span>

                  </span>

                </td>

                <td>

                  <?php
                  $slaStatus = $ticket->getSlaId() > 0 ? SlaCalculator::getSlaStatus($ticket->getTanggalDibuat(), $ticket->getDeadline()) : null;
                  ?>
                  <?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; ?>

                </td>

                <td>

                  <div class="d-flex align-items-center">

                    <div
                      class="rounded-circle bg-primary text-white fw-semibold d-flex align-items-center justify-content-center me-2"
                      style="width:32px;height:32px;">
                      <?= strtoupper(substr($ticket->getNamaUser(), 0, 1)); ?>
                    </div>

                    <div>

                      <div>
                        <div class="fw-semibold">
                          <?= $ticket->getNamaUser() ?>
                        </div>
                      </div>

                    </div>

                  </div>

                </td>

                <td>

                  <div class="fw-semibold">
                    <?= $ticket->getDeadline() ?>
                  </div>

                </td>

                <td>

                  <a
                    href="<?= url('DETAIL_TIKET', ['id' => $ticket->getId()]) ?>"
                    class="btn btn-light btn-sm">

                    <i class="bi bi-eye"></i>

                  </a>

                </td>

              </tr>
            <?php endforeach; ?>



          </tbody>

        </table>

      </div>

    </div>

  </div>

</div>