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

<h1 class="section-title mb-4">Program Studi</h1>

<div class="row g-4">
  <!-- Add form -->
  <div class="col-lg-4">
    <div class="card-stt p-4">
      <h6 class="fw-semibold mb-3 border-bottom pb-2">Tambah Program Studi</h6>

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

      <form method="post" action="<?= base_url('admin/programs') ?>">
        <?= csrf_field() ?>
        <div class="mb-3">
          <label class="form-label small fw-semibold">Nama Program Studi *</label>
          <input type="text" name="name" class="form-control form-control-sm" required maxlength="150"
                 value="<?= old('name') ?>" placeholder="Contoh: Teologi">
        </div>
        <div class="mb-3">
          <label class="form-label small fw-semibold">Kode *</label>
          <input type="text" name="code" class="form-control form-control-sm" required maxlength="20"
                 value="<?= old('code') ?>" placeholder="Contoh: TEO">
        </div>
        <button type="submit" class="btn btn-primary btn-sm w-100">
          <i class="bi bi-plus-circle me-1"></i>Tambah
        </button>
      </form>
    </div>
  </div>

  <!-- List -->
  <div class="col-lg-8">
    <div class="card-stt overflow-hidden">
      <table class="table table-stt mb-0">
        <thead><tr><th>Nama</th><th>Kode</th><th></th></tr></thead>
        <tbody>
          <?php if (empty($programs)): ?>
            <tr><td colspan="3" class="text-center text-muted py-4">Belum ada program studi.</td></tr>
          <?php else: ?>
            <?php foreach ($programs as $p): ?>
              <tr>
                <td class="small fw-semibold"><?= esc($p['name']) ?></td>
                <td class="small text-muted"><?= esc($p['code']) ?></td>
                <td>
                  <form method="post" action="<?= base_url('admin/programs/' . $p['id'] . '/delete') ?>" class="d-inline"
                        onsubmit="return confirm('Hapus program studi ini?')">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm btn-outline-danger">
                      <i class="bi bi-trash"></i>
                    </button>
                  </form>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

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