<?php
if (file_exists('../../../' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('../../../' . $_REQUEST['tenant'] . '/config.php');
    echo '1';
}
if (file_exists('/config.php')) {
    require_once('/config.php');
    echo '2';
}
if (!file_exists('../../../' . $_REQUEST['tenant'] . '/output/reports/' . $_REQUEST['clientnumber'])) {
    mkdir('../../../' . $_REQUEST['tenant'] . '/output/reports/' . $_REQUEST['clientnumber']);
}

require_once('../../includes/master/form_functions.php');
//$_REQUEST['repnumber']=1499725635;
//$_REQUEST['item']=315;
//$_REQUEST['form']=503;
//echo $_REQUEST['form'];

$rx = mysqli_query($GLOBALS['con'], "SELECT * FROM `reportitems` WHERE `repnumber`='" . $_REQUEST['repnumber'] . "' AND `itemnumber`='" . $_REQUEST['items'] . "' ORDER BY abs(insdate) DESC LIMIT 1") or die(mysqli_error($GLOBALS['con']));
$cx = mysqli_fetch_array($rx);
$rd = enrepReportDataFromRow($cx ?? []);

$itemcheck = mysqli_query($GLOBALS['con'], "SELECT * FROM `itemdetails` WHERE `itemnumber`='" . $cx['itemnumber'] . "'") or die(mysqli_error($GLOBALS['con']));
$itemcode = mysqli_fetch_array($itemcheck);

if (empty($cx['frequency'])) {
    $freq = getFreq($itemcode['code']);
} else {
    $freq = $cx['frequency'];
}

$contract = getItemOwner($cx['itemnumber']);
$clt = explode("|", $contract);
$details = getItemDetailed($_REQUEST['repnumber'], $cx['itemnumber']);
$det = explode("|", $details);
// Path to the custom header file
$custom_header_file = '../../../' . $_REQUEST['tenant'] . '/output/custom_header.php';

// Check if the file exists and has content
if (file_exists($custom_header_file) && filesize($custom_header_file) > 0) {
    // Include the content of the custom header file
    ob_start();
    include($custom_header_file);
    $post = ob_get_clean(); // Capture the output of the included file
} else {
    // Default $post content if the custom file is not found or is empty
    $post = '
    <table class="tableClientDetails" width="100%">
    <tr valign="top">
    <td width="30%" valign="top">
    <table width="100%">
    <tr><th width="25%" class="themeColour">Owner</th><td>' . strtoupper($clt[0]) . '</td></tr>
    <tr valign="top"><th valign="top"></th><td valign="top">' . getClientAddress($cx['clientnumber'], 'address') . '</td></tr>
    </table>
    </td>

    <td valign="top" width="30%">
    <table id="clientDetails" width="100%">
    <tr valign="top"><th width="25%" class="themeColour">Site</th><td>' . strtoupper(getItemLocation($cx['itemnumber'], 'name')) . '</td></tr>
    <tr valign="top"><th valign="top"></th><td valign="top">' . getItemLocation($cx['itemnumber'], 'address') . '</td></tr>
    </table>
    </td>

    <td valign="top">
    <table id="repDetails" class="table-sm" width="100%">
    <tr><td align="right"><i>' . nl2br(getItemReportDesc($_REQUEST['repnumber'], 'regs')) . '</i></td></tr>
    <tr><td align="right" style="font-size:12px" class="themeColour"><strong>' . getItemReportDesc($_REQUEST['repnumber'], 'formtype') . '</strong></td></tr>
    <tr><td align="right"><small><i>Contract Number:</i> ' . $clt[1] . '</small></td></tr>
    <tr><td align="right"><small><i>Report Number:</i> ' . $_REQUEST['repnumber'] . '</small></td></tr>
    <tr><td align="right"><small><i>Item Ref:</i> ' . $cx['itemnumber'] . '</small></td></tr>
    </table>
    </td>
    </tr>
    </table>';
}

$dynamicCases = include('../../..' . TENANT_URL . '/output/custom_reports.php');
include('../../..' . TENANT_URL . '/output/custom_images.php');
$caseValue = $_REQUEST['form'];
switch ($_REQUEST['form']) {
    default:
        // Check if the case value exists in the dynamic cases
        if (isset($dynamicCases[$caseValue])) {
            $dynamicCase = $dynamicCases[$caseValue];

            // Call the dynamic case as a function to generate content
            $caseData = $dynamicCase($cx, $det, $freq);

            // Use the returned array from the dynamic case
            $inst = $caseData['inst'];
            $pdf = $caseData['pdf'];
            $footerTemplate = $caseData['footerTemplate'];
        } else {
            echo "No matching case found.";
        }
        break;

    case "101": //OPEN DOCUMENT
        $content = urldecode(urldecode($rd['obs'] ?? ''));

    // Only affect top-level <p>, not <p> inside tables
    $pdf = '
 <table class="table table-sm tableDetails" width="100%">
            <tr>
<td >' . $content . '</td>
</tr>
        </table>';

        $footerTemplate = "footer.php";
        break;
    ///////////////////////////////////////////////////////////////////////////////////////////////////////		


    case "500": //THOROUGH REPORT
        if ($rd['nature'] == "") {
            $rd['nature'] = "In-Service Thorough Examination";
        }

        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th width="20%" class="themeColour">Item description</th><td width="45%">' . $det[0] . '</td><th width="20%" class="themeColour">Make</th><td width="15%">' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td></tr>
<tr><th class="themeColour">Nature of Examination</th><td>' . $rd['nature'] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
<tr><th class="themeColour">Location on site</th><td colspan="3">' . $det[6] . '</td></tr>
<tr><th class="themeColour">Safe Working Load</th><td colspan="3">' . $det[10] . '</td></tr>
</table>
';


        $pdf = '
<div style="display:block; border-top: 1px solid #06C; margin-top:20px;  margin-bottom:20px"></div>
<table id="repText" style="width:100%">
<tr><th align="left" class="subtitle"><strong><small>Defects which are, or could become a danger to persons, the remedial action, and the time in which the defects should be remedied:</small></strong><br></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['adefect']) . '</td></tr>
<tr><td style="display:block; padding-top:20px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Other defects requiring attention:</small></strong><br></th></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['bdefect']) . '</td></tr>
<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Observations and Recommendations:</small></strong><br></th></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['obs']) . '</td></tr>
</table>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	

    case "502": //PARTIAL REPORT
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Make</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td></tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
<tr><th class="themeColour">Safe Working Load</th><td colspan="3">' . $det[10] . '</td></tr>
</table>

<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>
';

        $pdf = '

