<?php
require_once __DIR__ . '/../../config/session.php';
requireAdmin();
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../functions/helper.php';
require_once __DIR__ . '/../../vendor/autoload.php';

$db = getDB();

$bulan = $_GET['bulan'] ?? '';
$tahun = $_GET['tahun'] ?? '';

// Check if filter is applied
$filter_applied = !empty($bulan) && !empty($tahun);

if ($filter_applied) {
    $stmt = $db->prepare("SELECT ih.nik, ih.nama_lengkap, ih.hpl, ih.status, u.nama_lengkap as nama_bidan, ih.hpht, ih.alamat FROM ibu_hamil ih LEFT JOIN users u ON ih.bidan_id = u.id WHERE MONTH(ih.tanggal_daftar) = ? AND YEAR(ih.tanggal_daftar) = ? ORDER BY ih.tanggal_daftar DESC");
    $stmt->execute([$bulan, $tahun]);
} else {
    $stmt = $db->query("SELECT ih.nik, ih.nama_lengkap, ih.hpl, ih.status, u.nama_lengkap as nama_bidan, ih.hpht, ih.alamat FROM ibu_hamil ih LEFT JOIN users u ON ih.bidan_id = u.id ORDER BY ih.tanggal_daftar DESC");
}
$data = $stmt->fetchAll();

// Extend FPDF
class PDF extends FPDF {
    function Header() {
        // Logo jika ada (opsional)
        // $this->Image('../../assets/img/logo_posyandu.png',10,6,30);
        $this->SetFont('Arial','B',15);
        $this->Cell(0,8,'POSYANDU TUNAS BANGSA',0,1,'C');
        $this->SetFont('Arial','',11);
        $this->Cell(0,5,'Desa Mentawak Ulu, Kec. Air Hitam, Kab. Merangin',0,1,'C');
        $this->SetLineWidth(0.5);
        $this->Line(10, 25, 200, 25);
        $this->Ln(10);
    }

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

$pdf = new PDF('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();

$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,10,'Laporan Pendaftaran Ibu Hamil ' . ($filter_applied ? 'Bulan ' . formatBulan($bulan) . ' ' . $tahun : 'Semua Data'),0,1,'C');
$pdf->Ln(5);

// Header Tabel
$pdf->SetFont('Arial','B',10);
$pdf->SetFillColor(220, 220, 220);
$pdf->Cell(10, 8, 'No', 1, 0, 'C', true);
$pdf->Cell(35, 8, 'NIK', 1, 0, 'C', true);
$pdf->Cell(50, 8, 'Nama Lengkap', 1, 0, 'C', true);
$pdf->Cell(25, 8, 'Usia Hamil', 1, 0, 'C', true);
$pdf->Cell(25, 8, 'HPL', 1, 0, 'C', true);
$pdf->Cell(45, 8, 'Bidan PJ', 1, 1, 'C', true);

// Isi Tabel
$pdf->SetFont('Arial','',9);
$no = 1;
if(count($data) > 0) {
    foreach($data as $row) {
        $diff = (new DateTime($row['hpht']))->diff(new DateTime())->days;
        $minggu = intdiv($diff, 7);
        
        $pdf->Cell(10, 7, $no++, 1, 0, 'C');
        $pdf->Cell(35, 7, $row['nik'], 1, 0, 'C');
        $pdf->Cell(50, 7, substr($row['nama_lengkap'], 0, 25), 1, 0, 'L');
        $pdf->Cell(25, 7, $minggu . ' Mgg', 1, 0, 'C');
        $pdf->Cell(25, 7, date('d-m-Y', strtotime($row['hpl'])), 1, 0, 'C');
        $pdf->Cell(45, 7, substr($row['nama_bidan'], 0, 22), 1, 1, 'L');
    }
} else {
    $pdf->Cell(190, 10, 'Tidak ada data', 1, 1, 'C');
}

$pdf->Ln(20);
$pdf->SetFont('Arial','',10);
$pdf->Cell(130);
$pdf->Cell(60, 5, 'Mentawak Ulu, ' . date('d ') . formatBulan(date('m')) . ' ' . date('Y'), 0, 1, 'C');
$pdf->Cell(130);
$pdf->Cell(60, 5, 'Bidan Penanggung Jawab', 0, 1, 'C');
$pdf->Ln(20);
$pdf->Cell(130);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(60, 5, escape($_SESSION['nama']), 0, 1, 'C');
$pdf->SetFont('Arial','',10);
$pdf->Cell(130);
$pdf->Cell(60, 5, 'NIP. .............................', 0, 1, 'C');

$pdf->Output('I', 'Laporan_Ibu_Hamil_'.($filter_applied ? $bulan.'_'.$tahun : 'Semua').'.pdf');
