<div class="main-content">
  <div class="container-fluid" style="margin-top: 50px;">
    <div class="row mb-4">
      <div class="col-12 d-flex justify-content-between align-items-center">
        <h2><i class="bi bi-people me-2"></i>Daftar Pegawai</h2>
        <?php if (in_array($this->session->userdata('role'), ['superadmin', 'admin_opd'])): ?>
          <div class="d-flex gap-2">
            <a href="<?= base_url('import_pegawai') ?>" class="btn btn-primary">
              <i class="bi bi-file-earmark-arrow-up me-1"></i> Impor Excel
            </a>
            <a href="<?= base_url('pegawai/export_excel') ?>" class="btn btn-success">
              <i class="bi bi-file-earmark-excel me-1"></i> Ekspor Excel
            </a>
          </div>
        <?php endif; ?>
      </div>
    </div>

    <!-- Filter Pencarian -->
    <div class="card mb-4">
      <div class="card-body">
        <?= form_open('pegawai', ['method' => 'GET']) ?>
          <div class="row g-3">
            <div class="col-md-3">
              <label>Pencarian</label>
              <input type="text" name="search" class="form-control" value="<?= htmlspecialchars($this->input->get('search')) ?>" placeholder="Cari NIP, Nama, Jabatan...">
            </div>
            <?php if ($this->session->userdata('role') === 'superadmin'): ?>
              <div class="col-md-3">
                <label>Filter berdasarkan UNOR</label>
                <select name="unor_id" class="form-control">
                  <option value="">-- Semua UNOR --</option>
                  <?php foreach ($unor_list as $u): ?>
                    <option value="<?= htmlspecialchars($u->nama) ?>" 
                      <?= $this->input->get('unor_id') == $u->nama ? 'selected' : '' ?>>
                      <?= htmlspecialchars($u->nama) ?>
                    </option>
                  <?php endforeach; ?>
                </select>
              </div>
            <?php endif; ?>
            <div class="col-md-2">
              <label>Tampilan</label>
              <select name="per_page" class="form-control" onchange="this.form.submit()">
                <?php foreach ($allowed_per_page as $val): ?>
                  <option value="<?= $val ?>" <?= $per_page == $val ? 'selected' : '' ?>><?= $val ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-md-2 d-flex align-items-end">
              <button type="submit" class="btn btn-primary">Cari</button>
            </div>
          </div>
        <?= form_close() ?>
      </div>
    </div>

    <!-- Hasil -->
    <div class="card">
      <div class="card-header bg-success text-white">
        <i class="bi bi-list me-2"></i> Hasil (<?= $total_data ?> data dari <?= $total_all ?> total)
      </div>
      <div class="card-body">
        <?php if (empty($pegawai)): ?>
          <div class="alert alert-info">Tidak ada data ditemukan.</div>
        <?php else: ?>
          <div class="table-responsive">
            <table class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>No</th>
                  <th>NIP</th>
                  <th>Nama Pegawai</th>
                  <th>Jabatan</th>
                  <th>Golongan</th>
                  <th>UNOR</th>
                  <th>Aksi</th>
                </tr>
              </thead>
              <tbody>
                <?php 
                $no = $this->uri->segment(3) ? ($this->uri->segment(3) - 1) * $per_page + 1 : 1;
                foreach ($pegawai as $p): ?>
                <tr>
                  <td><?= $no++ ?></td>
                  <td><?= htmlspecialchars($p->nip) ?></td>
                  <td><?= htmlspecialchars($p->nama) ?></td>
                  <td><?= htmlspecialchars($p->jabatan) ?></td>
                  <td><?= htmlspecialchars($p->golru ?? '-') ?></td>
                  <td><?= htmlspecialchars($p->unor_nama ?? '-') ?></td>
                  <td>
                    <a href="<?= base_url("pegawai/edit/{$p->id_peg}") ?>" class="btn btn-sm btn-warning">
                      <i class="bi bi-pencil"></i> Edit
                    </a>
                  </td>
                </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>

          <!-- Pagination -->
          <div class="mt-3">
            <?= $this->pagination->create_links(); ?>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>