<table id="repText" style="width:100%">
<tr><th align="left" class="subtitle"><strong><small>Reason for being partial:</small></strong></th></tr>
<tr><td>' . $rd['partialReason'] . '</td></tr>
<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Defects which are, or could become a danger to persons, the remedial action, and the time in which the defects should be remedied:</small></strong></th></tr>
<tr><td>' . nl2br($rd['adefect']) . '</td></tr>
<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Other defects requiring attention:</small></strong></th></tr>
<tr><td>' . nl2br($rd['bdefect']) . '</td></tr>
<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Observations and Recommendations:</small></strong></th></tr>
<tr><td>' . nl2br($rd['obs']) . '</td></tr>
</table>

';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	

    case "503": //PLANT NOT AVAILABLE no
        $inst = '

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Location on site</th><td>' . $det[6] . '</td></tr>
<tr><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td></tr>
<tr><th class="themeColour">Safe Working Load</th><td colspan="3">' . $det[10] . '</td></tr>
</table>
';


        $pdf = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:20px;  margin-bottom:20px"></div>
<table id="repText" style="width:100%">
<tr><th class="subtitle"><strong>Reason(s) for being reported as "Not available":</strong></th></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['pnareason']) . '</td></tr>
</table>
';

        $footerTemplate = "footer_pna.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    case "504": //NONE STAT REPORT no
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Make</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td></tr>
<tr><th class="themeColour">Location on site</th><td colspan="3">' . $det[6] . '</td></tr>
<tr><th class="themeColour">Safe Working Load</th><td colspan="3">' . $det[10] . '</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>
';

        $pdf = '

<table id="repText" style="width:100%">
<tr><th align="left" class="subtitle"><strong><small>Defects which are, or could become a danger to persons, the remedial action, and the time in which the defects should be remedied:</small></strong><br></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['adefect']) . '</td></tr>
<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Other defects requiring attention:</small></strong><br></th></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['bdefect']) . '</td></tr>
<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Observations and Recommendations:</small></strong><br></th></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['obs']) . '</td></tr>
</table>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	

    case "506": //FREE REPORT
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Make</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td></tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
<tr><th class="themeColour">Safe Working Load</th><td colspan="3">' . $det[10] . '</td></tr>
</table>

<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>
';

        $pdf = '

<table class="table table-sm tableDetails" width="100%" cellspacing="5">
<tr><th class="subtitle"><strong><small>Defects which are, or could become a danger to persons, the remedial action, and the time in which the defects should be remedied:</small></strong><br></tr>
<tr><td>' . nl2br($rd['adefect']) . '</td></tr>

<tr><td style="display:block; padding-top:20px;  padding-bottom:20px"></td></tr>

<tr><th class="subtitle"><strong><small>Other defects requiring attention:</small></strong><br></th></tr>
<tr><td>' . nl2br($rd['bdefect']) . '</td></tr>

<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>

<tr><th class="subtitle"><strong><small>Observations and Recommendations:</small></strong><br></th></tr>
<tr><td>' . nl2br($rd['obs']) . '</td></tr>
</table>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	

    case "507": //PALLET RACKING
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td></tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
</table>
';


        $pdf = '
<p><small>On the date shown, our engineer surveyor undertook examination of the racking installation detailed above. The following pages are a report of our findings.</small></p>
<br>
<p class="subtitle"><small><strong>Installation overview:</strong></small></p>

<table class="table table-sm tableDetails" width="100%" cellspacing="5">
<tr><th class="themeColour" width="20%">Rack / Run identity</th><td width="30%">' . $rd['rack'] . '</td><th width="20%" class="themeColour">Mechanical handling equipment in use</th><td>' . $rd['handling'] . '</td></tr>
<tr><th class="themeColour">Rack type and Arrangement</th><td>' . $rd['rackRun'] . '</td><th class="themeColour">Stability</th><td>' . $rd['stability'] . '</td></tr>
<tr><th class="themeColour">Beam spacing</th><td>' . $rd['beam'] . '</td><th class="themeColour">Safety Notices</th><td>' . $rd['safety'] . '</td></tr>
<tr><th class="themeColour">Accessories</th><td>' . $rd['accessories'] . '</td><th class="themeColour">Parts Inaccessable</th><td>' . $rd['partsInaccessible'] . '</td></tr>
</table>

<br>
<p align="center" style="background-color:#ffbb33; padding:10px"><small>Modification, adjustment or removal of beams or changes to the parameters governing strength and stability, or other physical alterations to the uprights or bracings should not be made without reference to the manufacturer or installer.</small></p>
<br>
<p class="subtitle"><small><strong>Conventions used in this report:</strong></small></p>
<p><small>
Where no bay identification system is employed the following bay description is utilised<br>
Bays viewed from Left to Right when facing the racking<br>
Bays numbered Left to Right and shelves from Ground to Ceiling when facing the racking
</small></p>
';
        $pdf .= "<pagebreak>";

        $pdf .= "<p class='subtitle'><small><strong>Defect Categories:</strong></small></p>
<p><small>In the following report reference is made to defective parts only. Where no mention is made, no defects have been found. Defects have been categorised in accordance with the Storage Equipment Manufacturers Association (SEMA) Guide to The Conduct of Pallet Racking Shelving Surveys. </small></p>
<br>
<p><small><span style='color:red'>Category A</span><br>Items which are damaged beyond the limitations of the SEMA guide. In such circumstances the racking should be IMMEDIATELY off-loaded and isolated from further use until repairs have been completed.</p>

<p><small><span style='color:orange'>Category B</span><br>Items which are damaged beyond the limitations of the SEMA guide, but not sufficiently serious enough to warrant immediate off-loading of the racking.</p>

<p><small><span style='color:green'>Category C</span><br>Items which are damaged but remain within the limitations of the SEMA guide. Other observations and recommendations.</p>

<br>

<p class='subtitle'><small><strong>Defects found during this inspection:</strong></small></p>

<br>
<p><small><span style='color:red'>Category A</span><br>" . nl2br($rd['adefect']) . "</p>

<p><small><span style='color:orange'>Category B</span><br>" . nl2br($rd['bdefect']) . "</p>

<p><small><span style='color:green'>Category C</span><br>" . nl2br($rd['obs']) . "</p>

