<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Your Vehicle Report</title>
</head>

<body style="margin:0; padding:0; font-family: Arial, sans-serif; background-color:#ffffff;">
<?php
    $vehicleMot = isset($leadFormData) && !empty($leadFormData->vehicle_mot_data_json) ? json_decode($leadFormData->vehicle_mot_data_json, true) : [];
    $roadTaxMot = isset($leadFormData) && !empty($leadFormData->mot_taxed_json) ? json_decode($leadFormData->mot_taxed_json, true) : [];
    $vehicleData = $vehicleMot['vehicleData'] ?? [];
    $vehicleInformation = $vehicleMot['vehicleInformation'] ?? [];
    $motMileageSummary = $vehicleMot['motMileageSummary'] ?? [];
    $motTestList = $vehicleMot['motTestList'] ?? [];
    $vehicleTaxCo2Emissions = $vehicleMot['vehicleTaxCo2Emissions'] ?? [];
    $general = $vehicleMot['general'] ?? [];
    $consumption = $vehicleMot['consumption'] ?? [];
    $performance = $vehicleMot['performance'] ?? [];
    $dimensions = $vehicleMot['dimensions'] ?? [];
    $safety = $vehicleMot['safety'] ?? [];
    $importExport = $vehicleMot['importExport'] ?? [];
    $classificationDetails = $vehicleMot['classificationDetails'] ?? [];
    $smmtDetails = $vehicleMot['smmtDetails'] ?? [];
    $colourChange = $vehicleMot['colourChange'] ?? [];
    $scrapDestruction = $vehicleMot['scrapDestruction'] ?? [];
    $V5CCertificates = $vehicleMot['V5CCertificates'] ?? [];
