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

// 1. DATA DPW
try {
    $dpw = $conn->query("SELECT * FROM profil_dpw WHERE id=1")->fetch(PDO::FETCH_ASSOC);
    $nama_dpw = $dpw['nama_dpw']?? 'BPW Keerom';
    $alamat_dpw = $dpw['alamat']?? 'Jl. Trans Irian, Keerom, Papua';
    $ketua_dpw = $dpw['ketua_dpw']?? 'Ketua BPW';
    $telp_dpw = $dpw['no_telp']?? '-';
    $email_dpw = $dpw['email']?? '-';
} catch (PDOException $e) {
    $nama_dpw = 'DPW Keerom';
    $alamat_dpw = 'Jl. Trans Irian, Keerom, Papua';
    $ketua_dpw = 'Ketua BPW';
    $telp_dpw = '-';
    $email_dpw = '-';
}

// 2. AMBIL DATA LANGSUNG DARI TABEL inventaris_dpw
try {
    $sql = "SELECT * FROM inventaris_dpw ORDER BY nama_barang ASC";
    $data_inventaris = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Tabel inventaris_bpw tidak ditemukan: " . $e->getMessage());
}

$total_barang = count($data_inventaris);
$total_nilai = 0;
foreach($data_inventaris as $inv){
    $harga = $inv['harga_perolehan'] ?? $inv['harga'] ?? $inv['nilai'] ?? 0;
    $jumlah = $inv['jumlah'] ?? $inv['qty'] ?? 1;
    $total_nilai += $harga * $jumlah;
}

ob_end_clean();

// FOLIO LANDSCAPE: 215.9 x 330.2 mm
class MYPDF extends TCPDF {
    public $logo = '../assets/img/logo-gbi.JPG';
    public $nama_dpw = '';
    public $alamat_dpw = '';
    public $telp_dpw = '';
    public $email_dpw = '';

    public function Header() {
        if(file_exists($this->logo)) $this->Image($this->logo, 70, 6, 18, '', 'JPG');
        $this->SetY(6);
        $this->SetFont('helvetica', 'B', 13);
        $this->Cell(0, 5, 'LAPORAN INVENTARIS ASET DPW', 0, 1, 'C');
        $this->SetFont('helvetica', 'B', 11);
        $this->Cell(0, 5, strtoupper($this->nama_dpw), 0, 1, 'C');
        $this->SetFont('helvetica', '', 7);
        $this->Cell(0, 4, $this->alamat_dpw.' | Telp: '.$this->telp_dpw.' | Email: '.$this->email_dpw, 0, 1, 'C');
        $this->SetLineWidth(0.5);
        $this->Line(12, 23, 318, 23);
    }
    public function Footer() {
        $this->SetY(-10);
        $this->SetFont('helvetica', 'I', 7);
        $this->Cell(0, 6, 'Dicetak: '.date('d-m-Y H:i').' | Halaman '.$this->getAliasNumPage().' dari '.$this->getAliasNbPages(), 0, 0, 'C');
    }
}

$pdf = new MYPDF('L', 'mm', array(215.9, 330.2), true, 'UTF-8', false);
$pdf->nama_dpw = $nama_dpw;
$pdf->alamat_dpw = $alamat_dpw;
$pdf->telp_dpw = $telp_dpw;
$pdf->email_dpw = $email_dpw;
$pdf->SetMargins(12, 25, 12);
$pdf->SetAutoPageBreak(TRUE, 12);
$pdf->SetTitle('Laporan Inventaris Aset '.$nama_dpw);
$pdf->AddPage();

$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(0, 6, 'DAFTAR ASET INVENTARIS '.strtoupper($nama_dpw), 0, 1, 'L');
$pdf->SetFont('helvetica', '', 8);
$pdf->Cell(0, 5, 'Total Item: '.$total_barang.' Barang | Total Nilai Aset: Rp '.number_format($total_nilai,0,',','.'), 0, 1, 'L');
$pdf->Ln(1);