";
        $pdf .= "<pagebreak>";

        if (file_exists('../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_1.jpg')) {
            $pdf .= '<table class="table table-sm tableDetails" width="100%" cellspacing="5"><tr><td align="center"><img src="../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_1.jpg" ></td></tr></table>';
        }
        if (file_exists('../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_2.jpg')) {
            $pdf .= '<table class="table table-sm tableDetails" width="100%" cellspacing="5"><tr><td align="center"><img src="../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_2.jpg" ></td></tr></table>';
        }
        if (file_exists('../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_3.jpg')) {
            $pdf .= '<table class="table table-sm tableDetails" width="100%" cellspacing="5"><tr><td align="center"><img src="../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_3.jpg" ></td></tr></table>';
        }
        if (file_exists('../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_4.jpg')) {
            $pdf .= '<table class="table table-sm tableDetails" width="100%" cellspacing="5"><tr><td align="center"><img src="../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_4.jpg" ></td></tr></table>';
        }
        if (file_exists('../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_5.jpg')) {
            $pdf .= '<table class="table table-sm tableDetails" width="100%" cellspacing="5"><tr><td align="center"><img src="../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_5.jpg" ></td></tr></table>';
        }
        if (file_exists('../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_6.jpg')) {
            $pdf .= '<table class="table table-sm tableDetails" width="100%" cellspacing="5"><tr><td align="center"><img src="../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_6.jpg" ></td></tr></table>';
        }

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	

    case "508": //CRANE REPORT
        if ($rd['nature'] == "") {
            $rd['nature'] = "In-Service Thorough Examination";
        }

        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Make</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td></tr>
<tr><th class="themeColour">Nature of Examination</th><td>' . $rd['nature'] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
<tr><th class="themeColour">Location on site</th><td colspan="3">' . $det[6] . '</td></tr>
<tr><th class="themeColour">Safe Working Load</th><td colspan="3">' . $det[10] . '</td></tr>
</table>
';


        $pdf = '
<div style="display:block; border-top: 1px solid #06C; margin-top:20px;  margin-bottom:20px"></div>
<table id="repText" style="width:100%">
<tr><th align="left" class="subtitle"><strong><small>Defects which are, or could become a danger to persons, the remedial action, and the time in which the defects should be remedied:</small></strong><br></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['adefect']) . '</td></tr>
<tr><td style="display:block; padding-top:20px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Other defects requiring attention:</small></strong><br></th></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['bdefect']) . '</td></tr>
<tr><td style="display:block; padding-top:0px;  padding-bottom:20px"></td></tr>
<tr><th align="left" class="subtitle"><strong><small>Test Details and Observations:</small></strong><br></th></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>' . nl2br($rd['obs']) . '</td></tr>
</table>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	

    case "601": //PRESSURE SYSTEM THOROUGH
        $rx = explode("|", $rd['protectiveDevices']);
        if ($rx[1] != "") {
            $rd['protectiveDevices'] = $rx[0] . " - " . $rx[1];
        } else {
            $rd['protectiveDevices'] = $rx[0];
        }
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Make</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
<tr><th class="themeColour">Safe Operating Limit</th><td colspan="3">' . $det[10] . '</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;"></div>
';



        $it = mysqli_query($GLOBALS['con'], "SELECT * FROM `itemdetails` WHERE `itemnumber`='" . $cx['itemnumber'] . "' ") or die(mysqli_error($GLOBALS['con']));
        $i = mysqli_fetch_array($it);
        $pdf = '
<div class="tableDetails">
<table class="table table-bordered" width="100%" style="text-align:left" cellspacing="7">
<tr><th class="themeColour" width="50%">Written Scheme Number</th><td>' . getWSNo($cx['itemnumber']) . '</td></tr>
<tr><th class="themeColour">Identification of System</th><td>' . ucwords($i['system_type']) . '</td></tr>
<tr><th class="themeColour">Is the system subject to lease/hire arrangements?<br><small>(i.e. Written agreement under schedule 2)</small></th><td>' . $rd['lease_or_hire'] . '</td></tr>
<tr><th class="themeColour">Is any change advised in the scheme of examination</th><td>' . $rd['changeScheme'] . '</td></tr>
<tr><th class="themeColour">Result of examination</th><td>' . $rd['resultOfExam'] . '</td></tr>
<tr><th class="themeColour">Date beyond which the system may not be operated without further examination under the Written Scheme</th><td>' . date('d-m-Y', $cx['nextDate']) . '</td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="7">
<tr><th class="themeColour" width="5%">1</th><th class="themeColour" width="45%">Relevant history including test and dates</th><td>' . nl2br($rd['history']) . '</td></tr>
<tr><th class="themeColour">2</th><th class="themeColour">State whether last report seen</th><td>' . $rd['lastReportSeen'] . '</td></tr>
<tr><th class="themeColour">3</th><th class="themeColour">Type of examination and test(s) undertaken</th><td>' . $rd['examType'] . '</td></tr>
<tr><th class="themeColour">4</th><th class="themeColour">Parts covered but requiring periodic examination and date of last examination</th><td>' . nl2br($rd['partsCovered']) . '</td></tr>
<tr><th class="themeColour">5</th><th class="themeColour">Parts inaccessible <small>(other than 4)</small></th><td>' . nl2br($rd['partsInaccessible']) . '</td></tr>

<tr><th class="themeColour">6</th><th class="themeColour" colspan="2">Condition of</th></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">a. Internal Parts</th><td>' . nl2br($rd['conditionInternal']) . '</td></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">b. External Parts</th><td>' . nl2br($rd['conditionExternal']) . '</td></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">c. Fittings</th><td>' . nl2br($rd['conditionFittings']) . '</td></tr>

<tr><th class="themeColour">7</th><th class="themeColour">a. Safe operating limits</th><td>' . $i['swl'] . '</td></tr>
<tr><th class="themeColour"></th><th class="themeColour">b. Are protective devices operating in order to maintain the system parts within the safe operating limits?</th><td>' . nl2br($rd['protectiveDevices']) . '</td></tr>

<tr><th class="themeColour">8</th><th class="themeColour">a. Repairs or modifications (if any) and date to be completed</th><td>' . nl2br($rd['repair_details']) . '</td></tr>

<tr><th class="themeColour"></th><th class="themeColour" colspan="2">b. Are changes to the Safe Operating Limits required?</th></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">i. Before the expiration of the period specified in 8a</th><td>' . $rd['9b1'] . '</td></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">ii. After the expiration of such period if the required repairs have not been completed</th><td>' . $rd['9b2'] . '</td></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">iii. After the completion of the required repairs</th><td>' . $rd['9b3'] . '</td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="5">
<tr><th class="themeColour">Other Observations</th></tr>
<tr><td>' . nl2br($rd['obs']) . '</td></tr>
</table>
</div>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    case "602": //PRESSURE SYSTEM NONE STAT
        $rx = explode("|", $rd['protectiveDevices']);
        if ($rx[1] != "") {
            $rd['protectiveDevices'] = $rx[0] . " - " . $rx[1];
        } else {
            $rd['protectiveDevices'] = $rx[0];
        }
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Make</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
<tr><th class="themeColour">Safe Operating Limit</th><td colspan="3">' . $det[10] . '</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:10px"></div>
';


        $it = mysqli_query($GLOBALS['con'], "SELECT * FROM `itemdetails` WHERE `itemnumber`='" . $cx['itemnumber'] . "' ") or die(mysqli_error($GLOBALS['con']));
        $i = mysqli_fetch_array($it);
        $pdf = '
