<?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'];

// 1. 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_telp']?? $info_gereja['telp']?? $info_gereja['telepon']?? '-';
$email_gereja = $info_gereja['email']?? '-';

// 2. 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';

// 3. REKAP TOTAL PER KATEGORI + KONDISI
$rekap = $conn->prepare("SELECT
    kategori,
    COUNT(*) as total_item,
    SUM(jumlah) as total_jumlah,
    SUM(CASE WHEN kondisi = 'Baik' THEN jumlah ELSE 0 END) as jml_baik,
    SUM(CASE WHEN kondisi = 'Rusak Ringan' THEN jumlah ELSE 0 END) as jml_rusak_ringan,
    SUM(CASE WHEN kondisi = 'Rusak Berat' THEN jumlah ELSE 0 END) as jml_rusak_berat
    FROM inventaris
    WHERE gereja_id =?
    GROUP BY kategori
    ORDER BY kategori ASC");
$rekap->execute([$gereja_id]);

// 4. TOTAL KESELURUHAN
$total_all = $conn->prepare("SELECT
    COUNT(*) as total_item,
    SUM(jumlah) as total_jumlah,
    SUM(CASE WHEN kondisi = 'Baik' THEN jumlah ELSE 0 END) as jml_baik,
    SUM(CASE WHEN kondisi = 'Rusak Ringan' THEN jumlah ELSE 0 END) as jml_rusak_ringan,
    SUM(CASE WHEN kondisi = 'Rusak Berat' THEN jumlah ELSE 0 END) as jml_rusak_berat
    FROM inventaris WHERE gereja_id =?");
$total_all->execute([$gereja_id]);
$data_total = $total_all->fetch(PDO::FETCH_ASSOC);

// 5. DETAIL BARANG
$list_barang = $conn->prepare("SELECT * FROM inventaris
    WHERE gereja_id =?
    ORDER BY kategori ASC, nama_barang ASC");
$list_barang->execute([$gereja_id]);

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 Inventaris');
$pdf->SetMargins(15, 40, 15);
$pdf->SetAutoPageBreak(TRUE, 30);
$pdf->AddPage();

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

// A. REKAP PER KATEGORI
$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(0, 7, 'A. REKAPITULASI PER KATEGORI', 0, 1, 'L');
$pdf->SetFont('helvetica', 'B', 8);
$pdf->Cell(40, 6, 'Kategori', 1, 0, 'C');
$pdf->Cell(25, 6, 'Total Item', 1, 0, 'C');
$pdf->Cell(25, 6, 'Total Jumlah', 1, 0, 'C');
$pdf->Cell(25, 6, 'Baik', 1, 0, 'C');
$pdf->Cell(30, 6, 'Rusak Ringan', 1, 0, 'C');
$pdf->Cell(30, 6, 'Rusak Berat', 1, 1, 'C');

$pdf->SetFont('helvetica', '', 8);
if($rekap->rowCount() > 0){
    while($r = $rekap->fetch(PDO::FETCH_ASSOC)){
        $pdf->Cell(40, 5, $r['kategori'], 1, 0, 'L');
        $pdf->Cell(25, 5, $r['total_item'], 1, 0, 'C');
        $pdf->Cell(25, 5, $r['total_jumlah'], 1, 0, 'C');
        $pdf->Cell(25, 5, $r['jml_baik'], 1, 0, 'C');
        $pdf->Cell(30, 5, $r['jml_rusak_ringan'], 1, 0, 'C');
        $pdf->Cell(30, 5, $r['jml_rusak_berat'], 1, 1, 'C');
    }
} else {
    $pdf->Cell(175, 5, 'BELUM ADA DATA INVENTARIS', 1, 1, 'C');
}

// TOTAL KESELURUHAN
$pdf->SetFont('helvetica', 'B', 8);
$pdf->Cell(40, 6, 'TOTAL KESELURUHAN', 1, 0, 'R');
$pdf->Cell(25, 6, $data_total['total_item']?? 0, 1, 0, 'C');
$pdf->Cell(25, 6, $data_total['total_jumlah']?? 0, 1, 0, 'C');
$pdf->Cell(25, 6, $data_total['jml_baik']?? 0, 1, 0, 'C');
$pdf->Cell(30, 6, $data_total['jml_rusak_ringan']?? 0, 1, 0, 'C');
$pdf->Cell(30, 6, $data_total['jml_rusak_berat']?? 0, 1, 1, 'C');
$pdf->Ln(5);

// B. DETAIL BARANG
$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(0, 7, 'B. DAFTAR DETAIL INVENTARIS', 0, 1, 'L');
$pdf->SetFont('helvetica', 'B', 7);
$pdf->Cell(8, 6, 'No', 1, 0, 'C');
$pdf->Cell(45, 6, 'Nama Barang', 1, 0, 'C');
$pdf->Cell(30, 6, 'Kategori', 1, 0, 'C');
$pdf->Cell(15, 6, 'Jumlah', 1, 0, 'C');
$pdf->Cell(25, 6, 'Kondisi', 1, 0, 'C');
$pdf->Cell(15, 6, 'Tahun', 1, 0, 'C');
$pdf->Cell(20, 6, 'Tgl Beli', 1, 0, 'C');
$pdf->Cell(22, 6, 'Keterangan', 1, 1, 'C');

$pdf->SetFont('helvetica', '', 7);
$no = 1;
if($list_barang->rowCount() > 0){
    while($b = $list_barang->fetch(PDO::FETCH_ASSOC)){
        if($pdf->GetY() > 250){
            $pdf->AddPage();
            $pdf->SetFont('helvetica', 'B', 7);
            $pdf->Cell(8, 6, 'No', 1, 0, 'C');
            $pdf->Cell(45, 6, 'Nama Barang', 1, 0, 'C');
            $pdf->Cell(30, 6, 'Kategori', 1, 0, 'C');
            $pdf->Cell(15, 6, 'Jumlah', 1, 0, 'C');
            $pdf->Cell(25, 6, 'Kondisi', 1, 0, 'C');
            $pdf->Cell(15, 6, 'Tahun', 1, 0, 'C');
            $pdf->Cell(20, 6, 'Tgl Beli', 1, 0, 'C');
            $pdf->Cell(22, 6, 'Keterangan', 1, 1, 'C');
            $pdf->SetFont('helvetica', '', 7);
        }
        $tgl_beli = $b['tgl_beli']? date('d/m/Y', strtotime($b['tgl_beli'])) : '-';
        $pdf->Cell(8, 5, $no++, 1, 0, 'C');
        $pdf->Cell(45, 5, $b['nama_barang'], 1, 0, 'L');
        $pdf->Cell(30, 5, $b['kategori'], 1, 0, 'L');
        $pdf->Cell(15, 5, $b['jumlah'], 1, 0, 'C');
        $pdf->Cell(25, 5, $b['kondisi'], 1, 0, 'C');
        $pdf->Cell(15, 5, $b['tahun']?? '-', 1, 0, 'C');
        $pdf->Cell(20, 5, $tgl_beli, 1, 0, 'C');
        $pdf->Cell(22, 5, $b['keterangan']?? '-', 1, 1, 'L');
    }
} else {
    $pdf->Cell(180, 5, 'BELUM ADA DATA INVENTARIS', 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_Inventaris_'.$nama_gereja.'_'.date('Ymd').'.pdf', 'I');
exit;
?>