<?php
ob_start();
error_reporting(0);
ini_set('display_errors', '0');

try {

    include_once('../../../classes/manager.class.php');
    include_once('../../../fpdf/fpdf1.php');

    $m = new Manager();
    extract($_REQUEST);

    $param_data = ControlConfifguration::GetConfiguration();
    define('PROJECT_NAME', $param_data->name);
    define('PROJECT_ADDRESS', $param_data->address);
    define('PROJECT_TEL', $param_data->contact);
    define('PROJECT_EMAIL', $param_data->email);
    define('PROJECT_WEBSITE', $param_data->website);
    define('PROJECT_COLOR', $param_data->color);

    $min_pending = isset($min_pending) && $min_pending != "" ? (int)$min_pending : 2;
    if ($min_pending < 2) $min_pending = 2;

    // ✅ Left margin
    $setx = 10;

    $pdf = new FPDFWithOutWaterMark('P', 'mm', 'legal');
    $pdf->SetMargins(0, 0, 0);

    $pdf->SetTitle('Multiple Pending Invoices Report');
    $pdf->AddPage();
    $pdf->Ln();

    // ================= Header =================
    $pdf->SetFont('Arial', 'B', 16);
    $pdf->Cell(0, 8, PROJECT_NAME, 0, 0, 'C');

    $pdf->SetX($setx);
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(0, 8, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 0, 'L');

    $pdf->Ln();
    $pdf->SetFont('Arial', 'B', 14);
    $pdf->Cell(0, 6, 'Multiple Pending Invoices Report', 0, 0, 'C');

    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(0, 4, 'Customers with Pending Invoices >= ' . $min_pending, 0, 0, 'C');

    $pdf->Ln();
    $pdf->Ln();

    // ✅ Customer # removed
    $header = array('S.No', 'Customer', 'Contact', 'Oldest Pending', 'Pending', 'Received', 'Balance');

    function FancyTableMultiplePending($header, $setx, $min_pending)
    {
        global $pdf;

        $totalPendingInvoices = 0;
        $totalRemainingBalance = 0;
        $totalReceived = 0;

        // Colors, line width and bold font
        $pdf->SetFillColor(224, 235, 255);
        $pdf->SetTextColor(0);
        $pdf->SetDrawColor(128, 0, 0);
        $pdf->SetLineWidth(.1);
        $pdf->SetFont('Arial', 'B', 10);

        /*
         ✅ CRITICAL FIX:
         Legal Portrait width = 216mm
         We are starting at X = 10, so usable width ≈ 196mm
         Total below widths = 196mm PERFECT ✅
        */
        $wr = array(12, 68, 30, 24, 18, 22, 22); // SUM = 196

        // Header Row
        $pdf->SetX($setx);
        for ($i = 0; $i < count($header); $i++) {
            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
        }
        $pdf->Ln();

        $pdf->SetFont('Arial', '', 9);
        $fill = false;

        $counter_pagebreak = 1;

        $rows = Invoice::GetCustomerMultiplePendingInvoicesReport($min_pending);

        $counter = 0;
        if ($rows) {
            foreach ($rows as $r) {
                $counter++;

                $totalPendingInvoices += (int)$r->pending_invoices;
                $totalReceived += (float)$r->total_received_against_pending;
                $totalRemainingBalance += (float)$r->remaining_balance;

                $pdf->SetX($setx);

                $pdf->Cell($wr[0], 7, $counter, 'LRBT', 0, 'C', $fill);
                $pdf->Cell($wr[1], 7, $r->customer_name, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[2], 7, $r->customer_contact, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[3], 7, date('d-m-Y', strtotime($r->oldest_pending_date)), 'LRBT', 0, 'C', $fill);
                $pdf->Cell($wr[4], 7, $r->pending_invoices, 'LRBT', 0, 'C', $fill);
                $pdf->Cell($wr[5], 7, number_format($r->total_received_against_pending, 2, '.', ''), 'LRBT', 0, 'R', $fill);
                $pdf->Cell($wr[6], 7, number_format($r->remaining_balance, 2, '.', ''), 'LRBT', 0, 'R', $fill);

                // ✅ Page break same style
                if ($counter == 38 || ($counter != 38 && ($counter_pagebreak) % 42 == 0)) {
                    $counter_pagebreak = 1;
                    $pdf->AddPage();

                    $pdf->Ln(5);
                    $pdf->SetFont('Arial', 'B', 10);
                    $pdf->SetX($setx);
                    for ($i = 0; $i < count($header); $i++) {
                        $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
                    }
                    $pdf->Ln();
                    $pdf->SetFont('Arial', '', 9);
                }

                $pdf->Ln();
                $fill = !$fill;
                $counter_pagebreak++;
            }

            // ✅ TOTAL ROW
            $pdf->SetX($setx);
            $pdf->SetFont('Arial', 'B', 10);

            $pdf->Cell($wr[0] + $wr[1] + $wr[2] + $wr[3], 7, 'TOTAL', 'LRBT', 0, 'R');
            $pdf->Cell($wr[4], 7, $totalPendingInvoices, 'LRBT', 0, 'C');
            $pdf->Cell($wr[5], 7, number_format($totalReceived, 2, '.', ''), 'LRBT', 0, 'R');
            $pdf->Cell($wr[6], 7, number_format($totalRemainingBalance, 2, '.', ''), 'LRBT', 0, 'R');

        } else {
            $pdf->SetX($setx);
            $pdf->SetFont('Arial', 'B', 10);
            $pdf->Cell(array_sum($wr), 8, 'No customers found.', 1, 0, 'C', false);
        }
    }

    FancyTableMultiplePending($header, $setx, $min_pending);

    if (ob_get_length()) {
        ob_end_clean();
    }

    $pdf->Output();
    exit;

} catch (Exception $e) {
    if (ob_get_length()) {
        ob_end_clean();
    }
    echo $e->getMessage();
}
?>
