<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

try {

    if (!isset($_REQUEST['days'])) {
        header('location: ../follow_up.php');
        exit;
    }

    include_once('../../../classes/manager.class.php');
   // include_once('../../../classes/PHPMailer/PHPMailerAutoload.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);

    // default days
    $days = isset($days) && $days != "" ? (int)$days : 7;
    if ($days <= 0) { $days = 7; }

    // ================= PDF Settings =================
    $font = 10;
    $setx = 10;

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

    $pdf->SetTitle('Follow Up 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, 'Follow Up Report', 0, 0, 'C');

    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(0, 4, 'Customers whose last invoice is ' . $days . ' days old or more', 0, 0, 'C');

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

    // ================= Table Header =================
    $pdf->SetX($setx);
    $header = array('S.No', 'Customer', 'Contact', 'Balance', 'Total Inv', 'Days', 'Remarks');

    function FancyTableFollowUp($header, $setx, $days, $m)
    {
        global $pdf;

        // 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);

        // Column widths (adjusted for legal page)
        $wr = array(9, 65, 25, 17,16, 12, 55);

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

        // restore font
        $pdf->SetFillColor(224, 235, 255);
        $pdf->SetTextColor(0);
        $pdf->SetFont('');

        $fill = false;
        $counter_pagebreak = 1;

        // Fetch Data
        $rows = Invoice::GetCustomerFollowUpReport($days);
		$totalBalance = 0;
		$totalInvoices = 0;
        $counter = 0;
        if ($rows) {
            foreach ($rows as $r) {
                $counter++;

                $pdf->SetX($setx);
                $pdf->SetFont('Arial', 'B', 9);

                $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, 'R', $fill);
                $pdf->Cell($wr[3], 7, number_format($r->balance, 2, '.', ''), 'LRBT', 0, 'R', $fill);
                $pdf->Cell($wr[4], 7, $r->total_invoices, 'LRBT', 0, 'C', $fill);
                $pdf->Cell($wr[5], 7, $r->days_since_last_invoice, 'LRBT', 0, 'C', $fill);
                $pdf->Cell($wr[6], 7, '', 'LRBT', 0, 'L', $fill);


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

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

                $pdf->Ln();
                $fill = !$fill;
                $counter_pagebreak++;
            }
        } else {
            // No data row
            $pdf->SetX($setx);
            $pdf->SetFont('Arial', 'B', 10);
            $pdf->Cell(array_sum($wr), 8, 'No customers found for follow-up.', 1, 0, 'C', false);
        }
    }

    FancyTableFollowUp($header, $setx, $days, $m);

    $pdf->Output();
    ob_end_flush();
    exit;

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