<div class="tableDetails">
<table class="table table-bordered" width="100%" style="text-align:left" cellspacing="7">
<tr><th class="themeColour" width="50%">Written Scheme Number</th><td>' . getWSNo($cx['itemnumber']) . '</td></tr>
<tr><th class="themeColour" width="50%">Identification of System</th><td>' . $i['system_type'] . '</td></tr>
<tr><th class="themeColour">Is the system subject to lease/hire arrangements?<br><small>(i.e. Written agreement under schedule 2)</small></th><td>' . $rd['lease_or_hire'] . '</td></tr>
<tr><th class="themeColour">Result of examination</th><td>' . $rd['resultOfExam'] . '</td></tr>
<tr><th class="themeColour">Date beyond which the system may not be operated without further examination under the Written Scheme</th><td>' . date('d-m-Y', $cx['nextDate']) . '</td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="7">
<tr><th class="themeColour" width="5%">1</th><th class="themeColour" width="45%">Relevant history including test and dates</th><td>' . nl2br($rd['history']) . '</td></tr>
<tr><th class="themeColour">2</th><th class="themeColour">State whether report last seen</th><td>' . $rd['lastReportSeen'] . '</td></tr>
<tr><th class="themeColour">3</th><th class="themeColour">Type of examination and test(s) undertaken</th><td>' . $rd['examType'] . '</td></tr>
<tr><th class="themeColour">4</th><th class="themeColour">Parts covered but requiring periodic examination and date of last examination</th><td>' . nl2br($rd['partsCovered']) . '</td></tr>
<tr><th class="themeColour">5</th><th class="themeColour">Parts inaccessible <small>(other than 4)</small></th><td>' . nl2br($rd['partsInaccessible']) . '</td></tr>

<tr><th class="themeColour">6</th><th class="themeColour" colspan="2">Condition of</th></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">a. Internal Parts</th><td>' . nl2br($rd['conditionInternal']) . '</td></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">b. External Parts</th><td>' . nl2br($rd['conditionExternal']) . '</td></tr>
<tr><th class="themeColour"></th><th class="themeSecondary">c. Fittings</th><td>' . nl2br($rd['conditionFittings']) . '</td></tr>

<tr><th class="themeColour">7</th><th class="themeColour">a. Safe operating limits</th><td>' . $i['swl'] . '</td></tr>
<tr><th class="themeColour"></th><th class="themeColour">b. Are protective devices operating in order to maintain the system parts within the safe operating limits?</th><td>' . nl2br($rd['protectiveDevices']) . '</td></tr>

<tr><th class="themeColour">8</th><th class="themeColour">a. Repairs or modifications (if any) and date to be completed</th><td>' . nl2br($rd['repair_details']) . '</td></tr>

<tr><th></th><th class="themeColour" colspan="2">b. Are changes to the Safe Operating Limits required?</th></tr>
<tr><th></th><th class="themeSecondary">i. Before the expiration of the period specified in 8a</th><td>' . $rd['9b1'] . '</td></tr>
<tr><th></th><th class="themeSecondary">ii. After the expiration of such period if the required repairs have not been completed</th><td>' . $rd['9b2'] . '</td></tr>
<tr><th></th><th class="themeSecondary">iii. After the completion of the required repairs</th><td>' . $rd['9b3'] . '</td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="5">
<tr><th class="themeColour">Other Observations</th></tr>
<tr><td>' . nl2br($rd['obs']) . '</td></tr>
</table>
</div>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////


    case "700": //LEV PERIODICAL
        $it = mysqli_query($GLOBALS['con'], "SELECT * FROM `itemdetails` WHERE `itemnumber`='" . $cx['itemnumber'] . "' ") or die(mysqli_error($GLOBALS['con']));
        $i = mysqli_fetch_array($it);
        $fd = explode("|", $i['fan_details']);

        if ($rd['test_procedure'] == "") {
            $rd['test_procedure'] = $rd['examType'];
        }
        if ($rd['process_conditions'] == "") {
            $rd['process_conditions'] = $rd['processConditions'];
        }
        if ($rd['effective'] == "") {
            $rd['effective'] = $rd['adefect'];
        }
        if ($rd['particulars'] == "") {
            $rd['particulars'] = $rd['bdefect'];
        }
        if ($rd['other_comments'] == "") {
            $rd['other_comments'] = $rd['obs'];
        }
        if ($rd['usage_comments'] == "") {
            $rd['usage_comments'] = $rd['partialReason'];
        }
        if ($rd['noise'] == "") {
            $rd['noise'] = $rd['nature'];
        }
        if ($rd['intended_performance'] == "") {
            $rd['intended_performance'] = $rd['partsInaccessible'];
        }
        if ($rd['achieving_perfomance'] == "") {
            $rd['achieving_perfomance'] = $rd['partsInaccessible'];
        }

        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; margin-bottom:10px; width:100%"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Manufacturer</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; width:100%"></div>
';


        $pdf = '
<table class="table table-sm" width="100%">
<tr><td>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour">Process Requiring LEV</th><td>' . $i['process'] . '</td></tr>
<tr><th class="themeColour">Work Exposure Limits of Hazardous Substance Elements</th><td>' . $i['limits'] . '</td></tr>
<tr><th class="themeColour">Extraction Points in use Simultaniously</th><td>' . $i['number_in_use'] . '</td></tr>
</table>
</td>

<td>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour">Hazardous Substance Requiring Control</th><td>' . $i['substance'] . '</td></tr>
<tr><th class="themeColour">Number of extraction Points</th><td>' . $i['number_of_points'] . '</td></tr>
<tr><th class="themeColour">Exhaust Air Returned To</th><td>' . $i['returned_to'] . '</td></tr>
</table>
</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:20px;  margin-bottom:10px; width:100%"></div>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr>
<th class="themeColour">Fan Details:</th><td>' . $fd[0] . '</td>
<th class="themeColour">Fan Volume Flow:</th><td>' . $fd[1] . '</td>
<th class="themeColour">Fan Direction of Rotation:</th><td>' . $fd[2] . '</td>
</tr>
<tr>
<th class="themeColour">Motor Details:</th><td>' . $i['motor_details'] . '</td>
<th class="themeColour">Pri. Filter:</th><td>' . $i['pri_filter'] . '</td>
<th class="themeColour">Sec. Filter:</th><td>' . $i['sec_filter'] . '</td>
</tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:20px; width:100%"></div>

