<?php
$is_edit = isset($user) && $user;
$action  = $is_edit ? site_url('account/update/'.$user->id) : site_url('account/store');
?>
<div class="card" style="max-width:480px;">
  <?= 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($user->name ?? '') ?>"
      placeholder="Nama lengkap" required>
  </div>

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

  <div class="form-group">
    <label>Password <?= $is_edit ? '(kosongkan jika tidak ingin mengubah)' : '<span style="color:red">*</span>' ?></label>
    <div style="position:relative;">
      <input type="password" name="password" id="inp_pw" class="form-control"
        placeholder="<?= $is_edit ? 'Isi untuk mengubah password' : 'Minimal 6 karakter' ?>"
        <?= !$is_edit ? 'required' : '' ?> autocomplete="new-password">
      <button type="button" onclick="togglePw()"
        style="position:absolute;right:10px;top:50%;transform:translateY(-50%);background:none;border:none;cursor:pointer;color:#aaa;font-size:15px;"
        id="pwBtn">👁</button>
    </div>
  </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"  <?= (isset($user) && $user->role === 'user')  ? 'selected' : '' ?>>👤 User</option>
        <option value="admin" <?= (isset($user) && $user->role === 'admin') ? 'selected' : '' ?>>👑 Admin</option>
      </select>
    </div>
    <?php if ($is_edit): ?>
    <div class="form-group">
      <label>Status</label>
      <select name="is_active" class="form-control">
        <option value="1" <?= ($user->is_active) ? 'selected' : '' ?>>✓ Aktif</option>
        <option value="0" <?= (!$user->is_active)? 'selected' : '' ?>>✗ Nonaktif</option>
      </select>
    </div>
    <?php endif; ?>
  </div>

  <?php if ($is_edit): ?>
  <div class="alert alert-info" style="font-size:12px; padding:10px 12px;">
    ℹ️ Role menentukan akses: <strong>Admin</strong> bisa lihat semua data &amp; kelola akun.
    <strong>User</strong> hanya bisa akses data milik sendiri.
  </div>
  <?php endif; ?>

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

  <?= form_close() ?>
</div>
<script>
function togglePw() {
  const inp = document.getElementById('inp_pw');
  const btn = document.getElementById('pwBtn');
  if (inp.type==='password') { inp.type='text'; btn.textContent='🙈'; }
  else { inp.type='password'; btn.textContent='👁'; }
}
</script>
