<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../fpdf/fpdf.php';

class GP_PDF extends FPDF
{
    function Header()
    {
        // Logo
        if (file_exists(__DIR__ . '/../assets/images/logo-bg-removed2.png')) {
            // Keep logo small and aligned top left
            $this->Image(__DIR__ . '/../assets/images/logo-bg-removed2.png', 12, 8, 20);
        }

        // Contact info on the right
        $this->SetTextColor(51, 51, 51); // #333
        $this->SetFont('Arial', '', 9);
        $this->SetXY(140, 8);
        // Using a single string with MultiCell for the address
        $this->MultiCell(58, 4, "Sivakasi, Tamil Nadu - 626189\nPhone: +91 7358948807\nEmail: gptraderssivakasi@gmail.com", 0, 'R');

        // BRANDING & TITLE - Centered
        $this->SetY(10);
        $this->SetFont('Arial', 'B', 24);
        $this->SetTextColor(211, 47, 47); // Theme Red #d32f2f
        $this->Cell(0, 12, 'GP CRACKERS', 0, 1, 'C');

        $this->SetFont('Arial', 'B', 14);
        $this->SetTextColor(102, 102, 102); // Medium Gray #666
        $this->Cell(0, 8, 'OFFICIAL PRICE LIST', 0, 1, 'C');

        // Double Accent Line
        $this->SetDrawColor(211, 47, 47);
        $this->SetLineWidth(0.8);
        $this->Line(10, 40, 200, 40);
        $this->SetLineWidth(0.2);
        $this->Line(10, 41.5, 200, 41.5);

        // Date under the line
        $this->SetXY(10, 41.5);
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(128, 128, 128);
        $this->Cell(190, 8, "Issued on: " . date('d M Y'), 0, 0, 'R');

        $this->SetY(52); // Move down for content
    }

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(128);
        $this->Cell(0, 10, 'Thank you for choosing GP Crackers! Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

    function LoadData($pdo)
    {
        $stmt = $pdo->query("SELECT * FROM products ORDER BY category, name");
        return $stmt->fetchAll();
    }

    function ProductTable($header, $data)
    {
        // Colors, line width and bold font
        $this->SetFillColor(230, 230, 230); // Light gray
        $this->SetTextColor(0);
        $this->SetDrawColor(200, 200, 200);
        $this->SetLineWidth(.3);
        $this->SetFont('', 'B');

        // Header
        $w = array(15, 80, 60, 35);
        for ($i = 0; $i < count($header); $i++)
            $this->Cell($w[$i], 10, $header[$i], 1, 0, 'C', true);
        $this->Ln();

        // Color and font restoration
        $this->SetFillColor(255, 255, 255);
        $this->SetTextColor(0);
        $this->SetFont('');

        // Data
        $count = 1;
        foreach ($data as $row) {
            $this->Cell($w[0], 8, $count++, 'LRB', 0, 'C');
            $this->Cell($w[1], 8, iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $row['name']), 'LRB', 0, 'L');
            $this->Cell($w[2], 8, strtoupper($row['category']), 'LRB', 0, 'L');
            // Rupees symbol alternative for FPDF standard fonts
            $priceStr = 'Rs. ' . number_format($row['price'], 0);
            $this->Cell($w[3], 8, $priceStr, 'LRB', 0, 'R');
            $this->Ln();
        }
        // Closing line
        $this->Cell(array_sum($w), 0, '', 'T');
    }
}

$pdf = new GP_PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$header = array('S.No', 'Product Name', 'Category', 'Price');
$data = $pdf->LoadData($pdo);
$pdf->ProductTable($header, $data);

// Output the PDF
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="GP-Crackers-PriceList.pdf"');
$pdf->Output('D', 'GP-Crackers-PriceList.pdf');
exit;
?>
