<div id="loadingObject" style="position: fixed; top:50%; width:100%">
    <h1 class="text-center"><i class="fad fa-flux-capacitor fa-spin fa-3x fa-fw"
            style="--fa-primary-color: #0055A6; --fa-secondary-color: gray;"></i><br>Loading Data <br><small>Please
            wait</small></h1>
</div>


<script>
$('#navBox').html('<li class="breadcrumb-item active">Locations: Inspection Status</li>')
</script>

<div id="exportTitle" style="display: none">Locations Inspection Status - <?= date('d-m-Y') ?></div>

<?php
function checkInsp2($c, $n)
{
    $now = strtotime('now');
    $count = 0;
    $then = strtotime('now +1 month');

    $stmt = $GLOBALS['con']->prepare("SELECT
a.clientnumber,
a.itemnumber,
a.locnumber,
a.duedate,
a.statuslive,
b.locnumber,
b.locname,
b.postcode
FROM itemdetails a JOIN locations b ON a.locnumber=b.locnumber
 WHERE `duedate`<'" . $then . "' AND a.clientnumber='" . $c . "' AND  a.statuslive='1'  LIMIT 1") or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();
    $ic = $stmt->get_result();
    $stmt->close();
    $c = mysqli_num_rows($ic);
    if ($c < 1) {
    } else {
        $id = mysqli_fetch_array($ic);
        if ($id['duedate'] < $now) {
            $status = '<td class="text-danger text-center text-strong">PAST</td>';
        } elseif ($id['duedate'] > $now || $id['duedate'] < $then) {
            $status = '<td class="text-warning text-center text-strong">DUE</td>';
        }
        $data = '<tr>
<td>' . $n . '</td>
<td>' . $id['locname'] . '</td>
<td>' . $id['postcode'] . '</td>
<td><span class="hidden noprint noExport" style="display:none">' . date('Y-m-d', $id['duedate']) . '</span>' . date('d-m-Y', $id['duedate']) . '</td>
' . $status . '
</tr>';
        return $data;
    }
}

$i = 0;
$start = microtime(true);
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname` FROM `clientdetails` WHERE `statuslive`='1'") or die(mysqli_error("GET_INV_NO: " . $GLOBALS['con']));
$stmt->execute();
$iv = $stmt->get_result();
$stmt->close();
while ($list = mysqli_fetch_assoc($iv)) {
    $data[] = $list;
}
mysqli_free_result($iv);

foreach ($data as $l) {
    $cnt++;
    $output .= checkInsp2($l['clientnumber'], $l['clientname']);
}

?>
<table id="listTable" class="table table-sm table-condensed">
    <thead>
        <tr>
            <th>Client</th>
            <th>Location</th>
            <th>Post Code</th>
            <th class="date-uk">Date Due</th>
            <th>Status</tthd>
        </tr>
    </thead>
    <tbody>
        <?= $output; ?>
    </tbody>
</table>

<?php $table = "listTable"; ?>
<?php require_once('includes/single_table.engine'); ?>