<?php
require_once __DIR__.'/../includes/db.php';
require_once __DIR__.'/../includes/auth.php';
include __DIR__.'/../includes/header.php';

$data = $mysqli->query("
  SELECT p.*,
         a.nama_lengkap AS pemberi,
         b.nama_lengkap AS penerima
  FROM pengajuan_surat_kuasa p
  JOIN infografis a ON p.pemberi_id=a.id
  JOIN infografis b ON p.penerima_id=b.id
  ORDER BY p.status='pending' DESC, p.tanggal_pengajuan DESC
");
?>

<h2>📥 Pengajuan Surat Kuasa</h2>

<table class="table">
<thead>
<tr>
  <th>Pemberi</th>
  <th>Penerima</th>
  <th>Status</th>
  <th>Aksi</th>
</tr>
</thead>
<tbody>
<?php while($r=$data->fetch_assoc()): ?>
<tr>
  <td><?= esc($r['pemberi']) ?></td>
  <td><?= esc($r['penerima']) ?></td>
  <td><?= strtoupper($r['status']) ?></td>
  <td>
    <?php if($r['status']=='pending'): ?>
      <a class="btn small primary"
         href="pengajuan_kuasa_acc.php?id=<?= $r['id'] ?>">
         ACC
      </a>
    <?php else: ?>
      <span class="badge success">SELESAI</span>
    <?php endif; ?>
  </td>
</tr>
<?php endwhile; ?>
</tbody>
</table>

<?php include __DIR__.'/../includes/footer.php'; ?>
