<?php

/**
 * @var array $divisions
 */
?>
<div class="container">
  <div class="card shadow-sm">
    <div class="card-body">
      <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
          <h3 class="mb-0">Daftar Divisi</h3>
          <small class="text-muted">
            Total <?= count($divisions) ?> divisi
          </small>
        </div>
        <button
          type="button"
          class="btn btn-primary"
          data-bs-toggle="modal"
          data-bs-target="#divisionModal">
          <i class="bi bi-plus"></i>
          Tambah Divisi
        </button>
      </div>
      <?php if (empty($divisions)): ?>
        <div class="text-center py-5 text-muted">
          <i class="bi bi-building fs-1"></i>
          <p class="mt-2 mb-0">Belum ada data divisi.</p>
        </div>
      <?php endif; ?>
      <?php foreach ($divisions as $division): ?>
        <div class="card shadow-sm mb-3">
          <div class="card-body d-flex justify-content-between align-items-start">
            <div>
              <h6 class="mb-1">
                <i class="bi bi-building me-2 text-primary"></i>
                <?= htmlspecialchars($division->getNamaDivisi()) ?>
              </h6>
              <?php if ($division->getDeskripsi()): ?>
                <small class="text-muted">
                  <?= htmlspecialchars($division->getDeskripsi()) ?>
                </small>
              <?php else: ?>
                <small class="text-muted fst-italic">
                  Tidak ada deskripsi
                </small>
              <?php endif; ?>
            </div>
            <div class="d-flex gap-2">
              <button
                type="button"
                class="btn btn-outline-primary btn-sm"
                data-bs-toggle="modal"
                data-bs-target="#divisionModal"
                data-id="<?= $division->getId() ?>"
                data-name="<?= htmlspecialchars($division->getNamaDivisi()) ?>"
                data-description="<?= htmlspecialchars($division->getDeskripsi()) ?>">
                <i class="bi bi-pencil"></i>
              </button>
              <button
                type="button"
                class="btn btn-danger btn-sm"
                data-bs-toggle="modal"
                data-bs-target="#divisionDeleteConfirmationModal"
                data-id="<?= $division->getId() ?>"
                data-name="<?= htmlspecialchars($division->getNamaDivisi()) ?>">
                <i class="bi bi-trash"></i>
              </button>
            </div>
          </div>
        </div>
      <?php endforeach; ?>
    </div>
  </div>
</div>
<!-- Modal Tambah Divisi -->
<div
  class="modal fade"
  id="divisionModal"
  tabindex="-1"
  aria-labelledby="divisionModalLabel"
  aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <form
        id="divisionForm"
        method="POST"
        action="<?= url('TAMBAH_DIVISI') ?>">
        <div class="modal-header">
          <h5
            class="modal-title"
            id="divisionModalLabel">
          </h5>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"></button>
        </div>
        <div class="modal-body">
          <input
            type="hidden"
            name="id"
            id="id">
          <div class="mb-3">
            <label class="form-label">
              Nama Divisi
              <span class="text-danger">*</span>
            </label>
            <input
              type="text"
              class="form-control"
              name="namaDivisi"
              placeholder="Contoh: IT Support"
              maxlength="100"
              id="namaDivisi"
              required>
            <div class="form-text">
              Nama divisi harus unik.
            </div>
          </div>
          <div class="mb-0">
            <label class="form-label">
              Deskripsi
            </label>
            <textarea
              class="form-control"
              name="deskripsi"
              id="deskripsi"
              rows="3"
              placeholder="Masukkan deskripsi singkat (opsional)"></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-light border"
            data-bs-dismiss="modal">
            Batal
          </button>
          <button
            type="submit"
            class="btn btn-primary">
            <i class="bi bi-check-lg"></i>
            Simpan
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- Modal Delete Divisi -->
<div class="modal fade" id="divisionDeleteConfirmationModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="divisionDeleteConfirmationModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="divisionDeleteConfirmationModalLabel">Hapus Divisi</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <p>
          Apakah anda yakin ingin menghapus divisi <strong id="deleteDivisionName"></strong>?
        </p>
        <form action="<?= url('HAPUS_DIVISI') ?>" method="post">
          <input type="hidden" name="id" id="deleteDivisionId">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button type="submit" class="btn btn-danger">Hapus</button>
        </form>
      </div>
    </div>
  </div>
</div>