<?php
require_once("../../assets/lib/fpdf/fpdf.php");
require_once("../../config/koneksi.php");

class PDF extends FPDF
{
    function Header()
    {
        $this->Image('../../assets/img/NH.png', 20, 5, 30);

        $this->SetFont('Times', 'B', 15);
        $this->Cell(308, 8, 'PENDATAAN WARGA SILAT', 0, 1, 'C');
        $this->Cell(308, 8, 'NH PERKASYA CAB. BLITAR', 0, 1, 'C');
        $this->Cell(308, 8, 'KEL. NGLEGOK KEC. NGLEGOK KAB. BLITAR', 0, 1, 'C');
        $this->Ln(5);

        $this->SetFont('Times', 'BU', 12);
        for ($i = 0; $i < 10; $i++) {
            $this->Cell(308, 0, '', 1, 1, 'C');
        }

        $this->Ln(1);
        $this->Cell(308, 8, 'LAPORAN DATA WARGA SILAT', 0, 1, 'C');
        $this->Ln(2);
        $this->SetFont('Times', 'B', 9.5);

        // Header tabel
        $this->cell(8, 7, 'NO.', 1, 0, 'C');
        $this->cell(20, 7, 'FOTO', 1, 0, 'C');
        $this->cell(40, 7, 'NAMA', 1, 0, 'C');
        $this->cell(30, 7, 'TEMPAT LHR', 1, 0, 'C');
        $this->cell(25, 7, 'TGL. LHR', 1, 0, 'C');
        $this->cell(8, 7, 'JK', 1, 0, 'C');
        $this->cell(35, 7, 'NAMA ORANG TUA', 1, 0, 'C');
        $this->cell(20, 7, 'AGAMA', 1, 0, 'C');
        $this->cell(16, 7, 'PDDKN', 1, 0, 'C');
        $this->cell(30, 7, 'ASAL RANTING', 1, 0, 'C');
        $this->cell(47, 7, 'ALAMAT', 1, 1, 'C');
    }

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }
}

// Ambil data warga dari database
$query = "SELECT *, TIMESTAMPDIFF(YEAR, `tanggal_lahir_warga`, CURDATE()) AS usia_warga FROM warga";
$hasil = mysqli_query($db, $query);
$data_warga = array();
while ($row = mysqli_fetch_assoc($hasil)) {
    $data_warga[] = $row;
}

// Buat PDF
$pdf = new PDF('L', 'mm', [210, 330]);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times', '', 9);

// Penomoran
$nomor = 1;

foreach ($data_warga as $warga) {
    $pdf->cell(8, 20, $nomor++ . '.', 1, 0, 'C');

    // Path gambar (Gunakan absolute path)
    $fotoPath = __DIR__ . '/../../uploads/' . $warga['foto'];

    // Simpan posisi sebelum gambar
    $x = $pdf->GetX();
    $y = $pdf->GetY();

    // Cek apakah file gambar ada
    if (!empty($warga['foto']) && file_exists($fotoPath)) {
        // Buat sel kosong untuk gambar
        $pdf->cell(20, 20, '', 1, 0, 'C');
        // Tampilkan gambar di dalam sel dengan padding
        $pdf->Image($fotoPath, $x + 2, $y + 2, 16, 16);
    } else {
        // Jika tidak ada gambar, tampilkan teks "No Img"
        $pdf->cell(20, 20, 'No Img', 1, 0, 'C');
    }

    $pdf->cell(40, 20, substr(strtoupper($warga['nama_warga']), 0, 17), 1, 0, 'C');
    $pdf->cell(30, 20, strtoupper($warga['tempat_lahir_warga']), 1, 0, 'C');
    $pdf->cell(25, 20, ($warga['tanggal_lahir_warga'] != '0000-00-00') ? date('d-m-Y', strtotime($warga['tanggal_lahir_warga'])) : '', 1, 0, 'C');
    $pdf->cell(8, 20, substr(strtoupper($warga['jenis_kelamin_warga']), 0, 1), 1, 0, 'C');
    $pdf->cell(35, 20, strtoupper($warga['nama_orang_tua']), 1, 0, 'C');
    $pdf->cell(20, 20, strtoupper($warga['agama_warga']), 1, 0, 'C');
    $pdf->cell(16, 20, strtoupper($warga['pendidikan_terakhir_warga']), 1, 0, 'C');
    $pdf->cell(30, 20, strtoupper($warga['asal_ranting']), 1, 0, 'C');
    $cellContent = $warga['desa_kelurahan_warga'] . ', ' . $warga['rt_warga'] .'/'. $warga['rw_warga'] .', '. $warga['kecamatan_warga'] . ', ' . $warga['kabupaten_kota_warga'] . ', '. $warga['provinsi_warga'] . ', ' . $warga['negara_warga'];
    $pdf->MultiCell(47, 10, $cellContent, 1, 'C');
}

// Output PDF
$pdf->Output();
?>
