<?php

use App\Models\StatusTicketModel;

/**
 * @var array $categories
 * @var array $priorities
 * @var array $slas
 * @var StatusTicketModel $statusOpen
 */
$user = $_SESSION['user'];

?>
<div class="container">
  <div class="card shadow-sm">
    <div class="card-body">
      <div class="mb-4">
        <h3 class="mb-1">
          Buat Tiket Baru
        </h3>
        <small class="text-muted">
          Isi informasi berikut untuk membuat tiket baru.
        </small>
      </div>
      <form
        method="POST"
        enctype="multipart/form-data"
        action="<?= url('TAMBAH_TIKET') ?>">

        <!-- ID USER -->
        <input type="hidden" name="userId" value="<?= $user['id'] ?>">
        <!-- STATUS = OPEN -->
        <input type="hidden" name="statusId" value="<?= $statusOpen->getId() ?>">
        <div class="mb-3">
          <label class="form-label">
            Judul
            <span class="text-danger">*</span>
          </label>
          <input
            type="text"
            class="form-control"
            name="judul"
            maxlength="255"
            placeholder="Contoh: Email tidak dapat diakses"
            required>
        </div>
        <div class="row">
          <div class="col-md-4 mb-3">
            <label class="form-label">
              Kategori
              <span class="text-danger">*</span>
            </label>
            <select
              class="form-select"
              name="kategoriId"
              required>
              <option value="">Pilih Kategori</option>
              <?php foreach ($categories as $category): ?>
                <option value="<?= $category->getId() ?>" selected="<?php $_GET["kategori"] == $category->getId() ?> ">
                  <?= htmlspecialchars($category->getNamaKategori()) ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>
          <?php if ($user['role'] === 'ADMIN'): ?>
            <div class="col-md-4 mb-3">
              <label class="form-label">
                Prioritas
                <span class="text-danger">*</span>
              </label>
              <select
                class="form-select"
                name="prioritasId"
                required>
                <option value="">Pilih Prioritas</option>
                <?php foreach ($priorities as $priority): ?>
                  <option value="<?= $priority->getId() ?>">
                    <?= htmlspecialchars($priority->getNamaPrioritas()) ?>
                  </option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-md-4 mb-3">
              <label class="form-label">
                SLA
                <span class="text-danger">*</span>
              </label>
              <select
                class="form-select"
                name="slaId"
                required>
                <option value="">Pilih SLA</option>
                <?php foreach ($slas as $sla): ?>
                  <option value="<?= $sla->getId() ?>">
                    <?= htmlspecialchars($sla->getNamaSla()) ?>
                  </option>
                <?php endforeach; ?>
              </select>
            </div>
          <?php endif; ?>
        </div>
        <div class="mb-4">
          <label class="form-label">
            Deskripsi
          </label>
          <textarea
            class="form-control"
            rows="8"
            name="deskripsi"
            placeholder="Jelaskan permasalahan secara lengkap..."></textarea>
        </div>

        <div class="mb-4">

          <label class="form-label">
            Lampiran
          </label>

          <div class="border rounded p-4 text-center bg-light">

            <i class="bi bi-cloud-upload fs-1 text-primary"></i>

            <p class="mb-2 mt-2">
              Pilih atau tarik file ke area ini
            </p>

            <input
              id="attachments"
              type="file"
              class="form-control"
              name="attachments[]"
              multiple>

          </div>

          <!-- Preview file -->
          <div id="attachmentPreview" class="mt-3"></div>

        </div>

        <div class="d-flex justify-content-end gap-2">
          <a
            href="<?= url('DAFTAR_TIKET') ?>"
            class="btn btn-light border">
            Batal
          </a>
          <button
            type="submit"
            class="btn btn-primary">
            <i class="bi bi-send"></i>
            Kirim Tiket
          </button>
        </div>
      </form>
    </div>
  </div>
</div>