<?php

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

/**
 * @var UserModel $data;
 * @var DivisiModel $divisions
 * @var int $totalUser
 */


?>

<div class="card shadow-sm border-0 mb-4">
  <div class="card-header bg-white">
    <div class="d-flex justify-content-between align-items-center">
      <div>
        <h5 class="mb-1">
          <i class="bi bi-funnel"></i>
          Filter User
        </h5>
        <small class="text-muted">
          Cari berdasarkan nama, username, role, divisi, atau status akun.
        </small>
      </div>
    </div>
  </div>
  <div class="card-body">
    <form method="GET" action="<?= url('MANAJEMEN_USER') ?>">
      <div class="row g-3">
        <!-- Search -->
        <div class="col-lg-4">
          <label class="form-label">
            Pencarian
          </label>
          <div class="input-group">
            <span class="input-group-text">
              <i class="bi bi-search"></i>
            </span>
            <input
              type="text"
              class="form-control"
              name="keyword"
              placeholder="Nama, atau username"
              value="<?= htmlspecialchars($_GET['keyword'] ?? '') ?>">
          </div>
        </div>
        <!-- Role -->
        <div class="col-lg-2">
          <label class="form-label">
            Role
          </label>
          <select
            class="form-select"
            name="role">
            <option value="">
              Semua Role
            </option>
            <?php foreach (UserRole::cases() as $role): ?>
              <option
                value="<?= $role->value ?>"
                <?= ($_GET['role'] ?? '') === $role->value ? 'selected' : '' ?>>
                <?= $role->value ?>
              </option>
            <?php endforeach; ?>
          </select>
        </div>
        <!-- Divisi -->
        <div class="col-lg-2">
          <label class="form-label">
            Divisi
          </label>
          <select
            class="form-select"
            name="divisi">
            <option value="">
              Semua Divisi
            </option>
            <?php foreach ($divisions as $division): ?>
              <option
                value="<?= htmlspecialchars($division->getNamaDivisi()) ?>"
                <?= ($_GET['divisi'] ?? '') === $division->getNamaDivisi() ? 'selected' : '' ?>>
                <?= htmlspecialchars($division->getNamaDivisi()) ?>
              </option>
            <?php endforeach; ?>
          </select>
        </div>
        <!-- Status -->
        <div class="col-lg-2">
          <label class="form-label">
            Status
          </label>
          <select
            class="form-select"
            name="status">
            <option value="">
              Semua Status
            </option>
            <?php foreach (StatusAkun::cases() as $status): ?>
              <option
                value="<?= $status->value ?>"
                <?= ($_GET['status'] ?? '') === $status->value ? 'selected' : '' ?>>
                <?= ucfirst(strtolower($status->value)) ?>
              </option>
            <?php endforeach; ?>
          </select>
        </div>
        <!-- Button -->
        <div class="col-lg-2 d-grid">
          <label class="form-label invisible">
            Action
          </label>
          <div class="d-flex gap-2">
            <button
              type="submit"
              class="btn btn-primary flex-fill">
              <i class="bi bi-search"></i>
            </button>
            <a
              href="<?= url('MANAJEMEN_USER') ?>"
              class="btn btn-outline-secondary">
              <i class="bi bi-arrow-clockwise"></i>
            </a>
          </div>
        </div>
      </div>
    </form>
  </div>
</div>

<div class="card shadow-sm border-0">
  <div class="card-header bg-white">
    <div class="d-flex justify-content-between align-items-center">
      <div>
        <h5 class="mb-1">Daftar User</h5>
        <small class="text-muted">
          Total <?= $totalUser ?> pengguna
        </small>
      </div>
      <a
        href="<?= url('TAMBAH_USER') ?>"
        class="btn btn-primary">
        <i class="bi bi-plus-lg"></i>
        Tambah User
      </a>
    </div>
  </div>
  <div class="table-responsive">
    <table class="table table-hover align-middle mb-0">
      <thead class="table-light">
        <tr>
          <th width="60"></th>
          <th>User</th>
          <th>Role</th>
          <th>Divisi</th>
          <th>Status</th>
          <th>Dibuat</th>
          <th width="60"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($data as $user): ?>
          <?php
          $initial = strtoupper(substr($user->getNama(), 0, 1));

          $roleClass = match ($user->getRole()) {
            UserRole::ADMIN => 'bg-danger',
            UserRole::TEKNISI => 'bg-primary',
            UserRole::USER => 'bg-secondary',
          };

          $statusClass = match ($user->getStatusAkun()) {
            StatusAkun::AKTIF => 'bg-success',
            StatusAkun::NONAKTIF => 'bg-danger',
          };

          ?>
          <tr>
            <td>
              <div
                class="rounded-circle bg-primary text-white fw-semibold d-flex align-items-center justify-content-center"
                style="width:42px;height:42px;">
                <?= $initial ?>
              </div>
            </td>
            <td>
              <div class="fw-semibold">
                <?= htmlspecialchars($user->getNama()) ?>
              </div>
              <div class="small text-muted">
                <?= htmlspecialchars($user->getEmail()) ?>
              </div>
              <div class="small text-muted">
                @<?= htmlspecialchars($user->getUsername()) ?>
              </div>
            </td>
            <td>
              <span class="badge <?= $roleClass ?>">
                <?= $user->getRole()->value ?>
              </span>
            </td>
            <td>
              <?php if ($user->getNamaDivisi()): ?>
                <span class="badge text-bg-light border">
                  <?= htmlspecialchars($user->getNamaDivisi()) ?>
                </span>
              <?php else: ?>
                <span class="text-muted">
                  -
                </span>
              <?php endif; ?>
            </td>
            <td>
              <span class="badge <?= $statusClass ?>">
                <?= ucfirst(strtolower($user->getStatusAkun()->value)) ?>
              </span>
            </td>
            <td>
              <small class="text-muted">
                <?= $user->getCreatedAt() ?>
              </small>
            </td>
            <td>
              <div class="dropdown">
                <button
                  class="btn btn-light btn-sm"
                  data-bs-toggle="dropdown">
                  <i class="bi bi-three-dots-vertical"></i>
                </button>
                <ul class="dropdown-menu dropdown-menu-end">
                  <li>
                    <a
                      class="dropdown-item"
                      href="<?= url('DETAIL_USER', ['id' => $user->getId()]) ?>">
                      <i class="bi bi-eye"></i>
                      Detail
                    </a>
                  </li>
                  <li>
                    <a
                      class="dropdown-item"
                      href="<?= url('UBAH_PASSWORD', ['id' => $user->getId()]) ?>">
                      <i class="bi bi-key"></i>
                      Ubah Password
                    </a>
                  </li>
                </ul>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>