<?php
session_start();
require_once '../config/database.php';

// Fix path TCPDF
require_once '../vendor/autoload.php'; // Kalau pake Composer
// require_once '../vendor/tcpdf/tcpdf.php'; // Kalau manual

if(!isset($_SESSION['user_id']) || $_SESSION['role']!= 'bendahara'){
    header("Location:../login.php");
    exit;
}

$db = new Database();
$conn = $db->getConnection();

$gereja_id = $_SESSION['gereja_id'];
$nama_gereja = $_SESSION['nama_gereja'];
$nama_bendahara = $_SESSION['nama'];

// Filter bulan & tahun
$bulan = $_GET['bulan']?? date('m');
$tahun = $_GET['tahun']?? date('Y');
$periode = "$tahun-$bulan";

// Ambil data keuangan - pakai kolom 'tipe' dan 'jenis'
$q_data = $conn->prepare("SELECT * FROM keuangan
                          WHERE gereja_id =? AND DATE_FORMAT(tanggal, '%Y-%m') =?
                          ORDER BY tanggal ASC, id ASC");
$q_data->execute([$gereja_id, $periode]);
$data = $q_data->fetchAll(PDO::FETCH_ASSOC);

// Hitung total
$total_pemasukan = 0;
$total_pengeluaran = 0;
foreach($data as $d){
    if($d['tipe'] == 'Pemasukan'){
        $total_pemasukan += $d['jumlah'];
    } else {
        $total_pengeluaran += $d['jumlah'];
    }
}
$saldo = $total_pemasukan - $total_pengeluaran;

// Buat PDF
class MYPDF extends TCPDF {
    public function Header() {
        $this->SetFont('helvetica', 'B', 14);
        $this->Cell(0, 10, 'LAPORAN KEUANGAN GEREJA', 0, 1, 'C');
        $this->Ln(5);
    }
    public function Footer() {
        $this->SetY(-15);
        $this->SetFont('helvetica', 'I', 8);
        $this->Cell(0, 10, 'Halaman '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, 0, 'C');
    }
}

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator('SIMJEDPW');
$pdf->SetAuthor($nama_bendahara);
$pdf->SetTitle('Laporan Keuangan');
$pdf->SetMargins(15, 25, 15);
$pdf->AddPage();

// Header Info
$pdf->SetFont('helvetica', '', 11);
$pdf->Cell(0, 6, 'Nama Gereja: '. $nama_gereja, 0, 1, 'L');
$pdf->Cell(0, 6, 'Periode: '. date('F Y', strtotime($periode.'-01')), 0, 1, 'L');
$pdf->Cell(0, 6, 'Bendahara: '. $nama_bendahara, 0, 1, 'L');
$pdf->Ln(5);

// Tabel
$pdf->SetFont('helvetica', 'B', 10);
$pdf->SetFillColor(230, 230, 230);
$pdf->Cell(10, 7, 'No', 1, 0, 'C', 1);
$pdf->Cell(25, 7, 'Tanggal', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Tipe', 1, 0, 'C', 1);
$pdf->Cell(35, 7, 'Jenis', 1, 0, 'C', 1);
$pdf->Cell(55, 7, 'Keterangan', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Jumlah', 1, 1, 'C', 1);

$pdf->SetFont('helvetica', '', 9);
$no = 1;
foreach($data as $d){
    $pdf->Cell(10, 6, $no++, 1, 0, 'C');
    $pdf->Cell(25, 6, date('d/m/Y', strtotime($d['tanggal'])), 1, 0, 'C');
    $pdf->Cell(30, 6, $d['tipe'], 1, 0, 'C');
    $pdf->Cell(35, 6, $d['jenis'], 1, 0, 'L');
    $pdf->Cell(55, 6, $d['keterangan'], 1, 0, 'L');
    $pdf->Cell(30, 6, 'Rp '.number_format($d['jumlah'],0,',','.'), 1, 1, 'R');
}

// Total
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(155, 7, 'Total Pemasukan', 1, 0, 'R', 1);
$pdf->Cell(30, 7, 'Rp '.number_format($total_pemasukan,0,',','.'), 1, 1, 'R', 1);
$pdf->Cell(155, 7, 'Total Pengeluaran', 1, 0, 'R', 1);
$pdf->Cell(30, 7, 'Rp '.number_format($total_pengeluaran,0,',','.'), 1, 1, 'R', 1);
$pdf->Cell(155, 7, 'SALDO AKHIR', 1, 0, 'R', 1);
$pdf->Cell(30, 7, 'Rp '.number_format($saldo,0,',','.'), 1, 1, 'R', 1);

// TTD
$pdf->Ln(10);
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(95, 6, '', 0, 0);
$pdf->Cell(90, 6, 'Keerom, '.date('d F Y'), 0, 1, 'C');
$pdf->Cell(95, 6, '', 0, 0);
$pdf->Cell(90, 6, 'Bendahara,', 0, 1, 'C');
$pdf->Ln(15);
$pdf->Cell(95, 6, '', 0, 0);
$pdf->Cell(90, 6, '( '.$nama_bendahara.' )', 0, 1, 'C');

$pdf->Output('Laporan_Keuangan_'.$bulan.'_'.$tahun.'.pdf', 'I');
?>