// TABEL HEADER - 9 KOLOM
$pdf->SetFont('helvetica', 'B', 7);
$pdf->SetFillColor(230, 230, 230);
$pdf->Cell(8, 7, 'No', 1, 0, 'C', 1);
$pdf->Cell(60, 7, 'Nama Barang', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Kategori', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Jumlah', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Satuan', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Kondisi', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Tgl Perolehan', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Harga Satuan', 1, 0, 'C', 1);
$pdf->Cell(30, 7, 'Total Nilai', 1, 1, 'C', 1);
$pdf->SetFont('helvetica', '', 7);

$no = 1;
foreach($data_inventaris as $inv){
    if($pdf->GetY() > 195){
        $pdf->AddPage();
        $pdf->SetFont('helvetica', 'B', 7);
        $pdf->SetFillColor(230, 230, 230);
        $pdf->Cell(8, 7, 'No', 1, 0, 'C', 1);
        $pdf->Cell(60, 7, 'Nama Barang', 1, 0, 'C', 1);
        $pdf->Cell(30, 7, 'Kategori', 1, 0, 'C', 1);
        $pdf->Cell(30, 7, 'Jumlah', 1, 0, 'C', 1);
        $pdf->Cell(30, 7, 'Satuan', 1, 0, 'C', 1);
        $pdf->Cell(30, 7, 'Kondisi', 1, 0, 'C', 1);
        $pdf->Cell(30, 7, 'Tgl Perolehan', 1, 0, 'C', 1);
        $pdf->Cell(30, 7, 'Harga Satuan', 1, 0, 'C', 1);
        $pdf->Cell(30, 7, 'Total Nilai', 1, 1, 'C', 1);
        $pdf->SetFont('helvetica', '', 7);
    }
    
    $nama_barang = $inv['nama_barang'] ?? $inv['nama'] ?? '-';
    $kategori = $inv['kategori'] ?? $inv['jenis'] ?? '-';
    $jumlah = $inv['jumlah'] ?? $inv['qty'] ?? 1;
    $satuan = $inv['satuan'] ?? $inv['unit'] ?? 'Unit';
    $kondisi = $inv['kondisi'] ?? $inv['status'] ?? 'Baik';
    $tgl_perolehan = ($inv['tgl_perolehan'] ?? $inv['tanggal_beli'] ?? '') != '' && ($inv['tgl_perolehan'] ?? '') != '0000-00-00' ? date('d-m-Y', strtotime($inv['tgl_perolehan'] ?? $inv['tanggal_beli'])) : '-';
    $harga = $inv['harga_perolehan'] ?? $inv['harga'] ?? $inv['nilai'] ?? 0;
    $total = $harga * $jumlah;
    
    $pdf->Cell(8, 6, $no++, 1, 0, 'C');
    $pdf->Cell(60, 6, $nama_barang, 1, 0, 'L');
    $pdf->Cell(30, 6, $kategori, 1, 0, 'C');
    $pdf->Cell(30, 6, $jumlah, 1, 0, 'C');
    $pdf->Cell(30, 6, $satuan, 1, 0, 'C');
    $pdf->Cell(30, 6, $kondisi, 1, 0, 'C');
    $pdf->Cell(30, 6, $tgl_perolehan, 1, 0, 'C');
    $pdf->Cell(30, 6, 'Rp '.number_format($harga,0,',','.'), 1, 0, 'R');
    $pdf->Cell(30, 6, 'Rp '.number_format($total,0,',','.'), 1, 1, 'R');
}

if($pdf->GetY() > 170){
    $pdf->AddPage();
}

$pdf->Ln(10);
$pdf->SetFont('helvetica', '', 9);
$pdf->Cell(220, 5, '', 0, 0);
$pdf->Cell(70, 5, 'Keerom, '.date('d F Y'), 0, 1, 'C');
$pdf->Cell(220, 5, '', 0, 0);
$pdf->Cell(70, 5, 'Ketua BPW,', 0, 1, 'C');
$pdf->Ln(18);
$pdf->Cell(220, 5, '', 0, 0);
$pdf->SetFont('helvetica', 'B, U', 9);
$pdf->Cell(70, 5, $ketua_dpw, 0, 1, 'C');

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