<?php
ob_start();
require_once '../config/auth.php';
cek_role('gembala');
require_once '../vendor/autoload.php';

$gereja_id = $_SESSION['gereja_id'];
$user_id = $_SESSION['user_id'];

// DATA GEREJA
$gereja = $conn->prepare("SELECT * FROM gereja WHERE id =?");
$gereja->execute([$gereja_id]);
$info_gereja = $gereja->fetch(PDO::FETCH_ASSOC);
$nama_gereja = $info_gereja['nama_gereja']?? 'GBI';
$alamat_gereja = $info_gereja['alamat']?? '-';
$telp_gereja = $info_gereja['no_telepon']?? '-';
$email_gereja = $info_gereja['email']?? '-';

// DATA GEMBALA
$gembala = $conn->prepare("SELECT * FROM users WHERE id =?");
$gembala->execute([$user_id]);
$data_user = $gembala->fetch(PDO::FETCH_ASSOC);
$nama_gembala = $data_user['nama_lengkap']?? $data_user['nama']?? $data_user['username']?? 'Gembala';

// REKAP 4 MINGGU TERAKHIR
$rekap = $conn->prepare("SELECT
    tanggal,
    jumlah_hadir,
    jumlah_laki,
    jumlah_perempuan,
    jumlah_anak,
    keterangan
    FROM absensi
    WHERE gereja_id =?
    ORDER BY tanggal DESC LIMIT 12");
$rekap->execute([$gereja_id]);

// TOTAL RATA-RATA
$avg = $conn->prepare("SELECT
    ROUND(AVG(jumlah_hadir)) as rata_hadir,
    ROUND(AVG(jumlah_laki)) as rata_laki,
    ROUND(AVG(jumlah_perempuan)) as rata_perempuan,
    ROUND(AVG(jumlah_anak)) as rata_anak
    FROM absensi WHERE gereja_id =?");
$avg->execute([$gereja_id]);
$data_avg = $avg->fetch(PDO::FETCH_ASSOC);

ob_end_clean();

class MYPDF extends TCPDF {
    public $logo_gbi = '../assets/img/logo-gbi.JPG';
    public $nama_gereja = '';
    public $alamat_gereja = '';
    public $telp_gereja = '';
    public $email_gereja = '';

    public function Header() {
        if(file_exists($this->logo_gbi)){
            $this->Image($this->logo_gbi, 15, 8, 25, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
        }
        $this->SetY(10);
        $this->SetFont('helvetica', 'B', 14);
        $this->Cell(0, 6, 'GEREJA BETHEL INDONESIA', 0, 1, 'C');
        $this->SetFont('helvetica', 'B', 16);
        $this->Cell(0, 7, strtoupper($this->nama_gereja), 0, 1, 'C');
        $this->SetFont('helvetica', '', 10);
        $this->Cell(0, 5, $this->alamat_gereja, 0, 1, 'C');
        $this->Cell(0, 5, 'Telp: '.$this->telp_gereja.' | Email: '.$this->email_gereja, 0, 1, 'C');
        $this->SetLineWidth(0.8);
        $this->Line(15, 35, 195, 35);
        $this->SetLineWidth(0.2);
        $this->Line(15, 36, 195, 36);
        $this->Ln(8);
    }
}

$pdf = new MYPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->nama_gereja = $nama_gereja;
$pdf->alamat_gereja = $alamat_gereja;
$pdf->telp_gereja = $telp_gereja;
$pdf->email_gereja = $email_gereja;
$pdf->SetCreator('SIMJEDPW');
$pdf->SetAuthor('SIMJEDPW');
$pdf->SetTitle('Laporan Absensi');
$pdf->SetMargins(15, 40, 15);
$pdf->SetAutoPageBreak(TRUE, 30);
$pdf->AddPage();

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(0, 8, 'LAPORAN ABSENSI IBADAH', 0, 1, 'C');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 5, 'Per Tanggal: '. date('d F Y'), 0, 1, 'C');
$pdf->Ln(3);

// A. RATA-RATA KEHADIRAN
$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(0, 7, 'A. RATA-RATA KEHADIRAN', 0, 1, 'L');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(50, 6, 'Rata-rata Total Hadir', 1, 0, 'L');
$pdf->Cell(30, 6, ($data_avg['rata_hadir']??0).' Jiwa', 1, 1, 'C');
$pdf->Cell(50, 6, 'Rata-rata Laki-laki', 1, 0, 'L');
$pdf->Cell(30, 6, ($data_avg['rata_laki']??0).' Jiwa', 1, 1, 'C');
$pdf->Cell(50, 6, 'Rata-rata Perempuan', 1, 0, 'L');
$pdf->Cell(30, 6, ($data_avg['rata_perempuan']??0).' Jiwa', 1, 1, 'C');
$pdf->Cell(50, 6, 'Rata-rata Anak-anak', 1, 0, 'L');
$pdf->Cell(30, 6, ($data_avg['rata_anak']??0).' Jiwa', 1, 1, 'C');
$pdf->Ln(5);

// B. DETAIL 12 IBADAH TERAKHIR
$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(0, 7, 'B. DETAIL 12 IBADAH TERAKHIR', 0, 1, 'L');
$pdf->SetFont('helvetica', 'B', 8);
$pdf->Cell(10, 6, 'No', 1, 0, 'C');
$pdf->Cell(30, 6, 'Tanggal', 1, 0, 'C');
$pdf->Cell(25, 6, 'Total Hadir', 1, 0, 'C');
$pdf->Cell(20, 6, 'Laki', 1, 0, 'C');
$pdf->Cell(25, 6, 'Perempuan', 1, 0, 'C');
$pdf->Cell(20, 6, 'Anak', 1, 0, 'C');
$pdf->Cell(50, 6, 'Keterangan', 1, 1, 'C');

$pdf->SetFont('helvetica', '', 8);
$no = 1;
if($rekap->rowCount() > 0){
    while($r = $rekap->fetch(PDO::FETCH_ASSOC)){
        if($pdf->GetY() > 250){
            $pdf->AddPage();
            $pdf->SetFont('helvetica', 'B', 8);
            $pdf->Cell(10, 6, 'No', 1, 0, 'C');
            $pdf->Cell(30, 6, 'Tanggal', 1, 0, 'C');
            $pdf->Cell(25, 6, 'Total Hadir', 1, 0, 'C');
            $pdf->Cell(20, 6, 'Laki', 1, 0, 'C');
            $pdf->Cell(25, 6, 'Perempuan', 1, 0, 'C');
            $pdf->Cell(20, 6, 'Anak', 1, 0, 'C');
            $pdf->Cell(50, 6, 'Keterangan', 1, 1, 'C');
            $pdf->SetFont('helvetica', '', 8);
        }
        $pdf->Cell(10, 5, $no++, 1, 0, 'C');
        $pdf->Cell(30, 5, date('d/m/Y',strtotime($r['tanggal'])), 1, 0, 'C');
        $pdf->Cell(25, 5, $r['jumlah_hadir'], 1, 0, 'C');
        $pdf->Cell(20, 5, $r['jumlah_laki'], 1, 0, 'C');
        $pdf->Cell(25, 5, $r['jumlah_perempuan'], 1, 0, 'C');
        $pdf->Cell(20, 5, $r['jumlah_anak'], 1, 0, 'C');
        $pdf->Cell(50, 5, $r['keterangan']?:'-', 1, 1, 'L');
    }
} else {
    $pdf->Cell(180, 5, 'BELUM ADA DATA ABSENSI', 1, 1, 'C');
}

// TTD GEMBALA
$pdf->Ln(15);
$tgl_cetak = date('d F Y');
$kota = explode(',', $alamat_gereja);
$kota = trim($kota[0]?? 'Kota');

$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(110, 5, '', 0, 0);
$pdf->Cell(70, 5, $kota.', '.$tgl_cetak, 0, 1, 'C');
$pdf->Cell(110, 5, '', 0, 0);
$pdf->Cell(70, 5, 'Gembala Sidang,', 0, 1, 'C');
$pdf->Ln(20);
$pdf->Cell(110, 5, '', 0, 0);
$pdf->SetFont('helvetica', 'B, U', 10);
$pdf->Cell(70, 5, $nama_gembala, 0, 1, 'C');

$pdf->Output('Laporan_Absensi_'.$nama_gereja.'_'.date('Ymd').'.pdf', 'I');
exit;
?>