<div class="main-content">
  <div class="container-fluid">
    <div class="row mb-4">
      <div class="col-12">
        <h2><i class="bi bi-pencil me-2"></i>Edit Admin</h2>
      </div>
    </div>

    <?= form_open('admin_group/edit/' . $admin->id, ['class' => 'card p-4']) ?>
      <input type="hidden" name="id" value="<?= $admin->id ?>">

      <div class="mb-3">
        <label>Username</label>
        <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($admin->username) ?>" required>
      </div>

      <div class="mb-3">
        <label>Password Baru <small>(kosongkan jika tidak diubah)</small></label>
        <input type="password" name="password" class="form-control" placeholder="••••••••">
      </div>

      <div class="mb-3">
        <label>Nama Lengkap</label>
        <input type="text" name="nama_lengkap" class="form-control" value="<?= htmlspecialchars($admin->nama_lengkap) ?>" required>
      </div>

      <div class="mb-3">
        <label>Role</label>
        <select name="role" class="form-control" disabled>
          <option value="admin_opd" <?= $admin->role === 'admin_opd' ? 'selected' : '' ?>>Admin OPD</option>
        </select>
        <input type="hidden" name="role" value="admin_opd">
      </div>

      <div class="mb-3">
        <label>OPD / UNOR</label>
        <input type="text" 
               id="unor_autocomplete" 
               class="form-control" 
               value="<?= htmlspecialchars($admin->unor_nama ?? '') ?>"
               placeholder="Ketik nama OPD/UNOR..."
               required>
        <input type="hidden" name="unor_id" id="unor_id" value="<?= htmlspecialchars($admin->unor_id) ?>" required>
        <div id="unor-error" class="text-danger" style="display:none;">OPD/UNOR harus dipilih dari daftar.</div>
      </div>

      <div class="d-flex gap-2">
        <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
        <a href="<?= base_url('admin_group') ?>" class="btn btn-secondary">Batal</a>
      </div>
    <?= form_close() ?>
  </div>
</div>

<script>
$(document).ready(function() {
    // Set nilai awal
    var initialName = "<?= htmlspecialchars($admin->unor_nama ?? '') ?>";
    var initialId = "<?= htmlspecialchars($admin->unor_id) ?>";
    
    $("#unor_autocomplete").autocomplete({
        source: "<?= base_url('admin_group/unor_autocomplete') ?>",
        minLength: 2,
        select: function(event, ui) {
            $("#unor_id").val(ui.item.id);
            $("#unor-error").hide();
        },
        response: function(event, ui) {
            if (ui.content.length === 0) {
                $("#unor_id").val('');
            }
        }
    }).on("keyup", function() {
        var value = $(this).val().trim();
        if (value === '') {
            $("#unor_id").val('');
        }
    });

    $("form").submit(function() {
        if ($("#unor_id").val() === '') {
            $("#unor-error").show();
            return false;
        }
        return true;
    });
});
</script>