<?php
require_once __DIR__.'/../includes/db.php';

// Ambil data pengajuan yang belum diverifikasi
$pengajuan = $mysqli->query("SELECT * FROM pengajuan_surat WHERE status = 'pending'");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // Ambil data dari form
  $id_pengajuan = $_POST['id_pengajuan'];
  $status = $_POST['status'];

  // Update status pengajuan
  $stmt = $mysqli->prepare("UPDATE pengajuan_surat SET status = ?, tanggal_verifikasi = CURDATE() WHERE id = ?");
  $stmt->bind_param('si', $status, $id_pengajuan);
  $stmt->execute();

  if ($status === 'approved') {
    // Jika disetujui, masukkan ke tabel surat_penghasilan_ortu
    $stmt = $mysqli->prepare("
      INSERT INTO surat_penghasilan_ortu (nomor_surat, warga_id, pekerjaan_ortu, penghasilan_ortu, alasan_surat, tanggal_surat)
      SELECT CONCAT('510.1/', RAND()*1000, '/Ds.M/X/', YEAR(CURDATE())), warga_id, pekerjaan_ortu, penghasilan_ortu, alasan_surat, tanggal_surat
      FROM pengajuan_surat WHERE id = ?
    ");
    $stmt->bind_param('i', $id_pengajuan);
    $stmt->execute();
  }

  header("Location: verifikasi_pengajuan.php");
  exit;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <title>Verifikasi Pengajuan Surat</title>
  <style>
    body { font-family: Arial, sans-serif; background-color: #f4f4f4; }
    .container { max-width: 800px; margin: 20px auto; padding: 20px; background: #fff; border-radius: 10px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); }
    h1 { text-align: center; }
    table { width: 100%; border-collapse: collapse; margin: 20px 0; }
    table, th, td { border: 1px solid #ddd; }
    th, td { padding: 8px; text-align: left; }
    .btn { padding: 10px 20px; background-color: #28a745; color: #fff; border: none; border-radius: 5px; cursor: pointer; }
    .btn:hover { background-color: #218838; }
    .btn-danger { background-color: #dc3545; }
    .btn-danger:hover { background-color: #c82333; }
  </style>
</head>
<body>

<div class="container">
  <h1>Verifikasi Pengajuan Surat</h1>

  <?php if ($pengajuan->num_rows > 0): ?>
    <table>
      <thead>
        <tr>
          <th>No</th>
          <th>Nama Warga</th>
          <th>Jenis Surat</th>
          <th>Status</th>
          <th>Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php $no = 1; while ($row = $pengajuan->fetch_assoc()): ?>
          <tr>
            <td><?= $no++ ?></td>
            <td><?= htmlspecialchars($row['nama_anak']) ?></td>
            <td><?= htmlspecialchars($row['jenis_surat']) ?></td>
            <td><?= htmlspecialchars($row['status']) ?></td>
            <td>
              <form method="POST" action="verifikasi_pengajuan.php">
                <input type="hidden" name="id_pengajuan" value="<?= $row['id'] ?>" />
                <button class="btn" type="submit" name="status" value="approved">Setujui</button>
                <button class="btn btn-danger" type="submit" name="status" value="rejected">Tolak</button>
              </form>
            </td>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  <?php else: ?>
    <p>Belum ada pengajuan yang perlu diverifikasi.</p>
  <?php endif; ?>
</div>

</body>
</html>
