<div id="loadingObject" class="d-none" 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>
<style>
.location-client-search {
    width: 100%;
    max-width: 420px;
    margin-right: auto;
    margin-left: auto;
    text-align: left;
    display: flex;
    justify-content: center;
}

.location-client-search .select-wrapper,
.location-client-search .select-wrapper.form-outline,
.location-client-search > .form-outline {
    display: block;
    width: 100% !important;
    max-width: 420px;
    margin-right: auto !important;
    margin-left: auto !important;
}

.location-client-search .select-input {
    width: 100% !important;
}
</style>
<?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();
$record = '';
$output = '';
$tbleName = "assetTable" . rand();
$mode = $_REQUEST['mode'] ?? '';
?>

<?php
switch ($mode) {
    default:
?>
        <div class="row justify-content-center pt-3 pt-md-5">
            <div class="col-12 col-xl-10">
                <div class="card border-0 shadow-2">
                    <div class="card-body px-4 py-5 px-md-5 text-center">
                        <i class="fad fa-map-marked-alt fa-4x mb-4"
                            style="--fa-primary-color: #0055A6; --fa-secondary-color: #6c757d;"></i>
                        <h2 class="mb-2">Location overview</h2>
                        <p class="lead text-muted mb-4">Choose a client to see every active location and its next inspection
                            status in one place.</p>

                <div class="row justify-content-center mb-5">
                    <div class="col-12 col-md-7 col-lg-6 text-center">
                        <label class="form-label fw-bold" for="loadClient">Select a client to begin</label>
                        <div class="location-client-search">
                            <select class="form-control" id="loadClient" data-mdb-placeholder="Search for a client"
                                onchange="loadClient()">
                                <option value=""></option>
                                <?php while ($pd = mysqli_fetch_array($cc)) { ?>
                                <option value="<?= $pd['clientnumber'] ?>"><?= $pd['clientname'] ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>

                        <div class="row g-4 text-start">
                            <div class="col-12 col-md-4">
                                <div class="h-100 p-4 bg-light rounded-3">
                                    <i class="fad fa-map-marker-alt fa-2x text-primary mb-3"></i>
                                    <h5>See every location</h5>
                                    <p class="text-muted mb-0">Review the client's active sites and postcodes in a single
                                        list.</p>
                                </div>
                            </div>
                            <div class="col-12 col-md-4">
                                <div class="h-100 p-4 bg-light rounded-3">
                                    <i class="fad fa-calendar-check fa-2x text-primary mb-3"></i>
                                    <h5>Check inspection dates</h5>
                                    <p class="text-muted mb-0">Quickly identify the next inspection date recorded at each
                                        location.</p>
                                </div>
                            </div>
                            <div class="col-12 col-md-4">
                                <div class="h-100 p-4 bg-light rounded-3">
                                    <i class="fad fa-traffic-light fa-2x text-primary mb-3"></i>
                                    <h5>Spot priorities</h5>
                                    <p class="text-muted mb-0">Clear status labels show which locations are clear, due, or
                                        past due.</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?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();
        $data = [];
        while ($list = mysqli_fetch_array($iv)) {
            $data[] = $list;
        }
        mysqli_free_result($iv);

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

        <div class="row align-items-end mb-4">
            <div class="col-12 col-md-5 col-lg-4">
                <label class="form-label fw-bold" for="loadClient">Change client</label>
                <select class="form-control form-control-sm" id="loadClient" data-mdb-placeholder="Search for a client"
                    onchange="loadClient()">
                    <option value=""></option>
                    <?php
                    mysqli_data_seek($cc, 0);
                    while ($pd = mysqli_fetch_array($cc)) { ?>
                        <option value="<?= $pd['clientnumber'] ?>"
                            <?= $pd['clientnumber'] == $sd['clientnumber'] ? 'selected' : '' ?>>
                            <?= $pd['clientname'] ?>
                        </option>
                    <?php } ?>
                </select>
            </div>
        </div>

        <div class="d-flex align-items-center mb-3">
            <i class="fad fa-map-marked-alt fa-2x text-primary me-3"></i>
            <div>
                <h3 class="mb-0"><?= $sd['clientname'] ?></h3>
                <small class="text-muted">Location inspection overview</small>
            </div>
        </div>
        <table id="<?= $tbleName ?>" 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;
}
if ($mode === 'client') {
    $table = $tbleName;
    $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()
        if (!c) {
            return;
        }
        location.href = "index.php?t=includes/master&p=location_report.php&mode=client&clientnumber=" +
            encodeURIComponent(c)
    }

    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>