<h4 class="subtitle">Examination Results</h4>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="50%">Test procedure employed</th><td>' . $rd['test_procedure'] . '</td></tr>
<tr><th class="themeColour">Process conditions at time of test</th><td>' . $rd['process_conditions'] . '</td></tr>
<tr><th class="themeColour">Does the system provide an effective contribution to overall control</th><td>' . $rd['effective'] . '</td></tr>
<tr><th class="themeColour">Particulars of any defect affecting operating performance and repairs required to restore adequate control</th><td>' . $rd['particulars'] . '</td></tr>
<tr><th class="themeColour">Other comments and observations</th><td>' . nl2br($rd['other_comments']) . '</td></tr>
<tr><th class="themeColour">Comments on operators usage</th><td>' . $rd['usage_comments'] . '</td></tr>
</table>

';

        $pdf .= '
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="50%">Noise Level</th><td>' . $rd['noise'] . '</td></tr>
<tr><th class="themeColour">Intended operating performance</th><td>' . $rd['intended_performance'] . '</td></tr>
<tr><th class="themeColour">Is the system achieving intended operational performance?</th><td>' . $rd['achieving_perfomance'] . '</td></tr>
<tr><th class="themeColour">Details of repairs carried out by client as a result of this report. <small>(to be completed by the client and a copy sent to ' . getCompanyDetails('name') . ' at next thorough examination)</small></th><td>' . $rd['repair_details'] . '</td></tr>
</table>
';
        $pdf .= '
<pagebreak>

<div align="center"><h4 class="text-center subtitle"  align="center">LEV EXAMINATION - INITIAL TEST RESULTS</h4></div>
';

        $rd = mysqli_query($GLOBALS['con'], "SELECT * FROM `lev_results_initial` WHERE `itemnumber`='" . $cx['itemnumber'] . "' ORDER BY point_number ASC") or die(mysqli_error($GLOBALS['con']));
        $pdf .= '
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour">Point Number</th><th class="themeColour">Point Detail</th><th class="themeColour">Hood/Duct Size (m2)</th><th class="themeColour">Face/Duct Velocity (m/s)</th><th class="themeColour">Static Pressure</th><th class="themeColour">Volume Flow (m3/s)</th></tr>
';
        while ($rdi = mysqli_fetch_array($rd)) {
            $pdf .= '	<tr><td>' . $rdi['point_number'] . '</td><td>' . $rdi['point_detail'] . '</td><td>' . $rdi['hoodDuct_size'] . '</td><td>' . $rdi['faceDuct_velocity'] . '</td><td>' . $rdi['static_pressure'] . '</td><td>' . $rdi['volume_flow'] . '</td></tr>';
        }
        $pdf .= '</table>

<div align="center"><h4 class="text-center subtitle"  align="center">LEV EXAMINATION - TEST RESULTS FOR THIS EXAMINATION</h4></div>
';

        $rd = mysqli_query($GLOBALS['con'], "SELECT * FROM `lev_results` WHERE `report_number`='" . $cx['repnumber'] . "' ORDER BY point_number ASC") or die(mysqli_error($GLOBALS['con']));
        $pdf .= '
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour">Point Number</th><th class="themeColour">Point Detail</th><th class="themeColour">Hood/Duct Size (m2)</th><th class="themeColour">Face/Duct Velocity (m/s)</th><th class="themeColour">Static Pressure</th><th class="themeColour">Volume Flow (m3/s)</th></tr>
';
        while ($rdi = mysqli_fetch_array($rd)) {
            $pdf .= '	<tr><td>' . $rdi['point_number'] . '</td><td>' . $rdi['point_detail'] . '</td><td>' . $rdi['hoodDuct_size'] . '</td><td>' . $rdi['faceDuct_velocity'] . '</td><td>' . $rdi['static_pressure'] . '</td><td>' . $rdi['volume_flow'] . '</td></tr>';
        }
        $pdf .= '</table>';
        $pdf .= '<pagebreak>

<div align="center"><h4 class="text-center subtitle" align="center">GENERAL ARRANGEMENT OF LEV PLANT (NOT TO SCALE)<br><small>Including position and point numbers for measurement and tests</small></h4></div>';
        $pdf .= '<div style="display:block; border-top: 1px solid #5c84b0; margin-top:20px;  margin-bottom:20px"></div>';


        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	

    case "701": //LEV INTIIAL


        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; margin-bottom:10px; width:100%"></div>

<table class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Manufacturer</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; width:100%"></div>
';


        $it = mysqli_query($GLOBALS['con'], "SELECT * FROM `itemdetails` WHERE `itemnumber`='" . $cx['itemnumber'] . "' ") or die(mysqli_error($GLOBALS['con']));
        $i = mysqli_fetch_array($it);
        $fd = explode("|", $i['fan_details']);
        $pdf = '
<table class="table table-sm" width="100%">
<tr><td>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour">Process Requiring LEV</th><td>' . $i['process'] . '</td></tr>
<tr><th class="themeColour">Work Exposure Limits of Hazardous Substance Elements</th><td>' . $i['limits'] . '</td></tr>
<tr><th class="themeColour">Extraction Points in use Simultaniously</th><td>' . $i['number_in_use'] . '</td></tr>
</table>
</td>

<td>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour">Hazardous Substance Requiring Control</th><td>' . $i['substance'] . '</td></tr>
<tr><th class="themeColour">Number of extraction Points</th><td>' . $i['number_of_points'] . '</td></tr>
<tr><th class="themeColour">Exhaust Air Returned To</th><td>' . $i['returned_to'] . '</td></tr>
</table>
</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:20px;  margin-bottom:10px; width:100%"></div>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr>
<th class="themeColour">Fan Details:</th><td>' . $fd[0] . '</td>
<th class="themeColour">Fan Volume Flow:</th><td>' . $fd[1] . '</td>
<th class="themeColour">Fan Direction of Rotation:</th><td>' . $fd[2] . '</td>
</tr>
<tr>
<th class="themeColour">Motor Details:</th><td>' . $i['motor_details'] . '</td>
<th class="themeColour">Pri. Filter:</th><td>' . $i['pri_filter'] . '</td>
<th class="themeColour">Sec. Filter:</th><td>' . $i['sec_filter'] . '</td>
</tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px;  margin-bottom:20px; width:100%"></div>

