<?php

use App\Models\TicketCommentModel;
use App\Models\TicketModel;

/**
 * @var TicketCommentModel $comments
 * @var TicketModel $ticket
 */


?>

<div class="card shadow-sm border-0">
  <div class="card-header bg-white d-flex justify-content-between align-items-center">
    <h5 class="mb-0">
      Komentar
    </h5>
    <span class="badge bg-secondary">
      <?= count((array) $comments) ?> Komentar
    </span>
  </div>
  <div class="card-body">

    <?php if (
      empty($comments) &&
      $ticket->getNamaStatus() !== "SOLVED" &&
      $ticket->getNamaStatus() !== "CANCELED"
    ): ?>
      <div class="text-center py-5">
        <i class="bi bi-chat-left-quote display-1 text-secondary opacity-50"></i>
        <h5 class="mt-3">
          Belum ada Komentar
        </h5>
      </div>
    <?php else: ?>
      <?php foreach ($comments as $comment): ?>
        <div class="mb-2 card bg-light">
          <div class="card-body d-flex">
            <div
              class="rounded-circle bg-primary text-white d-flex justify-content-center align-items-center flex-shrink-0"
              style="width:32px;height:32px;">
              <?= strtoupper(substr($comment->getNamaUser(), 0, 1)); ?>
            </div>
            <div class="ms-3 flex-grow-1">
              <div class="d-flex justify-content-between">
                <div>
                  <strong><?= $comment->getNamaUser() ?></strong>
                  <?php if ($comment->getUserId() === $ticket->getUserId()): ?>
                    <span class="badge bg-light text-dark border ms-2">
                      Requester
                    </span>
                  <?php endif; ?>
                </div>
                <small class="text-muted">
                  <?= $comment->getTanggalKomentar() ?>
                </small>
              </div>
              <div class="mt-2">
                <?= $comment->getKomentar() ?>
              </div>
            </div>
          </div>
        </div>
      <?php endforeach; ?>
      <hr>
    <?php endif; ?>
  </div>

  <!-- Form Komentar -->

  <?php if ($ticket->getNamaStatus() == "SOLVED" || $ticket->getNamaStatus() == "CANCELED"): ?>
    <div class="text-center py-5">
      <i class="bi bi-chat-left-quote display-1 text-secondary opacity-50"></i>
      <h5 class="mt-3">
        Komentar Ditutup
      </h5>
    </div>
  <?php else: ?>
    <form action="<?= url("SUBMIT_KOMENTAR") ?>" method="POST" class="p-3">
      <div class="mb-3">
        <input type="hidden" name="ticketId" value="<?= $ticket->getId() ?>">
        <label class="form-label">
          Tambah Komentar
        </label>
        <textarea
          name="komentar"
          class="form-control"
          rows="4"
          placeholder="Tulis komentar..."></textarea>
      </div>
      <div class="text-end">
        <button class="btn btn-primary">
          <i class="bi bi-send"></i>
          Kirim Komentar
        </button>
      </div>
    </form>
  <?php endif; ?>

</div>