<?php

use App\Models\StatusAkun;
use App\Models\UserModel;
use App\Models\UserRole;
use App\Models\DivisiModel;

/** 
 * @var DivisiModel $divisions 
 * @var UserModel $user
 * @var array $divisions
 */
?>

<div class="container">
  <?php if ($user !== null): ?>

    <div class="card">
      <div class="card-body">
        <!-- Header -->
        <div class="d-flex justify-content-between align-items-center mb-4">
          <div>
            <h5 class="mb-1">
              <i class="bi bi-person"></i>
              Detail User
            </h5>
            <small class="text-muted">
              Informasi lengkap tentang user.
            </small>
          </div>
          <div>
            <a
              href="<?= url('MANAJEMEN_USER') ?>"
              class="btn btn-link">
              <i class="bi bi-arrow-left"></i>
              Kembali
            </a>
          </div>
        </div>

        <!-- Form -->
        <form method="POST" action="<?= url('EDIT_USER', ['id' => $user->getId()]) ?>">

          <div class="mb-3">
            <label class="form-label">
              Nama Lengkap <span class="text-danger">*</span>
            </label>
            <input
              type="text"
              class="form-control"
              name="nama"
              value="<?= htmlspecialchars($user->getNama()) ?>"
              required>
          </div>

          <div class="mb-3">
            <label class="form-label">
              Username <span class="text-danger">*</span>
            </label>
            <input
              type="text"
              class="form-control"
              name="username"
              value="<?= htmlspecialchars($user->getUsername()) ?>"
              required>
            <div class="form-text">
              Minimal 4 karakter dan harus unik.
            </div>
          </div>

          <div class="mb-3">
            <label class="form-label">
              Email <span class="text-danger">*</span>
            </label>
            <input
              type="email"
              class="form-control"
              name="email"
              value="<?= htmlspecialchars($user->getEmail()) ?>"
              required>
            <div class="form-text">
              Contoh: user@perusahaan.com
            </div>
          </div>

          <div class="mb-3">
            <label class="form-label">No. Telepon</label>
            <input
              type="text"
              class="form-control"
              name="noTelepon"
              value="<?= htmlspecialchars($user->getNoTelepon()) ?>">
            <div class="form-text">
              Format: diawali 08 dan terdiri dari 10-15 digit angka.
            </div>
          </div>

          <div class="mb-3">
            <label class="form-label">
              Role <span class="text-danger">*</span>
            </label>

            <select class="form-select" name="role">
              <?php foreach (UserRole::cases() as $role): ?>
                <option
                  value="<?= $role->value ?>"
                  <?= $user->getRole() === $role ? 'selected' : '' ?>>
                  <?= $role->value ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>

          <div class="mb-3">
            <label class="form-label">Divisi</label>
            <select class="form-select" name="divisiId">
              <option value="">-- Tidak Ada --</option>

              <?php foreach ($divisions as $division): ?>
                <option
                  value="<?= $division->getId() ?>"
                  <?= $user->getDivisiId() === $division->getId() ? 'selected' : '' ?>>
                  <?= htmlspecialchars($division->getNamaDivisi()) ?>
                </option>
              <?php endforeach; ?>
            </select>
            <div class="form-text">
              Wajib dipilih apabila Role adalah <strong>Teknisi</strong>.
            </div>
          </div>

          <!-- Spesialisasi -->
          <div class="mb-3">
            <label class="form-label">Spesialisasi</label>
            <input
              type="text"
              class="form-control"
              name="spesialisasi"
              value="<?= htmlspecialchars($user->getSpesialisasi()) ?>">
            <div class="form-text">
              Wajib dipilih apabila Role adalah <strong>Teknisi</strong>.
            </div>
          </div>

          <div class="mb-3">
            <label class="form-label d-block">Status Akun</label>

            <?php foreach (StatusAkun::cases() as $status): ?>
              <div class="form-check">
                <input
                  class="form-check-input"
                  type="radio"
                  name="statusAkun"
                  id="status<?= $status->value ?>"
                  value="<?= $status->value ?>"
                  <?= $user->getStatusAkun() === $status ? 'checked' : '' ?>>

                <label
                  class="form-check-label"
                  for="status<?= $status->value ?>">
                  <?= ucfirst(strtolower($status->value)) ?>
                </label>
              </div>
            <?php endforeach; ?>
          </div>

          <div class="d-flex justify-content-end gap-2">

            <button type="submit" class="btn btn-primary">
              <i class="bi bi-check-lg"></i>
              Simpan Perubahan
            </button>
          </div>

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

  <?php else: ?>

    <div class="alert alert-danger">
      Pengguna tidak ditemukan.
      <a href="<?= url('MANAJEMEN_USER') ?>" class="btn btn-primary">
        Kembali
      </a>
    </div>

  <?php endif; ?>
</div>