<h4 class="subtitle">Examination Results</h4>
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="50%">Test procedure employed</th><td>' . $rd['test_procedure'] . '</td></tr>
<tr><th class="themeColour">Process conditions at time of test</th><td>' . $rd['process_conditions'] . '</td></tr>
<tr><th class="themeColour">Does the system provide an effective contribution to overall control</th><td>' . $rd['effective'] . '</td></tr>
<tr><th class="themeColour">Particulars of any defect affecting operating performance and repairs required to restore adequate control</th><td>' . $rd['particulars'] . '</td></tr>
<tr><th class="themeColour">Other comments and observations</th><td>' . nl2br($rd['other_comments']) . '</td></tr>
<tr><th class="themeColour">Comments on operators usage</th><td>' . $rd['usage_comments'] . '</td></tr>
<tr><th class="themeColour">Noise Level</th><td>' . $rd['noise'] . '</td></tr>
<tr><th class="themeColour">Intended operating performance</th><td>' . $rd['intended_performance'] . '</td></tr>
<tr><th class="themeColour">Is the system achieving intended operational performance?</th><td>' . $rd['achieving_perfomance'] . '</td></tr>
<tr><th class="themeColour">Details of repairs carried out by client as a result of this report. <small>(to be completed by the client and a copy sent to ' . getCompanyDetails('name') . ' at next thorough examination)</small></th><td>' . $rd['repair_details'] . '</td></tr>
</table>

';

        $pdf .= "<pagebreak>";

        $pdf .= '

<div style="display:block; margin-top:10px;  margin-bottom:10px"></div>
<div align="center"><h4 class="text-center subtitle"  align="center">LEV EXAMINATION INTIAL TEST RESULTS</h4></div>
';

        $rd = mysqli_query($GLOBALS['con'], "SELECT * FROM `lev_results_initial` WHERE `report_number`='" . $cx['repnumber'] . "'  ORDER BY point_number ASC") or die(mysqli_error($GLOBALS['con']));
        $pdf .= '<table class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Point Number</th><th class="themeColour">Point Detail</th><th class="themeColour">Hood/Duct Size (m2)</th><th class="themeColour">Face/Duct Velocity (m/s)</th><th class="themeColour">Static Pressure</th><th>Volume Flow (m3/s)</th></tr>
';
        while ($rdi = mysqli_fetch_array($rd)) {
            $pdf .= '	<tr><td>' . $rdi['point_number'] . '</td><td>' . $rdi['point_detail'] . '</td><td>' . $rdi['hoodDuct_size'] . '</td><td>' . $rdi['faceDuct_velocity'] . '</td><td>' . $rdi['static_pressure'] . '</td><td>' . $rdi['volume_flow'] . '</td></tr>';
        }
        $pdf .= '</table>';

        $pdf .= "<pagebreak>";

        $pdf .= '
<div style="display:block; border-top: 2px solid #5c84b0; margin-top:0px;  margin-bottom:20px"></div>
<div align="center"><h4 class="text-center subtitle" align="center">GENERAL ARRANGEMENT OF LEV PLANT (NOT TO SCALE)<br><small>Including position and point numbers for measurement and tests</small></h4></div>';
        $pdf .= '<div style="display:block; border-top: 1px solid #5c84b0; margin-top:20px;  margin-bottom:20px"></div>';


        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////	


    case "800": //POWER PRESS THOROUGH IV

        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; margin-bottom:10px; width:100%"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Manufacturer</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Capacity</th><td>' . $det[10] . '</td><th class="themeColour">Location on site</th><td>' . $det[6] . '</td></tr>
<tr><th class="themeColour" align="left">Examination Frequency</th><td colspan="3">' . $freq . ' M/ths</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; width:100%"></div>
';


        $it = mysqli_query($GLOBALS['con'], "SELECT * FROM `itemdetails` WHERE `itemnumber`='" . $cx['itemnumber'] . "' ") or die(mysqli_error($GLOBALS['con']));
        $i = mysqli_fetch_array($it);
        if ($rd['lastReportSeen'] == "") {
            $lastreport = "Not Known";
        } else {
            $lastreport = $rd['lastReportSeen'];
        }
        if ($cx['nextDate'] == "") {
            $nextDate = "Not Known";
        } else {
            $nextDate = date('d-m-Y', $cx['nextDate']);
        }
        if ($rd['partsInaccessible'] == "") {
            $parts[0] = "Enclosed parts where dismantling is required";
        } else {
            $parts = explode("|", $rd['partsInaccessible']);
        }
        if ($rd['adefect'] == "") {
            $adefect[0] = "None";
            $adefect[1] = "N/A";
            $adefect[2] = "N/A";
        } else {
            $adefect = explode("|", $rd['adefect']);
        }
        if ($rd['bdefect'] == "") {
            $rd['bdefect'] = "None";
        }
        if ($rd['obs'] == "") {
            $rd['obs'] = "None";
        }
        if (empty($parts[1])) {
            $parts[1] = "N/A";
        }
        if (empty($parts[2])) {
            $parts[2] = "N/A";
        }
        if (empty($adefect[1])) {
            $adefect[1] = "N/A";
        }
        if (empty($adefect[2])) {
            $adefect[2] = "N/A";
        }
        if (!empty($parts[1]) &&  $parts[1] == "100010") {
            $parts[1] = "Guaranteed for life subject to manufacturers maintenance program being adhered to.";
        }
        if (!empty($parts[2]) &&  $parts[2] == "100020") {
            $parts[2] = "Guaranteed for life subject to manufacturers maintenance program being adhered to.";
        }
        $pdf = '
<div class="tableDetails">
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="50%">Description of Clutch System</th><td>' . $rd['clutch'] . '</td></tr>
<tr><th class="themeColour">Safety Devices</th><td>' . nl2br($rd['safety']) . '</td></tr>

<tr><th class="themeColour" width="50%">Parts Inaccessible</th><td>' . $parts[0] . '</td></tr>
<tr><th class="themeColour" colspan="2"><strong>Date when inaccessible parts:</strong></th></tr>
<tr><th class="themeSecondary">a) Were last dismantled for examination</th><td>' . $parts[1] . '</td></tr>
<tr><th class="themeSecondary">b) Are next required to be dismantled for examinaton</th><td>' . $parts[2] . '</td></tr>

<tr><th class="themeColour" width="50%">Identification of any part found to have a defect which is or could become a danger to persons (category "A" defect)<br><small class="themeSecondary">State description  of the defect and any repair, renewal or alteration required to remedy the defect, either immediately or within a specified time.</small></th><td>' . nl2br($adefect[0]) . '</td></tr>
<tr><th class="themeColour">Date of notification of Category "A" defects to occupier</th><td>' . $adefect[1] . '</td></tr>
<tr><th class="themeColour">Category "A" defects rectified at time of examination</th><td>' . $adefect[2] . '</td></tr>

<tr><th class="themeColour" width="50%">Other defects requiring attention</th><td>' . nl2br($rd['bdefect']) . '</td></tr>

<tr><th class="themeColour" width="50%">Observations and recommendations</th><td>' . nl2br($rd['obs']) . '</td></tr>

<tr><th class="themeColour" width="50%">Date of last Thorough Examination <small>(if known)</small></th><td>' . $lastreport . '</td></tr>