?>
    <!-- Main Wrapper Table -->
    <table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" style="background-color:#ffffff;">
        <tr>
            <td align="center">
                <table width="600" cellpadding="0" cellspacing="0" border="0" align="center"
                    style="width:600px; margin:0 auto;">

                    <!-- Title -->
                    <tr>
                        <td align="center" style="padding:20px 0;">
                            <h2 style="color:#555555; margin:0; font-size:20px;">YOUR VEHICLE DATA</h2>
                        </td>
                    </tr>

                    {{-- @dd($vehicleInformation); --}}
                    <!-- SECTION 1: Vehicle Data -->
                    <tr>
                        <td>
                            <table cellpadding="10" cellspacing="10" border="0" width="100%">
                                
                                <tr>
                                    <td
                                        style="border:2px solid <?= !empty($vehicleInformation['Scrapped']) ? '#F44336;' : '#4CAF50;' ?> border-radius:6px; text-align:center; font-size:14px;">
                                        Scrapped<br><span style="color:<?= !empty($vehicleInformation['Scrapped']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($vehicleInformation['Scrapped']) ? '&#10006;' : '&#10004;' ?></span></td>
                                    <td
                                        style="border:2px solid <?= !empty($vehicleInformation['Exported']) ? '#F44336;' : '#4CAF50;' ?> border-radius:6px; text-align:center; font-size:14px;">
                                        Exported<br><span style="color:<?= !empty($vehicleInformation['Exported']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($vehicleInformation['Exported']) ? '&#10006;' : '&#10004;' ?></span></td>
                                    <td
                                        style="border:2px solid <?= !empty($vehicleInformation['Imported']) ? '#F44336;' : '#4CAF50;' ?> border-radius:6px; text-align:center; font-size:14px;">
                                        Imported<br><span style="color:<?= !empty($vehicleInformation['Imported']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($vehicleInformation['Imported']) ?  '&#10006;':'&#10004;'  ?></span></td>
                                    <td
                                        style="border:2px solid <?= !empty($vehicleInformation['plateChange']) ? '#F44336;' : '#f49320;' ?>  border-radius:6px; text-align:center; font-size:14px;">
                                        Plate<br><span style="color:<?= !empty($vehicleInformation['plateChange']) ? '#F44336;' : '#f49320;' ?> font-size:18px;"><?= !empty($vehicleData['plateChange']) ? '<span style="font-size:21px; font-weight:bold;">!</span>' :'&#10004;'  ?></span>
                                    </td>
                                    <td
                                        style="border:2px solid <?= !empty($vehicleInformation['colourChange']) ? '#F44336;' : '#4CAF50;' ?> border-radius:6px; text-align:center; font-size:14px;">
                                        Colour<br><span style="color:<?= !empty($vehicleInformation['colourChange']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($vehicleData['colourChange']) ? '&#10006;' :'&#10004;'  ?></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td
                                        style="border:2px solid <?= !empty($vehicleInformation['mileage']) ? '#F44336;' : '#4CAF50;' ?> border-radius:6px; text-align:center; font-size:14px;">
                                        Mileage<br><span style="color:<?= !empty($vehicleInformation['mileage']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($vehicleData['mileage']) ? '&#10006;' : '&#10004;' ?></span></td>
                                    <td
                                        style="border:2px solid <?= !empty($vehicleData['texi']) ? '#F44336;' : '#4CAF50;' ?> border-radius:6px; text-align:center; font-size:14px;">
                                        Taxi<br><span style="color:<?= !empty($vehicleData['texi']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($vehicleData['texi']) ? '&#10006;' : '&#10004;' ?></span></td>
                                    <td
                                        style="border:2px solid <?= isset($motMileageSummary['recall']) && $motMileageSummary['recall'] === 'yes' ? '#F44336;' : '#4CAF50;' ?> border-radius:6px; text-align:center; font-size:14px;">
                                        Recall<br><span style="color:<?= isset($motMileageSummary['recall']) && $motMileageSummary['recall'] === 'yes' ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= isset($motMileageSummary['recall']) && $motMileageSummary['recall'] === 'yes' ? '&#10006;' : '&#10004;' ?></span></td>
                                    <td
                                        style="border:2px solid #4CAF50; border-radius:6px; text-align:center; font-size:14px;">
                                        Keepers<br><span style="font-weight:bold;"><?= htmlspecialchars((string)($vehicleData['totalKeepers'] ?? '***')) ?></span></td>
                                   <td rowspan="2"
                                        style="border:2px solid <?= 
                                            (isset($vehicleData['keeperChanged']) && count($vehicleData['keeperChanged']) <= 3) 
                                                ? '#4CAF50' 
                                                : ((isset($vehicleData['keeperChanged']) && count($vehicleData['keeperChanged']) <= 6) 
                                                    ? '#d9534f' 
                                                    : '#F44336') 
                                        ?>;
                                        border-radius:6px; 
                                        text-align:left; 
                                        font-size:14px; 
                                        padding:8px; 
                                        vertical-align:top;">
                                                                            <b>Keepers</b><br>
                                    <?php if (!empty($vehicleData['keeperChanged']) && is_array($vehicleData['keeperChanged'])): ?>
                                    <?php foreach ($vehicleData['keeperChanged'] as $k): ?>
                                                                        <span style="color:#f49320">
                                    <?= !empty($k['date']) ? date('d-M-y', strtotime($k['date'])) : '' ?>
                                    </span><br>

                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                                                        </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <!-- SECTION 2: Untaxed + MOT -->
                    <tr>
                        <td>
                            <table cellpadding="10" cellspacing="10" border="0" width="100%">
                                <tr>
                                    <!-- Untaxed -->
                                    <?php
                                        $isTaxed = isset($roadTaxMot['taxStatus']) ? (strtolower($roadTaxMot['taxStatus']) === 'sorn') : null;
                                        $taxDueDate = $roadTaxMot['taxDueDate'] ?? null;
                                        $taxDueInDays = $roadTaxMot['taxDueInDays'] ?? null;
                                    ?>
                                    <td valign="top" width="50%"
                                    style="border:2px solid <?= $isTaxed? '#F44336' : '#4CAF50' ?>; border-radius:6px; padding:10px; text-align:left;">
                                        <h3 style="color:<?= $isTaxed ? '#F44336' : '#4CAF50' ?>; margin:0; font-size:16px;"><?= $isTaxed ?  '&#10006; Untaxed': '&#10004; Taxed' ?></h3>
                                        <p style="margin:0; font-size:12px; color:#777;">
                                            <?php if ($taxDueDate): ?>
                                            Tax due: <?= htmlspecialchars((string)$taxDueDate) ?><?= isset($taxDueInDays) ? ' – Since ' . htmlspecialchars((string)$taxDueInDays) . ' days' : '' ?>
                                            <?php else: ?>
                                            Tax due: N/A
                                            <?php endif; ?>
                                        </p>
                                        <hr style="border:0; border-top:1px solid #ddd; margin:8px 0;">
                                        <table width="100%" cellpadding="4" cellspacing="0" border="0"
                                            style="font-size:13px;">
                                            <tr>
                                                <td><b>Vehicle Make</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['make'] ?? '***')) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Fuel Type</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['fuelType'] ?? '***')) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Cylinder Capacity</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['engineCapacity'] ?? '***')) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Vehicle Colour</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['colour'] ?? '***')) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Euro Stats</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['euroStatus'] ?? '***')) ?> </td>
                                            </tr>
                                            <tr>
                                                <td><b>Co2 Emissions</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['co2Emissions'] ?? ($vehicleMot['co2emission'] ?? '***'))) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Real Driving Emissions</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['euroStatus'] ?? '***')) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Wheelplan</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['wheelplan'] ?? '***')) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Ulez Compliance</b></td>
                                                <td align="right"><a
                        href="https://tfl.gov.uk/modes/driving/check-your-vehicle/"
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        Check TFL
                      </a></td>
                                            </tr>
                                          
                                        </table>
                                    </td>

                                    <!-- MOT -->
                                    <td valign="top" width="50%"
                                        style="border:2px solid #4CAF50; border-radius:6px; padding:10px; text-align:left;">
                                        <?php
                                            $motStatus = $roadTaxMot['motStatus'] ?? null; // e.g., Valid/Invalid
                                            $motExpiryDate = $roadTaxMot['motExpiryDate'] ?? null;
                                            $motExpiryInDays = $roadTaxMot['motExpiryInDays'] ?? null;
                                        ?>

                                        <h3 style="color:<?= ($motStatus && strtolower($motStatus) === 'valid') ? '#4CAF50' : '#F44336' ?>; margin:0; font-size:16px;"><?= ($motStatus && strtolower($motStatus) === 'valid') ? '&#10004; MOT' : '&#10006; MOT' ?></h3>
                                        <p style="margin:0; font-size:12px; color:#777;">
                                            <?php if ($motExpiryDate): ?>
                                            Expired on: <?= htmlspecialchars((string)$motExpiryDate) ?><?= isset($motExpiryInDays) ? ' – Since ' . htmlspecialchars((string)$motExpiryInDays) . ' days' : '' ?>
                                            <?php else: ?>
                                            Expired on: N/A
                                            <?php endif; ?>
                                        </p>
                                        <hr style="border:0; border-top:1px solid #ddd; margin:8px 0;">
                                        <table width="100%" cellpadding="4" cellspacing="0" border="0"
                                            style="font-size:13px;">
                                            <tr>
                                                <td><b>Year Of Manufacture</b></td>
                                                <td align="right"><?= !empty($roadTaxMot['yearOfManufacture']) ? $roadTaxMot['yearOfManufacture'] : '' ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Date Of First Registration</b></td>
                                                <td align="right"><?= !empty($roadTaxMot['monthOfFirstRegistration']) ? date('M-y', strtotime($roadTaxMot['monthOfFirstRegistration'])) : '' ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Date Of Last V5C(logbook) Issued</b></td>
                                                <td align="right"><?= !empty($roadTaxMot['dateOfLastV5CIssued']) ? date('d-M-y', strtotime($roadTaxMot['dateOfLastV5CIssued'])) : '' ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Export Marker</b></td>
                                                <td align="right"><?= !empty($roadTaxMot['markedForExport'])? htmlspecialchars((string)($roadTaxMot['markedForExport'] ? 'Yes':'No')):"***" ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Vehicle Type Approval</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['typeApproval'] ??  "***")) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Revenue Weight</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['revenueWeight'] ??  "***")) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Vehicle Status</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['taxStatus'] ??  "***")) ?></td>
                                            </tr>
                                            <tr>
                                                <td><b>MOT Status</b></td>
                                                <td align="right"><?= htmlspecialchars((string)($roadTaxMot['motStatus'] ?? ($vehicleMot['motStatus'] ??  "***"))) ?></td>
                                            </tr>

                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <!-- SECTION 3: MOT & Mileage Summary -->
                    <tr>
                        <td>
                            <table cellpadding="10" cellspacing="10" border="0" width="100%" style="font-size: 13px;
                                font-family: Arial, sans-serif;
                                color: #333;
                                background: #ffffff;">
                                <tr>
                                    <td colspan="2" align="center">
                                        <h3 style="color:#555555; margin:0; font-size:18px;">Mot & Mileage Summary</h3>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="border:1px solid #ddd; border-radius:6px;"> <b>Total Test:</b><?= htmlspecialchars((string)($motMileageSummary['totalTests'] ?? '***')) ?></td>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><?= (isset($motMileageSummary['passRatePercentage']) && $motMileageSummary['passRatePercentage']>=80)? '<span style="color:#4CAF50; font-size:18px;">&#10004;</span>':((isset($motMileageSummary['passRatePercentage']) && $motMileageSummary['passRatePercentage']>=70 && $motMileageSummary['passRatePercentage'] < 80)?  '<span style="font-size:21px; font-weight:bold;color:#f49320;">! </span>':'&#10006;') ?> <b>Pass Rate:</b> <?= isset($motMileageSummary['passRatePercentage']) ? htmlspecialchars((string)$motMileageSummary['passRatePercentage']) . '%' : '***' ?></td>
                                </tr>
                                <tr>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="color:#4CAF50; font-size:18px;">&#10004;</span> <b>Test Passed:</b>
                                        <?= htmlspecialchars((string)($motMileageSummary['totalPassed'] ?? '***')) ?>
                                    </td>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="font-size:21px; font-weight:bold;color:#f49320;">! </span> <b>Pass Rate With Advisory:</b> <?= isset($motMileageSummary['passRateWithAdvisory']) ? htmlspecialchars((string)$motMileageSummary['passRateWithAdvisory']) . '%' : '***' ?></td>
                                </tr>
                                <tr>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="font-size:21px; font-weight:bold;color:#f49320;">!</span> <b>Passed With Advisory:</b>
                                        <?= htmlspecialchars((string)($motMileageSummary['passedWithAdvisory'] ?? '***')) ?>
                                    </td>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="color:#4CAF50; font-size:18px;">&#10004;</span> <b>Pass Rate Without
                                        Advisory:</b><?= isset($motMileageSummary['passRateWithoutAdvisory']) ? htmlspecialchars((string)$motMileageSummary['passRateWithoutAdvisory']) . '%' : '***' ?></td>
                                </tr>
                                <tr>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="font-size:21px; font-weight:bold;color:#f49320;">!</span> <b>Total Items Advised:</b>
                                        <?= htmlspecialchars((string)($motMileageSummary['totalItemsAdvised'] ?? '***')) ?>
                                    </td>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="color:#4CAF50; font-size:18px;">&#10004;</span> <b>Mileage Issues:</b>
                                        <?= !empty($motMileageSummary['mileageIssue']) ? 'Yes' : 'No' ?></td>
                                </tr>
                                <tr>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="font-size:21px; font-weight:bold;color:#f49320;">!</span> <b>Advisory Average/Per MOT:</b>
                                        <?= htmlspecialchars((string)($motMileageSummary['advisoryAverage'] ?? '***')) ?>
                                    </td>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><b>Last MOT Mileage:</b>
                                        <?= isset($motMileageSummary['lastMileage']) ? htmlspecialchars((string)$motMileageSummary['lastMileage']) . ' ' . htmlspecialchars((string)($motMileageSummary['lastMileageUnit'] ?? '')) : '***' ?></td>
                                </tr>
                                <tr>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="color:
                                    #F44336;
                                    font-size:18px;">&#10006;</span> <b>Test Failed:</b>
                                        <?= htmlspecialchars((string)($motMileageSummary['totalFailed'] ?? '***')) ?>
                                    </td>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><b>Est Current Mileage:</b>
                                        <?= isset($motMileageSummary['currentMileage']) ? htmlspecialchars((string)$motMileageSummary['currentMileage']) . ' ' . htmlspecialchars((string)($motMileageSummary['lastMileageUnit'] ?? '')) : '***' ?></td>
                                </tr>
                                <tr>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><span style="color:
                                #F44336;
                                font-size:18px;">&#10006;</span> <b>Total Items Failed:</b>
                                        <?= htmlspecialchars((string)($motMileageSummary['totalItemsFailed'] ?? '***')) ?>
                                    </td>
                                    <td style="border:1px solid #ddd; border-radius:6px;"><b>Average Mileage:</b>
                                        <?= isset($motMileageSummary['avgMileagePerYear']) ? htmlspecialchars((string)$motMileageSummary['avgMileagePerYear']) . ' Miles/Year' : '***' ?></td>
                                </tr>
                                 <tr>
                                     <td style="border:1px solid #ddd; border-radius:6px;"><?= !empty($motMileageSummary['taxi']) ? '<span style="color:
                                #F44336;
                                font-size:18px;">&#10006;</span>':'<span style="color:#4CAF50; font-size:18px;">&#10004;</span>' ?> <b>Taxi/Rent a Car Use:</b>
                                        <?= !empty($motMileageSummary['taxi']) ? 'Might be' : 'Might not' ?></td>
                                        <td style="border:1px solid #ddd; border-radius:6px;"><span style="font-size:21px; font-weight:bold;color:#f49320;">!</span> <b>Mileage Status:</b>
                                            <?= htmlspecialchars((string)($motMileageSummary['totalItemsAdvised'] ?? '***')) ?>
                                        </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <!-- SECTION 5: MOT Test List -->
                    <tr>
                        <td align="center"
                            style="padding-top:20px; padding-bottom:15px; font-size:18px; color:#555555; font-weight:bold;">
                            MOT Test List
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table cellpadding="6" cellspacing="0" border="1" width="100%"
                                style="border-collapse:collapse; font-size:13px; border-color:#ccc;">
                                <tr style="background:#777; color:#ffffff; font-weight:bold; text-align:center;">
                                    <td>Date</td>
                                    <td>Result</td>
                                    <td>Mileage</td>
                                    <td>Difference</td>
                                    <td>Days</td>
                                    <td>Reason</td>
                                </tr>
                                <?php
                                    // Combine MOT tests and keeper changes into one chronological list
                                    $combinedEvents = [];
                                    
                                    // Add MOT tests
                                    if (!empty($vehicleMot['motTestList']['motList'])) {
                                        foreach ($vehicleMot['motTestList']['motList'] as $test) {
                                            $combinedEvents[] = [
                                                'type' => 'mot',
                                                'data' => $test,
                                                'date' => $test['date'] ?? ''
                                            ];
                                        }
                                    }
                                    
                                    // Add keeper changes
                                    if (!empty($vehicleMot['motTestList']['keepersChange']['data'])) {
                                        foreach ($vehicleMot['motTestList']['keepersChange']['data'] as $keeper) {
                                            $combinedEvents[] = [
                                                'type' => 'keeper',
                                                'data' => $keeper,
                                                'date' => $keeper['date'] ?? ''
                                            ];
                                        }
                                    }
                                    
                                    // Add plate changes
                                    if (!empty($vehicleMot['motTestList']['plateChange'])) {
                                        foreach ($vehicleMot['motTestList']['plateChange'] as $plate) {
                                            $combinedEvents[] = [
                                                'type' => 'plate',
                                                'data' => $plate,
                                                'date' => $plate['date'] ?? ''
                                            ];
                                        }
                                    }
                                    
                                    // Sort by date (newest first)
                                    usort($combinedEvents, function($a, $b) {
                                        $dateA = strtotime($a['date']);
                                        $dateB = strtotime($b['date']);
                                        if ($dateA === false || $dateB === false) {
                                            return 0;
                                        }
                                        return $dateB - $dateA; // Descending order
                                    });
                                    
                                    
                                    foreach ($combinedEvents as $event):
                                        if ($event['type'] === 'mot'):
                                            $test = $event['data'];
                                ?>
                                <tr style="background:#f9fff9;">
                                    <td><?= !empty($k['date']) ? date('d-M-y', strtotime($test['date'])) : '' ?></td>
                                    <td style="font-weight:bold; color:<?= (isset($test['result']) && strtoupper($test['result']) === 'PASSED') ? '#4CAF50' : '#f44336' ?>;">
                                        <?= (isset($test['result']) && strtoupper($test['result']) === 'PASSED') ? '<span style="color:#4CAF50; font-size:18px;">&#10004;</span> PASS' : '❌ FAIL' ?>
                                    </td>
                                    <td><?= htmlspecialchars((string)($test['mileage'] ?? '')) ?> <?= htmlspecialchars((string)($test['unit'] ?? '')) ?></td>
                                    <td><?= htmlspecialchars((string)($test['motTesNumber'] ?? '')) ?></td>
                                    <td><?= htmlspecialchars((string)($test['days'] ?? '')) ?></td>
                                    <td>Failure(<?= count($test['failure'] ?? []) ?>) Adv.(<?= count($test['advisory'] ?? []) ?>)</td>
                                </tr>
                              
                                <?php elseif ($event['type'] === 'keeper'): 
                                            $keeper = $event['data'];
                                ?>
                                                                <tr style="background:#fff3e0;">
                                                                    
                                                                    <td><?= !empty($keeper['date']) ? date('d-M-y', strtotime($keeper['date'])) : '' ?></td>
                                                                    <td style="font-weight:bold; color:#FF9800;">KEEPER <?= htmlspecialchars((string)($keeper['count'] ?? '')) ?></td>
                                                                </tr>
                                <?php elseif ($event['type'] === 'plate'): 
                                            $plate = $event['data'];
                                ?>
                                <tr style="background:#fff3e0;">
                                    <td><?= !empty($plate['date']) ? date('d-M-y', strtotime($plate['date'])) : '' ?></td>
                                    <td style="font-weight:bold; color:#FF9800;">PLATE <?= htmlspecialchars((string)($plate['plate_number'] ?? '')) ?></td>
                                    <td>
                                    <div style="display:flex;align-items:center;gap: -2px;">
                                        <!-- Left Plate -->
                                        <div style="display:flex;align-items:center;border: 1px solid #000;border-radius: 4px;overflow:hidden;font-family:Arial, sans-serif;font-weight:bold;">
                                        
                                            <div style="padding: 3px 6px;font-size: 10px;"><?= $plate['previous'] ?></div>
                                        </div>

                                        <!-- Arrow -->
                                        <span style="font-size: 10px;color:#003399;">→</span>

                                        <!-- Right Plate -->
                                        <div style="display:flex;align-items:center;border: 1px solid #000;border-radius: 4px;overflow:hidden;font-family:Arial, sans-serif;font-weight:bold;">
                                        
                                            <div style="padding: 3px 6px;font-size: 10px;"><?= $plate['current'] ?></div>
                                        </div>
                                    </div>
                                </td>
                                </tr>
                            <?php endif; ?>
                            <?php endforeach; ?>
                            </table>
                        </td>
                    </tr>






                    <!-- SECTION 5: Mileage History -->
                      <tr>
                        <td align="center" style="padding-top:20px; font-size:18px; color:#555555; font-weight:bold;">
                            Mileage History
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table cellpadding="6" cellspacing="0" border="1" width="100%"
                                style="border-collapse:collapse; font-size:13px; border-color:#ccc;">
                                <tr style="background:#777; color:#ffffff; font-weight:bold; text-align:center;">
                                    <td>S No.</td>
                                    <td>Date</td>
                                    <td>Mileage</td>
                                    <td>Difference</td>
                                    <td>Days</td>
                                </tr>
                    <?php
                        // Combine mileage history with keeper changes
                        $combinedMileageEvents = [];
                        
                        // Add mileage history
                        if (!empty($vehicleMot['mileageHistory'])) {
                            $sno = 1;
                            foreach ($vehicleMot['mileageHistory'] as $mileage) {
                                $combinedMileageEvents[] = [
                                    'type' => 'mileage',
                                    'data' => $mileage,
                                    'sno' => $sno++,
                                    'date' => $mileage['date'] ?? ''
                                ];
                            }
                        }
                        
                        // Add keeper changes to mileage history
                        if (!empty($vehicleMot['motTestList']['keepersChange']['data'])) {
                            foreach ($vehicleMot['motTestList']['keepersChange']['data'] as $keeper) {
                                $combinedMileageEvents[] = [
                                    'type' => 'keeper',
                                    'data' => $keeper,
                                    'sno' => '',
                                    'date' => $keeper['date'] ?? ''
                                ];
                            }
                        }
                        
                        // Sort by date (newest first)
                        usort($combinedMileageEvents, function($a, $b) {
                            $dateA = strtotime($a['date']);
                            $dateB = strtotime($b['date']);
                            if ($dateA === false || $dateB === false) {
                                return 0;
                            }
                            return $dateB - $dateA; // Descending order
                        });
                        
                        $sno = 1;
                        foreach ($combinedMileageEvents as $event):
                            if ($event['type'] === 'mileage'):
                                $mileage = $event['data'];
                    ?>
                                                    <tr style="background:#f9fff9;">
                                                        <td><?= $sno++ ?></td>
                                                        <td><?= !empty($mileage['date']) ? date('d-M-y', strtotime($mileage['date'])) : '' ?></td>
                                                        <td><?= htmlspecialchars((string)($mileage['mileage'] ?? '')) ?> <?= htmlspecialchars((string)($mileage['unit'] ?? '')) ?></td>
                                                        <td><?= htmlspecialchars((string)($mileage['difference'] ?? '')) ?></td>
                                                        <td><?= htmlspecialchars((string)($mileage['days'] ?? '')) ?></td>
                                                    </tr>
                    <?php elseif ($event['type'] === 'keeper'): 
                                $keeper = $event['data'];
                    ?>
                                                    <tr style="background:#fff3e0;">
                                                        <td></td>
                                                        <td><?= !empty($mileage['date']) ? date('d-M-y', strtotime($keeper['date'])) : '' ?></td>
                                                        <td style="font-weight:bold; color:#FF9800;">Keeper <?= htmlspecialchars((string)($keeper['count'] ?? '')) ?></td>
                                                        <td></td>
                                                        <td></td>
                                                    </tr>
                    <?php endif; ?>
                    <?php endforeach; ?>
                                                </table>
                                                
                                            
                                            </td>
                                        </tr>

                                    

                    <!-- Legend -->
                    {{-- <tr>
                        <td align="center" style="padding:15px;">
                            <table cellpadding="5" cellspacing="0" border="0"
                                style="font-size:12px; color:#555;">
                                <tr>
                                    <td style="padding-right:15px;"><span
                                            style="background:#fffbe6; border:1px solid #FF9800; padding:2px 6px; border-radius:3px;">&nbsp;</span>
                                        Km/Mileage</td>
                                    <td style="padding-right:15px;"><span
                                            style="background:#ffe6e6; border:1px solid #F44336; padding:2px 6px; border-radius:3px;">&nbsp;</span>
                                        Mileage Reverse</td>
                                    <td><span
                                            style="background:#e6f3ff; border:1px solid #2196F3; padding:2px 6px; border-radius:3px;">&nbsp;</span>
                                        Taxi MOT Gaps</td>
                                </tr>
                            </table>
                        </td>
                    </tr> --}}

                    <!-- Taxi MOT Gaps Note -->
                    {{-- <tr>
                        <td align="center" style="padding:10px;">
                            <table cellpadding="10" cellspacing="0" border="0" width="100%"
                                style="background:#e6f3ff; border:1px solid #2196F3; border-radius:6px; font-size:13px; color:#333;">
                                <tr>
                                    <td align="center" style="font-weight:bold;">Taxi MOT Gaps</td>
                                </tr>
                                <tr>
                                    <td align="center">This vehicle might have been used as Taxi or Commercial
                                        purposes, as it has three consecutive 6 months MOTs.</td>
                                </tr>
                            </table>
                        </td>
                    </tr> --}}

                    <!-- Vehicle Information -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4aa3df; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fcff; padding:10px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4aa3df;">
                                            Vehicle Information
                                        </td>
                                    </tr>

                                    <!-- Rows -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Vehicle Registration Mark</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Vrm'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Make &amp; Model</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Make'] ?? '***')) ?> <?= htmlspecialchars((string)($vehicleInformation['Model'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Make</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Make'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Model</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Model'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Engine Capacity</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['EngineCapacity'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Year of Manufacture</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['YearOfManufacture'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Fuel</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Fuel'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Date First Registered</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['DateFirstRegistered'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Door Plan</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['DoorPlanLiteral'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Date of 1st Registration in UK</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['DateFirstRegisteredUk'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Colour</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Colour'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Year-Month 1st Registered</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['YearMonthFirstRegistered'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">CO2 Emissions</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Co2Emissions'] ?? ($vehicleMot['co2emission'] ?? '***'))) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Transmission</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['Transmission'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Gear Count</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['GearCount'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Transmission Type</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['TransmissionType'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Engine Number</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['EngineNumber'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Transmission Code</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['TransmissionCode'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Gross Weight</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['GrossWeight'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Door Plan Code</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['DoorPlanCode'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Maximum Permissable Mass</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['MaximumPermissableMass'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">MVRIS Model Code</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['MVRISModelCode'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Seating Capacity</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['SeatingCapacity'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">MVRIS Make Code</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['MVRISMakeCode'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Wheel Plan</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['WheelPlan'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Used Before First Registration</td>
                                        <td style="padding:8px;"><?= !empty($vehicleInformation['UsedBeforeFirstRegistration']) ? 'Yes' : 'No' ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Vehicle Class</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['VehicleClass'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Vin Confirmation Flag</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['VinConfirmationFlag'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Date Of Last Update</td>
                                        <td style="padding:8px;" colspan="3"><?= !empty($vehicleInformation['DateOfLastUpdate']) ? date('d-M-y h:i:s', strtotime($vehicleInformation['DateOfLastUpdate'])) : '' ?></td>
                                    </tr>
                                </table>
                        </td>
                    </tr>

                    <!-- General Information -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4aa3df; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fcff; padding:10px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4aa3df;">
                                            General Information
                                        </td>
                                    </tr>

                                    <!-- Rows -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Make</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['Make'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Euro Status</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['EuroStatus'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Aspiration</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['Aspiration'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Fuel System</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['FuelSystem'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Number Of Cylinders</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['NumberOfCylinders'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Fuel Delivery</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['FuelDelivery'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Valves Per Cylinder</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['ValvesPerCylinder'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Fuel Catalyst</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['FuelCatalyst'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Cylinder Arrangement</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['CylinderArrangement'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Stroke</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['Stroke'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Valve Gear</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['ValveGear'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Location</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['Location'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Description</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['Description'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Bore</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['Engine']['Bore'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Power Delivery</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['PowerDelivery'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Type Approval Category</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['TypeApprovalCategory'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Driving Axle</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['DrivingAxle'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Primary Fuel Flag</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['PrimaryFuelFlag'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Series Description</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['SeriesDescription'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Driver Position</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['DriverPosition'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Data Version Number</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['DataVersionNumber'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Is Limited Edition</td>
                                        <td style="padding:8px;"><?= !empty($general['IsLimitedEdition']) ? 'Yes' : 'No' ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">EV Battery Capacity</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['ElectricVehicleBattery']['Capacity'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">EV Battery Charge Port</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['ElectricVehicleBattery']['ChargePort'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">EV Battery Charge Time</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['ElectricVehicleBattery']['ChargeTime'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">EV Battery Type</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($general['ElectricVehicleBattery']['Type'] ?? '***')) ?></td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>

                    <!-- Previous Keepers -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4aa3df; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fcff; padding:10px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4aa3df;">
                                            Previous Keepers
                                        </td>
                                    </tr>

                                    <!-- Rows -->

                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Total Keepers</td>
                                        <td style="padding:8px;"><?= !empty($vehicleMot['motTestList']['keepersChange']['total'])? $vehicleMot['motTestList']['keepersChange']['total'] :"***" ?></td>

                                        <td style="padding:8px; font-weight:bold;"><?= !empty($vehicleMot['motTestList']['keepersChange']['data'][0]['keeper_count_text'])? $vehicleMot['motTestList']['keepersChange']['data'][0]['keeper_count_text']:"" ?></td>
                                        <td style="padding:8px;"><?= !empty($vehicleMot['motTestList']['keepersChange']['data'][0]['date']) ? date('d-M-y', strtotime($vehicleMot['motTestList']['keepersChange']['data'][0]['date'])) : '' ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Keeper Data Count</td>
                                        
                                        <td style="padding:8px;"><?= !empty($vehicleMot['motTestList']['keepersChange']['numberofprevioukeeper'])?  htmlspecialchars((string)($vehicleMot['motTestList']['keepersChange']['numberofprevioukeeper'])):"***" ?></td>
                                    <td style="padding:8px; font-weight:bold;"><?= !empty($vehicleMot['motTestList']['keepersChange']['data'][1]['keeper_count_text'])? $vehicleMot['motTestList']['keepersChange']['data'][1]['keeper_count_text']:"***" ?></td>
                                        <td style="padding:8px;"><?= !empty($vehicleMot['motTestList']['keepersChange']['data'][1]['date']) ? date('d-M-y', strtotime($vehicleMot['motTestList']['keepersChange']['data'][1]['date'])) : '' ?><br></td>   
                                    </tr>
                                   

                                </table>
                            </div>
                        </td>
                    </tr>


                    <!-- V5C Certificate issue details -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4aa3df; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fcff; padding:10px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4aa3df;">
                                            V5C Certificate issue details
                                        </td>
                                    </tr>

                                    <!-- Rows -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">V5C Certificate Count</td>
                                        <td style="padding:8px;"><?= ($V5CCertificates && is_array($V5CCertificates)) ? count($V5CCertificates) : '***' ?></td>
                                        <td style="padding:8px; font-weight:bold;">Certificate Dates:</td>
                                        <td style="padding:8px;">
                                        <?php if (!empty($V5CCertificates) && is_array($V5CCertificates)): ?>
                                        <?php foreach ($V5CCertificates as $date): ?>
                                                                                    <?= !empty($date) ? date('d-M-y', strtotime($date)) : '' ?><br>
                                        <?php endforeach; ?>
                                        <?php endif; ?>
                                        </td>
                                    </tr>
                                   

                                </table>
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4aa3df; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fcff; padding:10px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4aa3df;">
                                            Vehicle Tax &amp; CO₂ Emissions
                                        </td>
                                    </tr>

                                    <!-- Rows -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Standard Six Month</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['StandardSixMonth']['Amount'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Per Month</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['StandardSixMonth']['PerMonth'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Standard Twelve Month</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['StandardTwelveMonth']['Amount'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Per Month</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['StandardTwelveMonth']['PerMonth'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">First Year Six Month</td>
                                        <td style="padding:8px;">---</td>
                                        <td style="padding:8px; font-weight:bold;">Per Month</td>
                                        <td style="padding:8px;">---</td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">First Year Twelve Month</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['First Year Twelve Month']['Amount'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Per Month</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['First Year Twelve Month']['PerMonth'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Premium Vehicle Year 2 to 6 - Six
                                            Month</td>
                                        <td style="padding:8px;">---</td>
                                        <td style="padding:8px; font-weight:bold;">Per Month</td>
                                        <td style="padding:8px;">---</td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Premium Vehicle Year 2 to 6 - Twelve
                                            Month</td>
                                        <td style="padding:8px;">---</td>
                                        <td style="padding:8px; font-weight:bold;">Per Month</td>
                                        <td style="padding:8px;">---</td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">VED CO₂ Emissions</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['VedCo2Emissions'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">VED Band</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['VedBand'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">VED CO₂ Band</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleTaxCo2Emissions['VedCo2Band'] ?? '***')) ?></td>
                                        <td style="padding:8px;"></td>
                                        <td style="padding:8px;"></td>
                                    </tr>

                                </table>
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4aa3df; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fcff; padding:10px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4aa3df;">
                                            Performance Information
                                        </td>
                                    </tr>

                                    <!-- Rows -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Torque FtLb</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Torque']['FtLb'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Power Rpm</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Power']['Rpm'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Torque Rpm</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Torque']['Rpm'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Power Bhp</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Power']['Bhp'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Torque Nm</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Torque']['Nm'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Power Kw</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Power']['Kw'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Co2</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Co2'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">NoiseLevel</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['NoiseLevel'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Max Speed Mph</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['MaxSpeed']['Kph'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Particles</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Particles'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Max Speed Kph</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['MaxSpeed']['Mph'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Data Version Number</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['DataVersionNumber'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Acceleration ZeroTo 60Mph</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Acceleration']['ZeroTo60Mph'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Acceleration Mph</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Acceleration']['Mph'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">Acceleration Zero To 100Kph</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Acceleration']['ZeroTo100Kph'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Acceleration Kph</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($performance['Acceleration']['Kph'] ?? '***')) ?></td>
                                    </tr>

                                </table>
                            </div>
                        </td>
                    </tr>


                    <!-- consumption -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4aa3df; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fcff; padding:10px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4aa3df;">
                                            Consumption
                                        </td>
                                    </tr>

                                    <!-- Rows -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Combined Mpg</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($consumption['Combined']['Mpg'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Combined Lkm</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($consumption['Combined']['Lkm'] ?? '***')) ?></td>
                                    </tr>
                                    <tr style="background:#f9f9f9;">
                                        <td style="padding:8px; font-weight:bold;">ExtraUrban Mpg</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($consumption['ExtraUrban']['Mpg'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">ExtraUrban Lkm</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($consumption['ExtraUrban']['Lkm'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">UrbanCold Mpg</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($consumption['UrbanCold']['Mpg'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">UrbanCold Lkm</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($consumption['UrbanCold']['Lkm'] ?? '***')) ?></td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                    <!-- Plate Change History -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4CAF50; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    
                                    <tr>
                                        <td colspan="2" align="center"
                                            style="background:#f9fff9; padding:12px; font-weight:bold; font-size:16px; color:#555; border-bottom:1px solid #4CAF50;">
                                            <span style="color:<?= !empty($vehicleInformation['plateChange']) ? '#F44336;' : '#f49320;' ?> font-size:18px;"><?= !empty($vehicleData['plateChange']) ? '<span style="font-size:21px; font-weight:bold;">!</span>' :'&#10004;'  ?></span>Plate Change History
                                        </td>
                                    </tr>

                                    <!-- Row -->
                                  
                                    <tr>
                                    <td style="padding:10px; font-weight:bold; width:50%;">Plate Change Count</td>
                                    <td style="padding:10px; text-align:center;"><?= count($vehicleMot['motTestList']['plateChange']) ?>  </td>
                                </tr>
                                <?php if(isset($vehicleMot['motTestList']['plateChange'])){  foreach($vehicleMot['motTestList']['plateChange'] as $pltchang){  ?>
                                <td><?= $pltchang['plate_number'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'." ".date('d-M-y', strtotime($pltchang['date'])) ?></td>
                                <td><div style="display:flex;align-items:center;gap: -2px;">
                                    <div style="display:flex;align-items:center;border: 1px solid #000;border-radius: 4px;overflow:hidden;font-family:Arial, sans-serif;font-weight:bold;">
                                    
                                        <div style="padding: 3px 6px;font-size: 10px;"><?= $pltchang['previous'] ?></div>
                                    </div>

                                    <!-- Arrow -->
                                    <span style="font-size: 10px;color:#003399;">→</span>

                                    <!-- Right Plate -->
                                    <div style="display:flex;align-items:center;border: 1px solid #000;border-radius: 4px;overflow:hidden;font-family:Arial, sans-serif;font-weight:bold;">
                                    
                                        <div style="padding: 3px 6px;font-size: 10px;"><?= $pltchang['current'] ?></div>
                                    </div>
                                </div></td>  
                              <?php } } ?>

                                </table>
                            </div>
                        </td>
                    </tr>

                    <!-- Import/Export -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4CAF50; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fff9; padding:12px; font-weight:bold; font-size:16px; color:#555; border-bottom:1px solid #4CAF50;">
                                            <span style="color:<?= !empty($importExport['exported'] || $importExport['imported']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($importExport['exported'] || $importExport['imported']) ? '&#10006;' : '&#10004;' ?></span> Import/Export
                                        </td>
                                    </tr>

                                    <!-- Exported Row -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Exported</td>
                                        <td style="padding:8px;"><?= !empty($importExport['exported']) ? 'Yes' : 'No' ?></td>
                                        <td style="padding:8px; font-weight:bold;">Export Date</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($importExport['dateExported'] ?? '***')) ?></td>
                                    </tr>

                                    <!-- Imported Row -->
                                    <tr>
                                        <td style="padding:10px; font-weight:bold;">Imported</td>
                                        <td style="padding:10px;"><?= !empty($importExport['imported']) ? 'Yes' : 'No' ?></td>
                                        <td style="padding:10px;"></td>
                                        <td style="padding:10px;"></td>
                                    </tr>

                                </table>
                            </div>
                        </td>
                    </tr>


                    <!-- scrpa destructions -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4CAF50; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fff9; padding:12px; font-weight:bold; font-size:16px; color:#555; border-bottom:1px solid #4CAF50;">
                                            <span style="color:<?= !empty($scrapDestruction['scrapped']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= !empty($scrapDestruction['scrapped']) ? '&#10006;' : '&#10004;' ?></span> Scrap Destructions
                                        </td>
                                    </tr>

                                    <!-- Exported Row -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Scrapped</td>
                                        <td style="padding:8px;"><?= !empty($scrapDestruction['scrapped']) ? 'Yes' : 'No' ?></td>
                                        <td style="padding:8px; font-weight:bold;">Date of Scapped</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($scrapDestruction['dateofScapped'] ?? '***')) ?></td>
                                    </tr>

                                    <!-- Imported Row -->
                                    <tr>
                                        <td style="padding:10px; font-weight:bold;">Certificate of Destruction Issued
                                        </td>
                                        <td style="padding:10px;"><?= !empty($scrapDestruction['certificateofDestructionIssued']) ? 'Yes' : 'No' ?></td>
                                        <td style="padding:10px;">Vehicle ID Check Count</td>
                                        <td style="padding:10px;"><?= htmlspecialchars((string)($scrapDestruction['vehicleIdCheckCount'] ?? '***')) ?></td>
                                    </tr>

                                </table>
                            </div>
                        </td>
                    </tr>


                    <!-- color changes -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4CAF50; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fff9; padding:12px; font-weight:bold; font-size:16px; color:#555; border-bottom:1px solid #4CAF50;">
                                            <span style="color:<?= ((string)($colourChange['CurrentColour'])!=(string)($colourChange['OriginalColour'])) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= ((string)($colourChange['CurrentColour'])!=(string)($colourChange['OriginalColour'])) ? '&#10006;' : '&#10004;' ?></span> Colour Changes
                                        </td>
                                    </tr>

                                    <!-- Exported Row -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Current Colour
                                        </td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($colourChange['CurrentColour'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Number Of Previous Colours</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($colourChange['NumberOfPreviousColours'] ?? '***')) ?></td>
                                    </tr>

                                    <!-- Imported Row -->
                                    <tr>
                                        <td style="padding:10px; font-weight:bold;">Last Colour</td>
                                        <td style="padding:10px;"><?= htmlspecialchars((string)($colourChange['LastColour'] ?? '***')) ?></td>
                                        <td style="padding:10px;">Date Of Last Colour Change</td>
                                        <td style="padding:10px;"><?= htmlspecialchars((string)($colourChange['DateOfLastColourChange'] ?? '***')) ?></td>
                                    </tr>
                                    <tr>
                                        <td style="padding:10px; font-weight:bold;">Original Colour</td>
                                        <td style="padding:10px;"><?= htmlspecialchars((string)($colourChange['OriginalColour'] ?? '***')) ?></td>
                                        <td style="padding:10px;"></td>
                                        <td style="padding:10px;"></td>
                                    </tr>

                                </table>
                            </div>
                        </td>
                    </tr>

                    <!-- Previous VRM -->
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:600px; border:1px solid #4CAF50; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="6" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fff9; padding:12px; font-weight:bold; font-size:16px; color:#555; border-bottom:1px solid #4CAF50;">
                                            <span style="color:<?= (string)($vehicleInformation['PreviousVrmNi']) ? '#F44336;' : '#4CAF50;' ?> font-size:18px;"><?= (string)($vehicleInformation['PreviousVrmNi']) ? '&#10006;' : '&#10004;' ?></span> Previous VRM
                                        </td>
                                    </tr>

                                    <!-- Exported Row -->
                                    <tr>
                                        <td style="padding:8px; font-weight:bold;">Previous Northern Ireland</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['PreviousVrmNi'] ?? '***')) ?></td>
                                        <td style="padding:8px; font-weight:bold;">Previous UK VRM</td>
                                        <td style="padding:8px;"><?= htmlspecialchars((string)($vehicleInformation['PreviousVrmGb'] ?? '***')) ?></td>
                                    </tr>

                                    <!-- Imported Row -->

                                </table>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:800px; border:1px solid #4DB6E2; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="8" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fdff; padding:12px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4DB6E2;">
                                            Dimensions
                                        </td>
                                    </tr>

                                    <!-- Data Rows -->
                                    <tr>
                                        <td style="font-weight:bold;">Gross Vehicle Weight</td>
                                        <td><?= htmlspecialchars((string)($dimensions['GrossVehicleWeight'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Height</td>
                                        <td><?= htmlspecialchars((string)($dimensions['Height'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Gross Combined Weight</td>
                                        <td><?= htmlspecialchars((string)($dimensions['GrossCombinedWeight'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Width</td>
                                        <td><?= htmlspecialchars((string)($dimensions['Width'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Kerb Weight</td>
                                        <td><?= htmlspecialchars((string)($dimensions['KerbWeight'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Car Length</td>
                                        <td><?= htmlspecialchars((string)($dimensions['CarLength'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Unladen Weight</td>
                                        <td><?= htmlspecialchars((string)($dimensions['UnladenWeight'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Wheel Base</td>
                                        <td><?= htmlspecialchars((string)($dimensions['WheelBase'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Fuel Tank Capacity</td>
                                        <td><?= htmlspecialchars((string)($dimensions['FuelTankCapacity'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Pay Load Weight</td>
                                        <td><?= htmlspecialchars((string)($dimensions['PayLoadWeight'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Number Of Doors</td>
                                        <td><?= htmlspecialchars((string)($dimensions['NumberOfDoors'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Gross Train Weight</td>
                                        <td><?= htmlspecialchars((string)($dimensions['GrossTrainWeight'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Number Of Seats</td>
                                        <td><?= htmlspecialchars((string)($dimensions['NumberOfSeats'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Payload Volume</td>
                                        <td><?= htmlspecialchars((string)($dimensions['PayloadVolume'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Rigid Artic</td>
                                        <td><?= htmlspecialchars((string)($dimensions['RigidArtic'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Load Length</td>
                                        <td><?= htmlspecialchars((string)($dimensions['LoadLength'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Number Of Axles</td>
                                        <td><?= htmlspecialchars((string)($dimensions['NumberOfAxles'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Data Version Number</td>
                                        <td><?= htmlspecialchars((string)($dimensions['DataVersionNumber'] ?? '***')) ?></td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">Body Shape</td>
                                        <td colspan="3"><?= htmlspecialchars((string)($dimensions['BodyShape'] ?? 'NA')) ?></td>
                                    </tr>

                                </table>
                            </div>
                        </td>
                    </tr>




                    <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:800px; border:1px solid #4DB6E2; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="8" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fdff; padding:12px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4DB6E2;">
                                            Safety
                                        </td>
                                    </tr>

                                    <!-- Data Rows -->
                                    <tr>
                                        <td style="font-weight:bold;">Adult</td>
                                        <td><?= htmlspecialchars((string)($safety['EuroNcap']['Adult'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;">Child</td>
                                        <td><?= htmlspecialchars((string)($safety['EuroNcap']['Child'] ?? '***')) ?></td>
                                    </tr>
                                   
                                    <tr>
                                        <td style="font-weight:bold;">Pedestrian</td>
                                        <td><?= htmlspecialchars((string)($safety['EuroNcap']['Pedestrian'] ?? '***')) ?></td>
                                        <td style="font-weight:bold;"></td>
                                        <td></td>
                                    </tr>

                                </table>
                            </div>
                        </td>
                    </tr>
                    {{-- classificaio n details --}}
                         <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:800px; border:1px solid #4DB6E2; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="8" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fdff; padding:12px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4DB6E2;">
                                            Classification Details
                                        </td>
                                    </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">SMMT Make</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Smmt']['Make'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">DVLA Make</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Dvla']['Make'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">SMMT Range</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Smmt']['Range'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">DVLA Model</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Dvla']['Model'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">SMMT Trim</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Smmt']['Trim'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Is Electric Vehicle</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Ukvd']['IsElectricVehicle'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">SMMT Mvris MakeCode</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Smmt']['Mvris']['MakeCode'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Is GB General Format</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['VrmFormat']['IsGBGeneralFormat'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">SMMT Mvris ModelCode</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['Mvris']['ModelCode'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Is NI General Format</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($classificationDetails['VrmFormat']['IsNIGeneralFormat'] ?? '***')) ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>


        <!-- Smmt Details -->
          <tr>
                        <td align="center" style="padding-top:20px;">
                            <div
                                style="max-width:800px; border:1px solid #4DB6E2; border-radius:8px; overflow:hidden; background:#ffffff; margin:0 auto;">
                                <table width="100%" cellpadding="8" cellspacing="0" border="0"
                                    style="font-size:13px; font-family:Arial,sans-serif; color:#333; background:#ffffff; border-collapse:collapse;">

                                    <!-- Header -->
                                    <tr>
                                        <td colspan="4" align="center"
                                            style="background:#f9fdff; padding:12px; font-weight:bold; font-size:16px; color:#444; border-bottom:1px solid #4DB6E2;">
                                            Smmt Details
                                        </td>
                                    </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Marque</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['Marque'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Range</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['Range'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Model Variant</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['ModelVariant'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Fuel Type</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['FuelType'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Nominal Engine Capacity</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['NominalEngineCapacity'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Transmission</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['Transmission'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Engine Capacity</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['EngineCapacity'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Number Of Doors</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['NumberOfDoors'] ?? '***')) ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Body Style</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['BodyStyle'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Number Of Gears</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['NumberOfGears'] ?? '***')) ?>
                            </td>
                        </tr>

                         <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">DriveType</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['DriveType'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">SysSetup Date</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['SysSetupDate'] ?? '***')) ?>
                            </td>
                        </tr>
                         <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Country Of Origin</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['CountryOfOrigin'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Visibility Date</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['VisibilityDate'] ?? '***')) ?>
                            </td>
                        </tr>
                         <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Series</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['Series'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">TerminateDate</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['TerminateDate'] ?? '***')) ?>
                            </td>
                        </tr>
                         <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Marque Code</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['MarqueCode'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Cab Type</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['CabType'] ?? '***')) ?>
                            </td>
                        </tr>
                         <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Market Sector Code</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['MarketSectorCode'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Data Version Number</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['DataVersionNumber'] ?? '***')) ?>
                            </td>
                        </tr>
                         <tr>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;">Model Code</td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;">
                                <?= htmlspecialchars((string)($smmtDetails['ModelCode'] ?? '***')) ?>
                            </td>
                            <td style="font-weight: bold;padding:10px;width:30%;
                                    font-size:14px;"></td>
                            <td style="text-align:right;padding:10px;width:20%;
                                    font-size:14px;"></td>
                        </tr>
                    </table>
                </td>
            </tr>


                    <!-- MOT Test List -->


  <!-- SECTION 4: MOT Test List -->
                    <tr>
                        <td align="center"
                            style="padding-top:20px; padding-bottom:15px; font-size:18px; color:#555555; font-weight:bold;">
                            MOT Test List
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table cellpadding="6" cellspacing="0" border="1" width="100%"
                                style="border-collapse:collapse; font-size:13px; border-color:#ccc;">
                                <tr style="background:#777; color:#ffffff; font-weight:bold; text-align:center;">
                                    <td>Date</td>
                                    <td>Result</td>
                                    <td>Mileage</td>
                                    <td>Test Number</td>
                                </tr>
                                <?php foreach (($vehicleMot['motTestList']['motList'] ?? []) as $test): ?>
                                <tr style="background:#f9fff9;">
                                    <td><?= htmlspecialchars((string)($test['date'] ?? '')) ?></td>
                                    <td style="font-weight:bold; color:<?= (isset($test['result']) && strtoupper($test['result']) === 'PASSED') ? '#4CAF50' : '#f44336' ?>;">
                                        <?= (isset($test['result']) && strtoupper($test['result']) === 'PASSED') ? '<span style="color:#4CAF50; font-size:18px;">&#10004;</span> PASS' : '❌ FAIL' ?>
                                    </td>
                                    <td><?= htmlspecialchars((string)($test['mileage'] ?? '')) ?> <?= htmlspecialchars((string)($test['unit'] ?? '')) ?></td>
                                    <td><b><?= htmlspecialchars((string)($test['motTesNumber'] ?? '')) ?></b></td>
                                </tr>
                                <tr  style="padding-bottom: 500px">
                                <?php
                                    $isPass = isset($test['result']) && strtoupper($test['result']) === 'PASSED';
                                    $year = '';
                                    if (!empty($test['date'])) {
                                        $ts = strtotime($test['date']);
                                        $year = $ts ? date('Y', $ts) : '';
                                    }
                                ?>
                                    <td colspan="5" style="padding:12px; border:1px solid #eee;background-color:<?=$isPass? '#cefdd0':'#fdc9c5'?>">
                                        <div style=" margin-bottom:8px;">
                                            <div style=" 
                                            border-radius:5px; 
                                            padding: 5px 8px;
                                            display:inline-block; 
                                            background:<?= $isPass ? '#4CAF50' : '#f44336' ?>; 
                                            color:white; 
                                            font-weight:bold; 
                                            font-size:12px;">
                                    <?= $isPass ? 'PASS' : 'FAIL' ?><?= $year ? ' - ' . htmlspecialchars((string)$year) : '' ?>
                                </div>
                                        {{-- </div>
                                        <div style="margin-bottom:6px;"> --}}
                                            <span style="float: right;font-size: 14px;"><b>Recorded Mileage:</b> <?= htmlspecialchars((string)($test['mileage'] ?? '')) ?> <?= htmlspecialchars((string)($test['unit'] ?? '')) ?></span>
                                        </div>
                                        <?php if (!empty($test['advisory'])): ?>
                                        <div style="font-weight:bold; margin-bottom:4px;">Advisories:</div>
                                            <?php foreach ($test['advisory'] as $adv): ?>
                                        <div style="margin-bottom: 2px;">
                                            <?php if (!empty($adv['type'])): ?> <span style="background-color:#f19e23; color:#fff; padding:1px 6px; border-radius:3px; font-size:11px;"><?= htmlspecialchars((string)$adv['type']) ?></span><?php endif; ?> <?= htmlspecialchars((string)($adv['text'] ?? '')) ?>
                                        </div>
                        <?php endforeach; ?>
                            <?php endif; ?>

                                    <?php if (!empty($test['prsText'])): ?>
                                        <div style="font-weight:bold; margin:6px 0 4px;">PRS:</div>
                                <?php foreach ($test['prsText'] as $prs): ?>
                                        <div>
                                            <span style="background-color:#297dc5; color:#fff; padding:1px 6px; border-radius:3px; font-size:11px;">PRS</span> <?= htmlspecialchars((string)($prs['text'] ?? '')) ?> 
                                        </div>
                                        <?php endforeach; ?>
                                <?php endif; ?>

                                        <?php if (!empty($test['failure'])): ?>
                                        <div style="font-weight:bold; margin:6px 0 4px;">Failures:</div>
                                    <?php foreach ($test['failure'] as $fail): ?>
                                        <div>
                                            <?php if (!empty($fail['type'])): ?> <span style="background-color:#d9534f; color:#fff; padding:1px 6px; border-radius:3px; font-size:11px;"><?= htmlspecialchars((string)$fail['type']) ?></span><?php endif; ?> <?= htmlspecialchars((string)($fail['text'] ?? '')) ?>
                                        </div>
                                <?php endforeach; ?>
                                                <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </table>
                        </td>
                    </tr>
                    
                    <br>


                    <!-- Disclaimer Section -->
                    <tr>
                    <td align="center" style="padding-top:10px;">
                        <div style="max-width:800px; background:#f0f8fa; padding:25px; border-radius:8px; margin:0 auto; font-family:Arial, sans-serif; font-size:13px; line-height:1.6; color:#333;">
                        
                        <div style="text-align:center; font-weight:bold; font-size:15px; margin-bottom:15px;">
                            DISCLAIMER
                        </div>
                        
                        <div style="text-align:left;">
                            CarBaBa provides free vehicle data but does not guarantee it's precision. Although we strive to maintain up-to-date and accurate information, we are not responsible for any discrepancies, errors, or omissions. Users should independently verify details before making transactions and assume complete responsibility and liability for how they utilize the data.
                            <br><br>
                            Our platform is intended for general public use and not for commercial purposes. Traders should seek paid data services from providers like Experian or HPI.
                            <br><br>
                            We reserve the right to modify or discontinue vehicle data at any time. Users cannot demand continued access to this free service.
                            <br><br>
                            Additionally, our data cannot be used as legal evidence in any court or official capacity.
                        </div>

                        </div>
                    </td>
                    </tr>



                </table>
                </div>
            </td>
        </tr>
    </table>

</body>

</html>
