<?= $this->extend('layouts/admin') ?>
<?= $this->section('content') ?>

<div class="mb-4">
  <a href="<?= base_url('admin/users') ?>" class="btn btn-sm btn-outline-secondary">
    <i class="bi bi-arrow-left me-1"></i>Kembali
  </a>
  <h1 class="section-title mt-2 mb-0"><?= esc($title) ?></h1>
</div>

<?php if (session()->has('errors')): ?>
  <div class="alert alert-danger small py-2 mb-3">
    <?php foreach (session('errors') as $err): ?>
      <div><i class="bi bi-dot"></i><?= esc($err) ?></div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

<?php $action = $user ? base_url('admin/users/' . $user['user_id'] . '/update') : base_url('admin/users'); ?>
<form method="post" action="<?= $action ?>" class="row g-3" style="max-width:600px;">
  <?= csrf_field() ?>

  <div class="col-md-6">
    <label class="form-label small fw-semibold">Nama Lengkap <span class="text-danger">*</span></label>
    <input type="text" name="name" class="form-control" required maxlength="150"
           value="<?= old('name', $user['name'] ?? '') ?>">
  </div>

  <div class="col-md-6">
    <label class="form-label small fw-semibold">NIM / NIDN</label>
    <input type="text" name="identity_number" class="form-control" maxlength="30"
           value="<?= old('identity_number', $user['identity_number'] ?? '') ?>">
  </div>

  <?php if (! $user): ?>
  <div class="col-12">
    <label class="form-label small fw-semibold">Email <span class="text-danger">*</span></label>
    <input type="email" name="email" class="form-control" required maxlength="254"
           value="<?= old('email') ?>">
  </div>
  <?php endif; ?>

  <div class="col-md-6">
    <label class="form-label small fw-semibold"><?= $user ? 'Password Baru (kosongkan jika tidak diubah)' : 'Password *' ?></label>
    <input type="password" name="password" class="form-control" minlength="8"
           <?= $user ? '' : 'required' ?> placeholder="Min. 8 karakter">
  </div>

  <div class="col-md-6">
    <label class="form-label small fw-semibold">Peran <span class="text-danger">*</span></label>
    <select name="role" class="form-select" required>
      <?php foreach (['student'=>'Mahasiswa','lecturer'=>'Dosen','admin'=>'Admin','super_admin'=>'Super Admin'] as $v => $l): ?>
        <option value="<?= $v ?>" <?= old('role', $user['role'] ?? 'student') === $v ? 'selected' : '' ?>>
          <?= $l ?>
        </option>
      <?php endforeach; ?>
    </select>
  </div>

  <div class="col-md-6">
    <label class="form-label small fw-semibold">Program Studi</label>
    <select name="study_program_id" class="form-select">
      <option value="">-- Pilih --</option>
      <?php foreach ($programs as $p): ?>
        <option value="<?= $p['id'] ?>"
          <?= old('study_program_id', $user['study_program_id'] ?? '') == $p['id'] ? 'selected' : '' ?>>
          <?= esc($p['name']) ?>
        </option>
      <?php endforeach; ?>
    </select>
  </div>

  <div class="col-12">
    <button type="submit" class="btn btn-primary">
      <i class="bi bi-save me-2"></i><?= $user ? 'Simpan Perubahan' : 'Tambah Pengguna' ?>
    </button>
  </div>
</form>

<?= $this->endSection() ?>