<tr><th class="themeColour" width="50%">Latest date by which the next thorough examination must be carried out</th><td>' . $nextDate . '</td></tr>
</table>
</div>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    case "801": //POWER PRESS THOROUGH IV
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; margin-bottom:10px; width:100%"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Manufacturer</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Capacity</th><td>' . $det[10] . '</td><th class="themeColour">Location on site</th><td>' . $det[6] . '</td></tr>
<tr><th class="themeColour" align="left">Examination Frequency</th><td colspan="3">' . $freq . ' M/ths</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; width:100%"></div>
';


        $it = mysqli_query($GLOBALS['con'], "SELECT * FROM `itemdetails` WHERE `itemnumber`='" . $cx['itemnumber'] . "' ") or die(mysqli_error($GLOBALS['con']));
        $i = mysqli_fetch_array($it);
        if ($rd['lastReportSeen'] == "") {
            $lastreport = "Not Known";
        } else {
            $lastreport = $rd['lastReportSeen'];
        }
        if ($cx['nextDate'] == "") {
            $nextDate = "Not Known";
        } else {
            $nextDate = date('d-m-Y', $cx['nextDate']);
        }
        if ($rd['partsInaccessible'] == "") {
            $parts[0] = "Enclosed parts where dismantling is required";
        } else {
            $parts = explode("|", $rd['partsInaccessible']);
        }
        if ($rd['adefect'] == "") {
            $adefect[0] = "None";
            $adefect[1] = "N/A";
            $adefect[2] = "N/A";
        } else {
            $adefect = explode("|", $rd['adefect']);
        }
        if ($rd['bdefect'] == "") {
            $rd['bdefect'] = "None";
        }
        if ($rd['obs'] == "") {
            $rd['obs'] = "None";
        }
        if (empty($parts[1])) {
            $parts[1] = "N/A";
        }
        if (empty($parts[2])) {
            $parts[2] = "N/A";
        }
        if (empty($adefect[1])) {
            $adefect[1] = "N/A";
        }
        if (empty($adefect[2])) {
            $adefect[2] = "N/A";
        }

        if (!empty($parts[1]) &&  $parts[1] == "100010") {
            $parts[1] = "Guaranteed for life subject to manufacturers maintenance program being adhered to.";
        }
        if (!empty($parts[2]) &&  $parts[2] == "100020") {
            $parts[2] = "Guaranteed for life subject to manufacturers maintenance program being adhered to.";
        }

        $pdf = '
<div class="tableDetails">
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="50%">Description of Clutch System</th><td>' . $rd['clutch'] . '</td></tr>
<tr><th class="themeColour">Safety Devices</th><td>' . nl2br($rd['safety']) . '</td></tr>
<tr><th class="themeColour">Parts Inaccessible</th><td>' . $parts[0] . '</td></tr>
<tr><th class="themeColour" colspan="2"><strong>Date when inaccessible parts:</strong></th></tr>
<tr><th class="themeSecondary">a) Were last dismantled for examination</th><td>' . $parts[1] . '</td></tr>
<tr><th class="themeSecondary">b) Are next required to be dismantled for examinaton</th><td>' . $parts[2] . '</td></tr>

<tr><th class="themeColour" width="50%">Identification of any part found to have a defect which is or could become a danger to persons (category "A" defect)<br><small class="themeSecondary">State description  of the defect and any repair, renewal or alteration required to remedy the defect, either immediately or within a specified time.</small></th><td>' . nl2br($adefect[0]) . '</td></tr>
<tr><th class="themeColour">Date of notification of Category "A" defects to occupier</th><td>' . $adefect[1] . '</td></tr>
<tr><th class="themeColour">Category "A" defects rectified at time of examination</th><td>' . $adefect[2] . '</td></tr>

<tr><th class="themeColour" width="50%">Other defects requiring attention</th><td>' . nl2br($rd['bdefect']) . '</td></tr>

<tr><th class="themeColour" width="50%">Observations and recommendations</th><td>' . nl2br($rd['obs']) . '</td></tr>

<tr><th class="themeColour" width="50%">Date of last Thorough Examination <small>(if known)</small></th><td>' . $lastreport . '</td></tr>

<tr><th class="themeColour" width="50%">Latest date by which the next thorough examination must be carried out</th><td>' . $nextDate . '</td></tr>
</table>
</div>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    case "802": //INJECTION

        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; margin-bottom:10px; width:100%"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Manufacturer</th><td>' . $det[3] . '</td></tr>
<tr><th class="themeColour">Model</th><td>' . $det[5] . '</td>';
        if (empty($det[4])) {
            $inst .= '<th class="themeColour">Year</th><td>Not Marked</td>';
        } else {
            $inst .= '<th class="themeColour">Year</th><td>' . $det[4] . '</td>';
        }
        $inst .= '</tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Capacity</th><td>' . $det[10] . '</td><th class="themeColour">Location on site</th><td>' . $det[6] . '</td></tr>
<tr><th class="themeColour" align="left">Examination Frequency</th><td colspan="3">' . $freq . ' M/ths</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; width:100%"></div>
';

        if ($rd['lastReportSeen'] == "") {
            $lastreport = "Not Known";
        } else {
            $lastreport = $rd['lastReportSeen'];
        }
        if ($rd['partsInaccessible'] == "") {
            $rd['partsInaccessible'] = "Enclosed parts where dismantling is required";
        }
        if ($rd['safety'] == "") {
            foreach (range(0, 12) as $i) {
                $safety[$i] = "yes";
            }
        } else {
            $safety = explode("|", $rd['safety']);
        }
        if ($rd['repair_details'] == "") {
            $repair[0] = "None";
            $repair[1] = "None";
        } else {
            $repair = explode("|", $rd['repair_details']);
        }
        if ($rd['bdefect'] == "") {
            $rd['bdefect'] = "None";
        }
        if ($rd['obs'] == "") {
            $rd['obs'] = "None";
        }

        $pdf = '
