<?php $isEdit = !empty($u); ?>
<form method="post" action="<?= $isEdit ? url('/pengguna/'.(int)$u['id'].'/ubah') : url('/pengguna/tambah') ?>" data-loading>
<?= csrf() ?>
<div class="row g-3 justify-content-center">
  <div class="col-lg-8">
    <div class="card"><div class="card-header"><i class="bi bi-person-gear me-1"></i><?= e($judul) ?></div>
      <div class="card-body row g-3">
        <div class="col-md-4"><label class="form-label required" for="username">Nama Pengguna</label>
          <input type="text" class="form-control" id="username" name="username" required minlength="3" maxlength="60"
                 value="<?= attr($isEdit ? $u['username'] : old('username')) ?>" <?= $isEdit ? 'readonly' : '' ?>>
          <?php if ($isEdit): ?><div class="form-text">Nama pengguna tidak dapat diubah.</div><?php endif; ?></div>
        <div class="col-md-4"><label class="form-label required" for="name">Nama Lengkap</label>
          <input type="text" class="form-control" id="name" name="name" required maxlength="120" value="<?= attr($isEdit ? $u['name'] : old('name')) ?>"></div>
        <div class="col-md-4"><label class="form-label required" for="email">Email</label>
          <input type="email" class="form-control" id="email" name="email" required maxlength="150" value="<?= attr($isEdit ? $u['email'] : old('email')) ?>"></div>

        <?php if (!$isEdit): ?>
        <div class="col-md-4"><label class="form-label required" for="password">Kata Sandi Awal</label>
          <input type="text" class="form-control" id="password" name="password" required minlength="<?= (int)$minSandi ?>">
          <div class="form-text">Minimal <?= (int)$minSandi ?> karakter, memuat huruf dan angka.</div></div>
        <?php endif; ?>
        <div class="col-md-4"><label class="form-label required" for="role_id">Peran</label>
          <select class="form-select" id="role_id" name="role_id" required>
            <?php foreach ($roles as $r): ?>
              <option value="<?= (int)$r['id'] ?>" <?= (int)($isEdit ? $u['role_id'] : 2) === (int)$r['id'] ? 'selected':'' ?>><?= e($r['name']) ?></option>
            <?php endforeach; ?></select>
          <div class="form-text">Peran menentukan izin dan cakupan data yang dapat dilihat.</div></div>
        <div class="col-md-4"><label class="form-label" for="phone">Telepon</label>
          <input type="text" class="form-control" id="phone" name="phone" maxlength="40" value="<?= attr($isEdit ? $u['phone'] : '') ?>"></div>

        <div class="col-md-3"><label class="form-label" for="area_id">Area</label>
          <select class="form-select" id="area_id" name="area_id"><option value="">—</option>
            <?php foreach ($areas as $a): ?><option value="<?= (int)$a['id'] ?>" <?= (int)($isEdit ? $u['area_id'] : 0)===(int)$a['id']?'selected':'' ?>><?= e($a['name']) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-3"><label class="form-label" for="business_unit_id">Unit Bisnis</label>
          <select class="form-select" id="business_unit_id" name="business_unit_id"><option value="">—</option>
            <?php foreach ($bus as $b): ?><option value="<?= (int)$b['id'] ?>" <?= (int)($isEdit ? $u['business_unit_id'] : 0)===(int)$b['id']?'selected':'' ?>><?= e($b['name']) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-3"><label class="form-label" for="salesperson_id">Terhubung ke Sales</label>
          <select class="form-select" id="salesperson_id" name="salesperson_id"><option value="">—</option>
            <?php foreach ($salespersons as $s): ?><option value="<?= (int)$s['id'] ?>" <?= (int)($isEdit ? $u['salesperson_id'] : 0)===(int)$s['id']?'selected':'' ?>><?= e($s['name']) ?></option><?php endforeach; ?></select>
          <div class="form-text">Wajib untuk peran Sales.</div></div>
        <div class="col-md-3"><label class="form-label" for="collection_officer_id">Terhubung ke Petugas Penagihan</label>
          <select class="form-select" id="collection_officer_id" name="collection_officer_id"><option value="">—</option>
            <?php foreach ($officers as $o): ?><option value="<?= (int)$o['id'] ?>" <?= (int)($isEdit ? $u['collection_officer_id'] : 0)===(int)$o['id']?'selected':'' ?>><?= e($o['name']) ?></option><?php endforeach; ?></select>
          <div class="form-text">Wajib untuk peran Collection.</div></div>

        <div class="col-12"><label class="form-label">Peran Tambahan (opsional)</label>
          <div class="d-flex flex-wrap gap-3">
            <?php foreach ($roles as $r): ?>
              <div class="form-check">
                <input class="form-check-input" type="checkbox" id="er_<?= (int)$r['id'] ?>" name="extra_roles[]" value="<?= (int)$r['id'] ?>"
                       <?= in_array((int)$r['id'], $extraRoles, true) ? 'checked' : '' ?>>
                <label class="form-check-label small" for="er_<?= (int)$r['id'] ?>"><?= e($r['name']) ?></label></div>
            <?php endforeach; ?>
          </div>
          <div class="form-text">Izin dari peran tambahan digabungkan dengan peran utama.</div></div>

        <div class="col-md-6"><div class="form-check form-switch">
          <input class="form-check-input" type="checkbox" id="is_active" name="is_active" value="1" <?= (!$isEdit || (int)$u['is_active']===1) ? 'checked':'' ?>>
          <label class="form-check-label" for="is_active">Akun aktif</label></div></div>
        <?php if (!$isEdit): ?>
        <div class="col-md-6"><div class="form-check form-switch">
          <input class="form-check-input" type="checkbox" id="must_change_password" name="must_change_password" value="1" checked>
          <label class="form-check-label" for="must_change_password">Wajib ganti kata sandi saat masuk pertama kali</label></div></div>
        <?php endif; ?>
      </div>
      <div class="card-footer d-flex gap-2">
        <button class="btn btn-primary" type="submit"><i class="bi bi-check-lg me-1"></i>Simpan</button>
        <a class="btn btn-light" href="<?= url('/pengguna') ?>">Batal</a>
      </div>
    </div>
  </div>
</div>
</form>
