<?php

require_once(APPPATH.'third_party/fpdf/fpdf.php');

class PDF extends FPDF
{
    private $nama;
    private $alamat;
    private $telepon;
    private $logo;

    private $isTable   = false;
    private $tableWidth  = [];
    private $tableHeader = [];

    // Lebar kertas A4 Portrait = 210mm, margin kiri+kanan = 10+10 = 20mm
    // Area cetak efektif = 190mm
    private $pageWidth = 190;

    // ----------------------------------------------------------------
    // SETTER
    // ----------------------------------------------------------------

    public function setHeaderData($nama, $alamat, $telepon, $logo = '')
    {
        $this->nama    = $nama;
        $this->alamat  = $alamat;
        $this->telepon = $telepon;
        $this->logo    = $logo;
    }

    public function setTableHeader($header, $width)
    {
        $this->tableHeader = $header;
        $this->tableWidth  = $width;
    }

    // ----------------------------------------------------------------
    // HEADER HALAMAN
    // ----------------------------------------------------------------

    function Header()
    {
        // Logo (opsional)
        if (!empty($this->logo) && file_exists($this->logo)) {
            $this->Image($this->logo, 10, 8, 18);
            $this->SetXY(31, 10);
        } else {
            $this->SetXY(10, 10);
        }

        // Nama perusahaan
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(0, 6, $this->nama, 0, 1, 'L');

        // Alamat
        $this->SetFont('Arial', '', 9);
        $x = !empty($this->logo) && file_exists($this->logo) ? 31 : 10;
        $this->SetX($x);
        $this->Cell(0, 5, $this->alamat, 0, 1, 'L');

        $this->SetX($x);
        $this->Cell(0, 5, 'Telp : ' . $this->telepon, 0, 1, 'L');

        // Garis pemisah header (A4 Portrait: dari x=10 s/d x=200)
        $this->Ln(2);
        $this->SetLineWidth(0.6);
        $this->Line(10, $this->GetY(), 200, $this->GetY());
        $this->SetLineWidth(0.2);
        $this->Line(10, $this->GetY() + 1, 200, $this->GetY() + 1);

        $this->Ln(5);

        // Judul laporan
        $this->SetFont('Arial', 'B', 14);
        $this->SetTextColor(245, 140, 0);
        $this->Cell(0, 8, 'LAPORAN DATA BARANG', 0, 1, 'C');
        $this->SetTextColor(0, 0, 0);

        // Tanggal cetak
        $this->SetFont('Arial', '', 9);
        $this->Cell(0, 5, 'DIcetak Pada : ' . date('d-m-Y') . '   -  ' . date('H:i:s'), 0, 1, 'C');

        $this->Ln(3);

        // Header tabel (muncul otomatis saat ganti halaman)
        if ($this->isTable && !empty($this->tableHeader)) {
            $this->_drawTableHeader();
        }
    }

    // ----------------------------------------------------------------
    // FOOTER HALAMAN
    // ----------------------------------------------------------------

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

    // ----------------------------------------------------------------
    // HELPER: Gambar baris header tabel
    // ----------------------------------------------------------------

    private function _drawTableHeader()
    {
        $this->SetFillColor(245, 140, 0);
        $this->SetTextColor(255, 255, 255);
        $this->SetFont('Arial', 'B', 8);

        foreach ($this->tableHeader as $k => $v) {
            $this->Cell($this->tableWidth[$k], 7, $v, 1, 0, 'C', true);
        }

        $this->SetTextColor(0, 0, 0);
        $this->Ln();
    }

    // ----------------------------------------------------------------
    // TABEL DATA BARANG
    // ----------------------------------------------------------------

    public function ItemsTable($data)
    {
        /*
         * Total lebar kolom = 190mm (pas A4 Portrait, margin 10mm kiri & kanan)
         *
         *  NO          :   8
         *  TGL BELI    :  22
         *  KODE        :  22
         *  NAMA BARANG :  48  ← kolom terpanjang
         *  GUDANG      :  35
         *  STOK        :  13
         *  HARGA BELI  :  21
         *  HARGA JUAL  :  21
         *                ----
         *  TOTAL       : 190
         */
        $w = [8, 22, 22, 48, 35, 13, 21, 21];

        $header = [
            'NO',
            'TGL BELI',
            'KODE',
            'NAMA BARANG',
            'GUDANG',
            'STOK',
            'HARGA BELI',
            'HARGA JUAL',
        ];

        // Daftarkan SEBELUM AddPage() agar Header() sudah tahu isTable=true
        // ketika halaman berikutnya dibuat (termasuk halaman ke-2, 3, dst.)
        $this->isTable = true;
        $this->setTableHeader($header, $w);

        // Gambar header tabel di halaman PERTAMA secara eksplisit,
        // karena saat AddPage() awal dipanggil isTable masih false.
        $this->_drawTableHeader();

        // ---- Baris data ----
        $this->SetFont('Arial', '', 8);
        $no = 1;

        foreach ($data as $row) {

            // Ganti halaman sebelum batas bawah (A4 Portrait: tinggi efektif ~267mm)
            if ($this->GetY() > 262) {
                $this->AddPage('P', 'A4');
                // _drawTableHeader() sudah dipanggil otomatis oleh Header()
            }

            // Warna baris selang-seling
            if ($no % 2 === 0) {
                $this->SetFillColor(255, 245, 230);
                $fill = true;
            } else {
                $fill = false;
            }

            $this->Cell($w[0], 6, $no++,                                          1, 0, 'C', $fill);
            $this->Cell($w[1], 6, date('d-m-Y', strtotime($row->tanggal_pembelian)), 1, 0, 'C', $fill);
            $this->Cell($w[2], 6, $row->kode_detail,                               1, 0, 'C', $fill);
            $this->Cell($w[3], 6, mb_substr($row->nama_barang, 0, 45),             1, 0, 'L', $fill);
            $this->Cell($w[4], 6, mb_substr($row->gudang ?: '-', 0, 30),           1, 0, 'L', $fill);
            $this->Cell($w[5], 6, number_format($row->stok),                       1, 0, 'R', $fill);
            $this->Cell($w[6], 6, number_format($row->harga_beli, 0, ',', '.'),    1, 0, 'R', $fill);
            $this->Cell($w[7], 6, number_format($row->harga_jual, 0, ',', '.'),    1, 1, 'R', $fill);
        }

        $this->isTable = false;
    }

    // ----------------------------------------------------------------
    // RINGKASAN & TANDA TANGAN
    // ----------------------------------------------------------------

    public function TotalBarang($jumlah)
    {
        $this->Ln(4);
        $this->SetFont('Arial', 'B', 10);
        $this->Cell(0, 7, 'Total Data Barang : ' . $jumlah, 0, 1, 'R');
    }

    // public function Pembuat($nama)
    // {
    //     $this->Ln(12);
    //     $this->SetFont('Arial', '', 10);
    //     $this->Cell(0, 6, 'Mengetahui,', 0, 1, 'R');
    //     $this->Ln(18);
    //     $this->SetFont('Arial', 'B', 10);
    //     $this->Cell(0, 6, $nama, 0, 1, 'R');
    // }
}
