<?php

use App\Models\ActivityLogModel;
use App\Models\AttachmentModel;
use App\Models\TeknisiModel;
use App\Models\TicketCommentModel;
use App\Models\TicketModel;

/**
 * @var TicketModel $ticket
 * @var array $availableStatus
 * @var TeknisiModel $daftarTeknisi
 * @var AttachmentModel $attachments
 * @var ActivityLogModel $timelines
 * @var TicketCommentModel  $comments
 */

?>

<div class="row">
  <!-- Content -->
  <div class="col-lg-8">
    <ul class="nav nav-tabs">
      <li class="nav-item">
        <button
          class="nav-link active"
          data-bs-toggle="tab"
          data-bs-target="#detail">
          <i class="bi bi-file-text me-1"></i>
          Detail
        </button>
      </li>
      <li class="nav-item">
        <button
          class="nav-link"
          data-bs-toggle="tab"
          data-bs-target="#timeline">
          <i class="bi bi-clock-history me-1"></i>
          Timeline
        </button>
      </li>
      <li class="nav-item">
        <button
          class="nav-link"
          data-bs-toggle="tab"
          data-bs-target="#comment">
          <i class="bi bi-chat-left-text me-1"></i>
          Komentar (<?= count((array) $comments) ?>)
        </button>
      </li>
    </ul>
    <div class="card rounded-0 border-top-0">
      <div class="card-body">
        <div class="tab-content">
          <!-- DETAIL -->
          <div
            class="tab-pane fade show active"
            id="detail">
            <div class="card shadow-sm border-0">
              <div class="card-header bg-white">
                <h5 class="mb-0">
                  Deskripsi Permasalahan
                </h5>
              </div>
              <div class="card-body">
                <p class="mb-0 p-2" style="background-color: #f1f1f1">
                  <?= $ticket->getDeskripsi() ?>
                </p>
              </div>
            </div>
            <!-- Attachment -->
            <div class="card shadow-sm border-0 mt-4">
              <div class="card-header bg-white d-flex gap-2">
                <h5 class="mb-0">
                  Lampiran
                </h5>
                <span class="badge bg-secondary">
                  <?= count((array) $attachments) ?>
                </span>
              </div>
              <div class="list-group list-group-flush">
                <?php if (empty($attachments)): ?>
                  <div class="list-group-item text-center py-4 text-muted">
                    <i class="bi bi-paperclip fs-2 d-block mb-2"></i>
                    Belum ada lampiran.
                  </div>
                <?php else: ?>
                  <?php foreach ($attachments as $attachment): ?>
                    <?php
                    $mime = strtolower($attachment->getTipeFile());

                    if (str_starts_with($mime, 'image/')) {
                      $icon = 'bi-file-earmark-image text-success';
                    } elseif ($mime === 'application/pdf') {
                      $icon = 'bi-file-earmark-pdf text-danger';
                    } elseif (
                      in_array($mime, [
                        'application/msword',
                        'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                      ])
                    ) {
                      $icon = 'bi-file-earmark-word text-primary';
                    } elseif (
                      in_array($mime, [
                        'application/vnd.ms-excel',
                        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                      ])
                    ) {
                      $icon = 'bi-file-earmark-excel text-success';
                    } elseif (
                      in_array($mime, [
                        'application/zip',
                        'application/x-rar-compressed',
                        'application/x-zip-compressed'
                      ])
                    ) {
                      $icon = 'bi-file-earmark-zip text-warning';
                    } else {
                      $icon = 'bi-file-earmark text-secondary';
                    }

                    $size = number_format($attachment->getUkuranFile() / 1024, 1);

                    ?>
                    <div class="list-group-item">
                      <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex align-items-center">
                          <div class="me-3">
                            <i class="bi <?= $icon ?> fs-2"></i>
                          </div>
                          <div>
                            <div class="fw-semibold">
                              <?= htmlspecialchars($attachment->getNamaFile()) ?>
                            </div>
                            <small class="text-muted">
                              <?= strtoupper(pathinfo($attachment->getNamaFile(), PATHINFO_EXTENSION)) ?>
                              •
                              <?= $size ?> KB
                            </small>
                          </div>
                        </div>
                        <div class="d-flex gap-1">
                          <a
                            href="<?= ticket_upload($attachment->getNamaFile()) ?>"
                            target="_blank"
                            class="btn btn-light btn-sm"
                            title="Lihat">
                            <i class="bi bi-eye"></i>
                          </a>
                          <a
                            href="<?= ticket_upload($attachment->getNamaFile()) ?>"
                            download
                            class="btn btn-primary btn-sm"
                            title="Download">
                            <i class="bi bi-download"></i>
                          </a>
                        </div>
                      </div>
                    </div>
                  <?php endforeach; ?>
                <?php endif; ?>
              </div>
            </div>
          </div>
          <!-- TIMELINE -->
          <div
            class="tab-pane fade"
            id="timeline">
            <?php $this->partial('ticket/detail/timeline', [
              'timelines' => $timelines
            ]); ?>
          </div>
          <!-- KOMENTAR -->
          <div
            class="tab-pane fade"
            id="comment">
            <?php $this->partial('ticket/detail/comment', [
              'comments' => $comments,
              'ticket' => $ticket
            ]); ?>
          </div>
        </div>
      </div>
    </div>
  </div>