<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:20px">
  <div>
    <h2 style="font-size:1.2rem;font-weight:700">👥 Daftar Warga Binaan</h2>
    <p class="text-muted text-sm"><?= e($ri['nama']) ?> &middot; <?= count($data) ?> warga</p>
  </div>
</div>

<div class="tabs">
  <?php foreach(['semua'=>'Semua','Tinggi'=>'🔥 Prioritas Tinggi','Sedang'=>'⚡ Sedang','Rendah'=>'✅ Rendah'] as $l=>$label): ?>
  <a href="?level=<?= $l ?>" class="tab-btn <?= $level===$l?'active':'' ?>"><?= $label ?></a>
  <?php endforeach; ?>
</div>

<div class="filters-bar">
  <input type="text" id="search-w" class="form-control" placeholder="🔍 Cari nama warga..." style="max-width:280px">
</div>

<div class="card">
  <div class="card-body no-pad">
    <div class="table-wrap">
      <table id="tbl-w">
        <thead>
          <tr><th>#</th><th>Nama</th><th>Status Kemiskinan</th><th>Prioritas</th><th>Penghasilan</th><th>Tanggungan</th><th>Aksi</th></tr>
        </thead>
        <tbody>
          <?php foreach($data as $i=>$w): ?>
          <tr>
            <td class="text-xs text-muted"><?= $i+1 ?></td>
            <td>
              <div style="font-weight:600"><?= e($w['nama']) ?></div>
              <div class="text-xs text-muted"><?= e($w['kelurahan']??'') ?></div>
            </td>
            <td>
              <?php $sc=['Miskin Ekstrem'=>'badge-red','Miskin'=>'badge-orange','Rentan Miskin'=>'badge-yellow']; ?>
              <span class="badge <?= $sc[$w['status_kemiskinan']]??'badge-gray' ?>"><?= e($w['status_kemiskinan']) ?></span>
            </td>
            <td>
              <?php $pc=['Tinggi'=>'badge-red','Sedang'=>'badge-yellow','Rendah'=>'badge-green']; ?>
              <span class="badge <?= $pc[$w['level_prioritas']]??'badge-gray' ?>"><?= e($w['level_prioritas']) ?></span>
              <div class="text-xs text-muted"><?= $w['skor_prioritas'] ?>/100</div>
            </td>
            <td class="text-sm"><?= formatRupiah($w['penghasilan_bulanan']??0) ?></td>
            <td class="text-sm text-center"><?= $w['jumlah_tanggungan'] ?> orang</td>
            <td>
              <a href="<?= APP_URL ?>/rumah-ibadah/warga/<?= $w['id'] ?>" class="btn btn-ghost btn-sm">Detail</a>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
    <?php if(empty($data)): ?>
    <div class="empty-state">
      <div class="empty-state-icon">👥</div>
      <h3>Tidak ada warga binaan</h3>
      <p>Warga binaan akan otomatis ditugaskan berdasarkan radius pelayanan</p>
    </div>
    <?php endif; ?>
  </div>
</div>

<script>document.addEventListener('DOMContentLoaded',()=>App.initTableSearch('search-w','tbl-w'));</script>
