<?php

require "plugins/fpdf/fpdf.php";

// Buat folder jika belum ada
if (!is_dir('pdf')) mkdir('pdf');
if (!is_dir('zip')) mkdir('zip');

// Data contoh untuk PDF
$data_list = [
    ['nama' => 'Andi', 'tagihan' => 100000],
    ['nama' => 'Budi', 'tagihan' => 150000],
    ['nama' => 'Citra', 'tagihan' => 200000],
];

// Generate PDF per orang
foreach ($data_list as $i => $data) {
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 14);
    $pdf->Cell(0, 10, 'Bukti Pembayaran PBB', 0, 1, 'C');
    $pdf->SetFont('Arial', '', 12);
    $pdf->Cell(0, 10, 'Nama: ' . $data['nama'], 0, 1);
    $pdf->Cell(0, 10, 'Tagihan: Rp ' . number_format($data['tagihan'], 0, ',', '.'), 0, 1);

    $filename = 'pdf/' . $data['nama'] . '.pdf';
    $pdf->Output('F', $filename);
}

// ZIP semua file PDF
$zip = new ZipArchive();
$zip_filename = 'zip/bukti-pembayaran.zip';

if ($zip->open($zip_filename, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
    foreach (glob('pdf/*.pdf') as $file) {
        $zip->addFile($file, basename($file)); // nama dalam ZIP
    }
    $zip->close();
    echo "✅ ZIP berhasil dibuat: <a href='$zip_filename' download>Download ZIP</a>";
} else {
    echo "❌ Gagal membuat ZIP";
}
