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

try {

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

    $m = new Manager();

    if (!isset($_SESSION['sess_userid'])) {
        header("Location: " . PROJECT_ADMIN_URL);
        exit();
    }

    // Get filters exactly like HTML report
    $cat_type    = isset($_GET['cat_type']) ? $_GET['cat_type'] : 'All';
    $status_type = isset($_GET['status_type']) ? $_GET['status_type'] : 'All';

    $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);
    define('PROJECT_DISCOUNT', $param_data->is_discount);
    define('PROJECT_TAX', $param_data->is_tax);

    // Build filter exactly like HTML
    $filterReport = " WHERE 1 ";

    if ($cat_type != 'All') {
        $filterReport .= " AND is_mart = '" . addslashes($cat_type) . "' ";
    }

    if ($status_type != 'All') {
        $filterReport .= " AND status = '" . addslashes($status_type) . "' ";
    }

   // $filterReport .= " ORDER BY name ASC ";

    $customerList = CustomerSetup::GetCustomerSetupList($filterReport);

    $pdf = new FPDFWithOutWaterMark('P', 'mm', 'A4');
    $pdf->SetMargins(10, 10, 10);
    $pdf->SetAutoPageBreak(true, 10);
    $pdf->SetTitle('Customer List Report');
    $pdf->AddPage();

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

    $pdf->SetFont('Arial', '', 8);
    $pdf->Cell(95, 6, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 0, 'L');
    $pdf->Cell(95, 6, 'Type: ' . ($cat_type == '0' ? 'Wholesaler' : ($cat_type == '1' ? 'Local Mart' : 'All')), 0, 1, 'R');
    $pdf->Cell(95, 6, '', 0, 0, 'L');
    $pdf->Cell(95, 6, 'Status: ' . ucfirst($status_type), 0, 1, 'R');

    $pdf->Ln(2);
    $pdf->SetFont('Arial', 'B', 14);
    $pdf->Cell(0, 8, 'Customer List Report', 0, 1, 'C');
    $pdf->Ln(3);

    // Table header
    $header = array('S.No', 'Name', 'Contact', 'Location', 'Status');
    $widths = array(15, 55, 35, 55, 30);

    $drawTableHeader = function() use ($pdf, $header, $widths) {
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->SetFillColor(224, 235, 255);
        $pdf->SetTextColor(0);
        $pdf->SetDrawColor(128, 128, 128);
        $pdf->SetLineWidth(.2);

        for ($i = 0; $i < count($header); $i++) {
            $pdf->Cell($widths[$i], 8, $header[$i], 1, 0, 'C', true);
        }
        $pdf->Ln();
    };

    $drawTableHeader();

    if (!empty($customerList)) {
        $pdf->SetFont('Arial', '', 8);
        $fill = false;
        $counter = 1;

        foreach ($customerList as $row) {

            // Page break handling with repeated header
            if ($pdf->GetY() > 270) {
                $pdf->AddPage();
                $drawTableHeader();
                $pdf->SetFont('Arial', '', 8);
            }

            $name     = isset($row->name) ? $row->name : '';
            $contact  = isset($row->contact) ? $row->contact : '';
            $location = isset($row->location) ? $row->location : '';
            $status   = isset($row->status) ? $row->status : '';

            $pdf->Cell($widths[0], 7, $counter, 1, 0, 'C', $fill);
            $pdf->Cell($widths[1], 7, $name, 1, 0, 'L', $fill);
            $pdf->Cell($widths[2], 7, $contact, 1, 0, 'L', $fill);
            $pdf->Cell($widths[3], 7, $location, 1, 0, 'L', $fill);
            $pdf->Cell($widths[4], 7, $status, 1, 1, 'L', $fill);

            $fill = !$fill;
            $counter++;
        }
    } else {
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(0, 10, 'No Customers Found', 1, 1, 'C');
    }

    $pdf->Output();
    exit;

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