<div class="tableDetails">
<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" colspan="4">Are the following parts of the guard(s) and safety device(s) in good working order? If not, state what defects have been found and which require remedy</th></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="subtitle" colspan="4">Guards</th></tr>
<tr><th class="themeColour" width="20%">A) Front</th><td>' . $safety[0] . '</td><th class="themeColour" width="20%">B) Rear</th><td>' . $safety[1] . '</td></tr>
<tr><th class="themeColour">C) Tunnel</th><td>' . $safety[2] . '</td><th class="themeColour">D) Toggle</th><td>' . $safety[3] . '</td></tr>
<tr><th class="themeColour">E) Well Discharge</th><td>' . $safety[4] . '</td><th class="themeColour">F) Nozzle / Purge</th><td>' . $safety[5] . '</td></tr>
<tr><th class="themeColour">G) Hopper Throat</th><td>' . $safety[6] . '</td><th class="themeColour">H) Top</th><td>' . $safety[7] . '</td></tr>
<tr><th class="themeColour">I) Barrel</th><td>' . $safety[8] . '</td><th class="themeColour"></th><td></td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="subtitle" colspan="4">Safety Devices</th></tr>
<tr><th class="themeColour" width="20%">J) Emergency Stops</th><td>' . $safety[9] . '</td><th class="themeColour" width="20%">K) Interlocks</th><td>' . $safety[10] . '</td></tr>
<tr><th class="themeColour">L) Mechanical Scotch</th><td>' . $safety[11] . '</td><th class="themeColour">M) Safety Mat(s)</th><td>' . $safety[12] . '</td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="50%">Parts Inaccessible</th><td>' . $rd['partsInaccessible'] . '</td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="subtitle" colspan="2">Repairs, renewals or alterations to the injection moulder\'s guards and protection devices that are or may become a cause of danger to employed persons either a) Immediately or b) Within a specified time</th></tr>
<tr><th class="themeColour" width="5%">a)</th><td>' . $repair[0] . '</td></tr>
<tr><th class="themeColour">b)</th><td>' . $repair[1] . '</td></tr>
</table>

<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="50%">Recommendations</th><td>' . $rd['bdefect'] . '</td></tr>
<tr><th class="themeColour">Observations</th><td>' . $rd['obs'] . '</td></tr>
<tr><th class="themeColour">Date of last Thorough Examination <small>(if known)</small></th><td>' . $lastreport . '</td></tr>
<tr><th class="themeColour">Latest date by which the next Thorough Examination must be carried out</th><td>' . date('d-m-Y', $cx['nextDate']) . '</td></tr>
</table>

</div>
';

        $footerTemplate = "footer.php";
        break;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    case "900": //PAT
        $inst = '
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; margin-bottom:10px; width:100%"></div>

<table  class="table table-sm tableDetails" width="100%">
<tr><th class="themeColour">Item description</th><td>' . $det[0] . '</td><th class="themeColour">Class</th><td>' . $rd['class'] . '</td></tr>
<tr><th class="themeColour">Plant Number</th><td>' . $det[1] . '</td><th class="themeColour">Serial Number</th><td>' . $det[2] . '</td></tr>
<tr><th class="themeColour">Location on site</th><td>' . $det[6] . '</td><th class="themeColour" align="left">Examination Frequency</th><td>' . $freq . ' M/ths</td></tr>
</table>
<div style="display:block; border-top: 1px solid #5c84b0; margin-top:10px; width:100%"></div>
';

        $pdf = '
<div class="tableDetails">

<table class="table table-sm tableDetails" width="100%" cellspacing="10">
<tr><th class="themeColour" width="30%">Class:</th><td>' . $rd['class'] . '</td><th class="themeColour" width="30%">Voltage:</th><td>' . $rd['volts'] . '</td></tr>
<tr><th class="themeColour">Earth Bond: (ohms)</th><td>' . $rd['ebond'] . '</td><th class="themeColour">Insulation Resistance: (M ohms)</th><td>' . $rd['ires'] . '</td></tr>
<tr><th class="themeColour">Result of examination:</th><td colspan="3">' . $cx['result'] . '</td></tr>
<tr><th colspan="4"></th></tr>
<tr><th class="subtitle" colspan="4">Defects, Recommendations and Observations</th></tr>
<tr><td colspan="2">' . $rd['obs'] . '</td></tr>
</table>
</div>
';

        $footerTemplate = "footer.php";
        break;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////	
if ($_REQUEST['imageMode'] == "1") {
    $imagePaths = [];
    for ($i = 1; $i <= 6; $i++) {
        $imgPath = '../../../' . TENANT_URL . '/images/report_images/' . $cx['itemnumber'] . '_' . $i . '.jpg';
        if (file_exists($imgPath)) {
            $imagePaths[] = $imgPath;
        }
    }

    $imgCount = count($imagePaths);

    if ($imgCount === 1) {
        // Only one image: single page as before
        $pdf .= '<div style="page-break-before: always"></div>
            <table class="table table-sm tableDetails" width="100%" cellspacing="5">
                <tr><td align="center"><img src="' . $imagePaths[0] . '" style="max-width:90%; max-height:400px;"></td></tr>
            </table>';
    } elseif ($imgCount > 1) {
        // Multiple images: grid, 2 per row
        $pdf .= '<div style="page-break-before: always"></div>
            <table class="table table-sm tableDetails" width="100%" cellspacing="10">';
        for ($i = 0; $i < $imgCount; $i += 2) {
            $pdf .= '<tr>';
            // First image in row
            $pdf .= '<td align="center" style="width:50%"><img src="' . $imagePaths[$i] . '" style="max-width:90%; max-height:200px;"></td>';
            // Second image in row, or empty cell if none
            if (isset($imagePaths[$i + 1])) {
                $pdf .= '<td align="center" style="width:50%"><img src="' . $imagePaths[$i + 1] . '" style="max-width:90%; max-height:200px;"></td>';
            } else {
                $pdf .= '<td style="width:50%"></td>';
            }
            $pdf .= '</tr>';
        }
        $pdf .= '</table>';
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////	


if ($cx['olddate'] == "" || $cx['olddate'] == "Not Known") {
    $olddate = "Not Known";
} else {
    $olddate = date('d-m-Y', $cx['olddate']);
}
$pdfx .= '
<div class="tableDetails">
<table class="table table-sm" width="100%">
<tr><th width="50%" align="center">Last Inspection</th><th width="50%" align="center">Next Inspection</th></tr>
<tr><td width="50%" align="center">' . $olddate . '</td><td width="50%" align="center">' . date('d-m-Y', $cx['nextDate']) . '</td></tr>
</table>
</div>
';
if ($_REQUEST['mode'] == "preview") {
    $filestore = '../../..' . TENANT_URL . '/output/reports/' . $_REQUEST['clientnumber'] . '/preview.pdf';
} else {
    if ($_REQUEST['mode'] == "hse") {
        $filestore = '../../..' . TENANT_URL . '/output/reports/' . $_REQUEST['clientnumber'] . '/' . $_REQUEST['repnumber'] . '_' . $_REQUEST['items'] . '_HSE.pdf';
    } else {
        $filestore = '../../..' . TENANT_URL . '/output/reports/' . $_REQUEST['clientnumber'] . '/' . $_REQUEST['repnumber'] . '_' . $_REQUEST['items'] . '.pdf';
    }
}




include('../../includes/engines/ext.pdf.php');
echo $_REQUEST['repnumber'] . "-0-0" . $cx['itemnumber'];