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


<div id="exportTitle" style="display: none">Locations Inspection Status - <?= date('d-m-Y') ?></div>
<?php
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname` FROM `clientdetails` WHERE `statuslive`='1'  ORDER BY `clientname` ASC") or die(mysqli_error($GLOBALS['con']));
$stmt->execute();
$cc = $stmt->get_result();
$stmt->close(); ?>

<div class="row mt-0 mb-4">
    <div class="col-3 ml-auto">
        <select class="form-control form-control-sm" id="loadClient" data-mdb-placeholder="Load Client"
            onchange="loadClient()">
            <option value=""></option>
            <?php while ($pd = mysqli_fetch_array($cc)) { ?>
            <option value="<?= $pd['clientnumber'] ?>"><?= $pd['clientname'] ?></option>
            <?php } ?>
        </select>
    </div>
</div>
<?php
switch ($_REQUEST['mode']) {
    default:
        $record = file_get_contents('../' . TENANT_URL . '/framework/dashboard_functions/location_report.txt');
        $record = explode("|", $record);
?>

<table id="assetTable" class="table table-sm table-sm" style="display:none">
    <thead class="table-dark">
        <tr>
            <th>Client</th>
            <th>Location</th>
            <th>Post Code</th>
            <th class="date-uk">Date Due</th>
            <th>Status</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?= $record[1]; ?>
    </tbody>
</table>
<?php
        break;

    case "client":


        function checkInsp6($cx, $cn, $l, $ln, $p)
        {
            $now = strtotime('now');
            $count = 0;
            $then = strtotime('now +1 month');

            $stmt = $GLOBALS['con']->prepare("SELECT `itemnumber`,`duedate` FROM `itemdetails`  WHERE clientnumber='" . $cx . "' AND  statuslive='1' AND `locnumber`='" . $l . "'  ORDER BY abs(duedate) ASC   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'] > $then) {
                    $status = '<td class="text-success text-center text-strong">CLEAR</td>';
                }
                if ($id['duedate'] < $now) {
                    $status = '<td class="text-danger text-center text-strong">PAST</td>';
                }
                if ($id['duedate'] > $now && $id['duedate'] < $then) {
                    $status = '<td class="text-warning text-center text-strong">DUE</td>';
                }


                $data = '<tr>
<td>' . $ln . '</td>
<td>' . $p . '</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 . '
<td><a href="index.php?t=includes/master&amp;p=client.php&amp;clientnumber=' . $cx . '&amp;view=asset" class="btn btn-secondary noprint m-0 px-3 py-2 waves-effect" data-toggle="tooltip" data-placement="top" data-original-title="View Schedule"><i class="fad fa-th-list"></i></a></td>
</tr>';
                return $data;
            }
        }

        $sc = mysqli_query($GLOBALS['con'], "SELECT `clientnumber`,`clientname` FROM `clientdetails` WHERE `clientnumber` = '" . $_REQUEST['clientnumber'] . "' AND `statuslive`='1'") or die(mysqli_error($GLOBALS['con']));
        $sd = mysqli_fetch_array($sc);

        $stmtx = $GLOBALS['con']->prepare("SELECT `locnumber`,`locname`,`postcode` FROM `locations`  WHERE `clientnumber`='" . $sd['clientnumber'] . "' AND `statuslive`='1' ") or die(mysqli_error($GLOBALS['con']));
        $stmtx->execute();
        $iv = $stmtx->get_result();
        $stmtx->close();
        while ($list = mysqli_fetch_array($iv)) {
            $data[] = $list;
        }
        mysqli_free_result($iv);

        foreach ($data as $l) {
            $cnt++;
            $output .= checkInsp6($sd['clientnumber'], $sd['clientname'], $l['locnumber'], $l['locname'], $l['postcode']);
        }
    ?>


<h3>Viewing results for <?= $sd['clientname'] ?></h3>
<table id="assetTable" class="table table-sm table-sm" style="display:none">
    <thead class="table-dark">
        <tr>
            <th>Location</th>
            <th>Post Code</th>
            <th class="date-uk">Date Due</th>
            <th>Status</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?= $output; ?>
    </tbody>
</table>

<?php
        break;
}
$table = "assetTable";
$dateTarget = "2";
$length = "25";
$sortTarget = "";
$sortCell = "";
$logo = "yes";
$scroller = "";
require(ROOT_URL . "/includes/tables/asset_table.php");
?>

<script>
<?php if (!empty($_REQUEST['search'])) { ?>
setTimeout(function() {
    $('.dataTables_filter input').val('<?= $_REQUEST['search'] ?>')
    $('.dataTables_filter input').keyup();
}, 500);
<?php } ?>
$(document).ready(function() {
    $('#loadingObject').css('display:none');
    $('#listTable').css('display:block');
})



function loadClient() {
    var c = $('#loadClient').val()
    var s = $('#loadClient option:selected').text();
    location.href = "index.php?t=includes/master&p=location_report.php&mode=client&clientnumber=" + c + "&clientname=" +
        s
}

document.addEventListener('DOMContentLoaded', function() {
    var mySelect = new mdb.Select(document.getElementById('loadClient'), {
        filter: true,
        container: "body"
    });
});
document.getElementById('navBox').innerHTML =
    '<li class="breadcrumb-item">Tools <i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i></li><li class="breadcrumb-item active">Inspection Status</li>';
</script>