<?php
if (file_exists('../../../' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('../../../' . $_REQUEST['tenant'] . '/config.php');
}
if (file_exists('/config.php')) {
    require_once('/config.php');
}

function getSurveyorsName($esname)
{
    $stmt = $GLOBALS['con']->prepare("SELECT `surname`,`forename` FROM `surveyor` WHERE `id`=?") or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i', $esname);
    $stmt->execute();
    $rc = $stmt->get_result();
    $stmt->close();
    $cd = mysqli_fetch_array($rc);
    return $cd['forename'] . " " . $cd['surname'];
}
?>

<style>
.dt-buttons {
    display: none
}
</style>

<div id="loadingObject">
    <h1 class="text-center"><i class="fad fa-file-archive fa-spin fa-3x fa-fw"
            style="--fa-primary-color: #0055A6; --fa-secondary-color: gray;"></i><br>Loading Assets <br><small>Please
            wait</small></h1>
</div>


<table id="assetTable" class="table table-striped table-sm table-bordered table-hover responsive table-condensed"
    width="100%" style="display: none">
    <thead>
        <tr>
            <th class="no-sort">Select</th>
            <th>Description</th>
            <th>Plant Number</th>
            <th>Serial Number</th>
            <th>Location</th>
            <th>Due Date</th>

        </tr>
    </thead>
    <tbody>
        <?php
        if ($_REQUEST['locnumber'] != "ALL") {
            $filter = "AND a.locnumber='" . $_REQUEST['locnumber'] . "'";
        }
        $stmt = $GLOBALS['con']->prepare("SELECT 
           a.itemnumber, a.locnumber, a.subloc, a.desc, a.code, a.currdate, a.plantnumber, a.serialnumber, a.model, a.dateofmanf, a.manf, a.swl, a.duedate, a.linked, a.itemnotes, a.statuslive, 
           b.locnumber, b.locname, b.locnotes, b.postcode
           FROM itemdetails a 
           INNER JOIN locations b ON (b.locnumber=a.locnumber)
          
           WHERE a.clientnumber='" . $_REQUEST['clientnumber'] . "' AND a.statuslive='1' $filter") or die(mysqli_error($GLOBALS['con']));
        $stmt->execute();
        $ic = $stmt->get_result();
        $stmt->close();



        while ($id = mysqli_fetch_array($ic)) {

            if ($id['duedate'] < strtotime('now')) {
                $stat = "status_colour color danger-color-dark";
                $statTitle = "Overdue";
            }
            if ($id['duedate'] > strtotime('now') && $id['duedate'] < strtotime('now +1 month')) {
                $stat = "status_colour color warning-color-dark";
                $statTitle = "Pending";
            }
            if ($id['duedate'] > strtotime('now +1 month')) {
                $stat = "status_colour color success-color-dark";
                $statTitle = "Not_Due";
                $statFTitle = "Not Due";
            }
        ?>
        <tr id="<?= $id['itemnumber'] ?>">
            <td id="C<?= $id['itemnumber'] ?>" data-value="C<?= $id['itemnumber'] ?>"></td>
            <td><?= $id['desc'] ?></td>
            <td><?= $id['plantnumber'] ?></td>
            <td><?= $id['serialnumber'] ?></td>
            <td><?= getItemLocation($id['itemnumber'], 'name') ?></td>
            <td class="prod_color"><?php if (empty($id['duedate'])) {
                                            echo "No date stored";
                                        } else { ?>
                <span class="hidden"><?= date('Y-m-d', $id['duedate']); ?></span><?= date('d-m-Y', $id['duedate']);
                                                                                    } ?>
            </td>

        </tr>

        <?php } ?>
    </tbody>
</table>
<div id="selectedItems" style="display:block"></div>
<div class="row mt-5 mb-5">
    <div class="col-12 text-center"><a href="javascript:void(0)" class="btn btn-info" onclick="assignSite()">Assign
            Site</a>
        <a id="assignAsset" href="javascript:void(0)" class="btn btn-info" onclick="assignItems()"
            style="display:none">Assign Selected
            assets</a>
    </div>
</div>
<?php
$table = "assetTable";
$sortTarget = "0";
$sortCell = "first-child";
$dateTarget = "5";
$logo = "yes";
require("../../includes/tables/asset_table.php");
?>