<?php
error_reporting(E_ERROR | E_STRICT);
ini_set('display_errors', '0');
try {

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

    $filterReport = "WHERE 1 ";

    // Filter by `is_mart`
    if (isset($cat_type) && $cat_type != 'All') {
        $filterReport .= " AND is_mart = '$cat_type' ";
    }

    // Filter by `status`
    if (isset($status_type) && $status_type != 'All') {
        $filterReport .= " AND status = '$status_type' ";
    }

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

    if (!empty($invoice)) {
        $pdf = new FPDFWithOutWaterMark('P', 'mm', 'A4');
        $pdf->SetMargins(10, 0, 0);
        $pdf->AddPage();

        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(0, 8, PROJECT_NAME, 0, 1, 'C');
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(0, 8, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 1, 'C');
        //$pdf->Ln();

        $pdf->SetFont('Arial', '', 14);
        $pdf->Cell(0, 6, 'Customer List Report', 0, 1, 'C');
       // $pdf->Ln();

        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(0, 4, 'Customer Type: ' . ($cat_type == 'All' ? 'All' : ($cat_type == '0' ? 'Wholesaler' : 'Local Mart')), 0, 1, 'C');
        $pdf->Cell(0, 4, 'Status: ' . ucfirst($status_type), 0, 1, 'C');
        $pdf->Ln();

        $header = ['S.No', 'Customer Name', 'Location', 'Status'];
        $wr = [10, 80, 50, 40];

        // Header
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->SetFillColor(224, 235, 255);
        for ($i = 0; $i < count($header); $i++) {
            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'C', true);
        }
        $pdf->Ln();

        // Data
        $pdf->SetFont('Arial', '', 7);
        $counter = 1;
        foreach ($invoice as $row) {
            $pdf->Cell($wr[0], 6, $counter, 1, 0, 'C');
            $pdf->Cell($wr[1], 6, htmlspecialchars($row->name), 1, 0, 'L');
            $pdf->Cell($wr[2], 6, htmlspecialchars($row->location), 1, 0, 'L');
            $pdf->Cell($wr[3], 6, ($row->status ), 1, 0, 'C');
            $pdf->Ln();
            $counter++;
        }

        $pdf->Output('customerlist_report.pdf', 'I');
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }
} catch (Exception $e) {
    echo $e->getMessage();
}
?>
