<?php
$is_edit = isset($is_edit) && $is_edit === TRUE;
$usr     = isset($usr) ? $usr : NULL;
$action  = $is_edit ? site_url('user/update/' . $usr->id) : site_url('user/store');
?>
<div class="card" style="max-width:500px;">
  <?= form_open($action) ?>

  <div class="form-group">
    <label>Nama Lengkap <span style="color:red">*</span></label>
    <input type="text" name="name" class="form-control"
      value="<?= htmlspecialchars($usr->name ?? '') ?>"
      placeholder="Nama lengkap pengguna" required maxlength="100">
  </div>

  <div class="form-group">
    <label>Username <span style="color:red">*</span></label>
    <input type="text" name="username" class="form-control"
      value="<?= htmlspecialchars($usr->username ?? '') ?>"
      placeholder="Username untuk login (unik)"
      required maxlength="50" autocomplete="off">
  </div>

  <div class="form-group">
    <label>
      <?= $is_edit ? 'Password Baru' : 'Password' ?>
      <?= $is_edit ? '' : '<span style="color:red">*</span>' ?>
    </label>
    <div style="position:relative;">
      <input type="password" name="password" id="pwField"
        class="form-control"
        placeholder="<?= $is_edit ? 'Kosongkan jika tidak ingin mengubah' : 'Minimal 6 karakter' ?>"
        <?= $is_edit ? '' : 'required' ?>
        minlength="<?= $is_edit ? '0' : '6' ?>"
        autocomplete="new-password">
      <span onclick="togglePw()"
        style="position:absolute; right:10px; top:50%; transform:translateY(-50%);
               cursor:pointer; font-size:15px; color:#aaa; user-select:none;">👁</span>
    </div>
    <?php if ($is_edit): ?>
      <p style="font-size:11px; color:#888; margin-top:4px;">
        Biarkan kosong jika tidak ingin mengubah password.
      </p>
    <?php endif; ?>
  </div>

  <div class="form-row">
    <div class="form-group">
      <label>Role <span style="color:red">*</span></label>
      <select name="role" class="form-control" required>
        <option value="user"
          <?= (($usr->role ?? 'user') === 'user') ? 'selected' : '' ?>>
          👤 User
        </option>
        <option value="admin"
          <?= (($usr->role ?? '') === 'admin') ? 'selected' : '' ?>>
          👑 Admin
        </option>
      </select>
      <p style="font-size:11px; color:#888; margin-top:4px;">
        Admin: akses penuh + kelola akun.<br>
        User: hanya data milik sendiri.
      </p>
    </div>

    <?php if ($is_edit): ?>
      <div class="form-group">
        <label>Status Akun</label>
        <select name="is_active" class="form-control">
          <option value="1" <?= ($usr->is_active ?? 1) ? 'selected' : '' ?>>● Aktif</option>
          <option value="0" <?= !($usr->is_active ?? 1) ? 'selected' : '' ?>>● Nonaktif</option>
        </select>
      </div>
    <?php endif; ?>
  </div>

  <div class="form-actions">
    <button type="submit" class="btn btn-primary">
      <?= $is_edit ? '💾 Simpan Perubahan' : '+ Tambah Akun' ?>
    </button>
    <a href="<?= site_url('user') ?>" class="btn btn-light">Batal</a>
  </div>

  <?= form_close() ?>
</div>

<script>
function togglePw() {
  const f = document.getElementById('pwField');
  f.type = f.type === 'password' ? 'text' : 'password';
}
</script>
