<?php
if($_REQUEST['filter']=="archive"){$status=0;}
else{$status=1;}
$stmt = $GLOBALS['con']->prepare("SELECT 
a.itemnumber,
a.locnumber,
a.desc,
a.plantnumber,
a.serialnumber,
a.swl,
a.duedate,
a.linked,
a.itemnotes,
a.statuslive,
b.locname,
b.locnotes,
b.postcode
FROM itemdetails a JOIN locations b ON a.locnumber=b.locnumber
WHERE a.clientnumber='".$_REQUEST['clientnumber']."' AND a.statuslive='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$ic=$stmt->get_result();$stmt->close();
?>
<div class="row">
    <div class="col-12">
        <div id="loadingObject">
            <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><span
                      class="sr-only">Loading...</span></h1>
        </div>
        <div id="exportTitle" style="display:none; background-color:#353b50; color:#ffffff">
            <p style="font-size:10px"><?= getClientName($_REQUEST['clientnumber'])?> : Schedule</p>
        </div>
        <div id="getDetailsbox"></div>

        <table id="assetTable" class="dt-print-table table table-striped table-sm table-bordered table-hover responsive" width="100%" style="width:100%; display:none">
            <thead>
                <tr>
                    <th class="noExport">Item ID</th>
                    <th class="noExport">Chain</th>
                    <th>Description</th>
                    <th>Plant Number</th>
                    <th>Serial Number</th>
                    <th>Safe Working Limit</th>
                    <th class="noExport">Location</th>
                    <th class="noExport">Post Code</th>
                    <th>A Defect</th>
                    <th>B Defect</th>
                    <th>Observations</th>
                    <th class="date-uk">Due Date</th>
                </tr>
            </thead>
            <tbody>
                <?php
        while($id=mysqli_fetch_array($ic)){
        $stmt = $GLOBALS['con']->prepare("SELECT `adefect`,`bdefect`,`obs` FROM `reportitems` WHERE `itemnumber`=? ORDER BY abs(insdate) DESC LIMIT 1")or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i',$id['itemnumber']);$stmt->execute();$r=$stmt->get_result();$stmt->close();
        $rd=mysqli_fetch_array($r);

        $ldesc=explode(":",$id['linked']);
        if(!empty($ldesc[0])){$chain="C".$ldesc[0];}
        else{$chain='';}
        ?>
                <tr>

                    <td><?= $id['itemnumber']?></td>

                    <td><?= $chain?></td>

                    <td><?= $id['desc']?></td>

                    <td><?= $id['plantnumber']?></td>

                    <td><?= $id['serialnumber']?></td>

                    <td><?= $id['swl']?></td>

                    <td><?= $id['locname']?></td>

                    <td><?= $id['postcode']?></td>

                    <td width="15%"><?= $rd['adefect']?></td>

                    <td width="15%"><?= $rd['bdefect']?></td>

                    <td width="15%"><?= $rd['obs']?></td>

                    <?php if(empty($id['duedate'])){?>
                    <td>No date stored</td>
                    <?php }
                    else{?>
                    <td><?= date('d-m-Y',$id['duedate'])?></td>
                    <?php }?>



                </tr>
                <?php }?>

            </tbody>
        </table>
        <div id="tableWrapper" class="d-none"></div>
    </div>
</div>
<?php
  $table="assetTable";
  $dateTarget="11";
  $sortTarget="1";
  $length="25";
  $sortCell="";
  $logo="yes";
  $scroller="";
require_once(ROOT_URL.'/includes/tables/print_table.php');
?>

<script>
document.getElementById('navBox').innerHTML =
    '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Client List</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&clientnumber=<?= $_REQUEST['clientnumber']?>&view=load"><?= getClientName($_REQUEST['clientnumber'])?></a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&clientnumber=<?= $_REQUEST['clientnumber']?>&view=asset">Schedule</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Schedule: Print</li>';

$(document).ready(function() {
    let table = $('#assetTable').DataTable();
    new $.fn.dataTable.SearchBuilder(table, {});
    table.searchBuilder.container().prependTo(table.table().container());
    var d = table.searchBuilder.getDetails();

    $(".dtsb-value").change(function() {
        alert("Handler for .change() called.");
    });

    var stored;
    $('.buttons-print').click(function(e) {
        e.preventDefault()

        stored = $('#assetTable').DataTable().searchBuilder.getDetails();
        var str = JSON.stringify(stored);
        str = JSON.parse(str);
        var str2 = JSON.stringify(stored, null, 4);
        var title = str['criteria'][0].data;
        var vals = str['criteria'][0].value;

        if (title == "Location") {
            $('#getDetailsbox').text(vals)
        }

    })

});
</script>