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


if($_REQUEST['FCN']=="clients"){
$stmt = $GLOBALS['con']->prepare("
SELECT 
a.clientnumber, a.repitem, a.itemnumber, a.reptype, a.insdate, b.itemnumber, b.linked, b.statuslive
FROM reportitems a INNER JOIN itemdetails b 
ON b.itemnumber = a.itemnumber 
WHERE a.insdate >= '".strtotime($_REQUEST['start'])."' AND a.insdate <=  '".strtotime($_REQUEST['end'])."' AND b.statuslive='1' 
ORDER BY abs(a.repitem) DESC 
")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$ic=$stmt->get_result();$stmt->close();
$n=mysqli_num_rows($ic);
if($n<1){echo '<div class="col-12 alert alert-warning text-center">No clients inspected in the period specified</div>';}
else{
while($id=mysqli_fetch_array($ic)){$clients[]=$id['clientnumber'];}
$result = array_unique($clients);
?>
<div id="exportTitle" style="display:none; background-color:#353b50; color:#ffffff">
    <p style="font-size:14px; font-weight:bold">EnRep Reporting System</p>
    <p style="font-size:12px"><a href="">Item List</a> : <a href=""></a> : <a href="">Schedule</a> : <a href="">Active Items</a></p>
</div>

<table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive nowrap" width="100%">
    <thead class="table-dark">
        <tr>
            <th>Client</th>
            <th class="no-sort noExport"></th>
        </tr>
    </thead>
    <tbody>
        <?php
foreach($result as $c){
$stmt = $GLOBALS['con']->prepare("SELECT `clientname` FROM `clientdetails` WHERE `clientnumber`='".$c."'");
$stmt->execute();$ic=$stmt->get_result();$stmt->close();#
$id=mysqli_fetch_array($ic);
echo '<tr><td>'.$id['clientname'].'</td><td><a class="btn btn-secondary" onclick="loadClient(\''.$c.'\')">Load Data</a></td></tr>';
}
?>
    </tbody>
</table>
<?php }}

if($_REQUEST['FCN']=="items"){
$stmt = $GLOBALS['con']->prepare("SELECT `clientname` FROM `clientdetails` WHERE `clientnumber`='".$_REQUEST['clientnumber']."'");
$stmt->execute();$ic=$stmt->get_result();$stmt->close();#
$cn=mysqli_fetch_array($ic);
echo $_REQUEST['clientnumber']."-".$_REQUEST['start']."-".$_REQUEST['end'];
$stmt = $GLOBALS['con']->prepare("
SELECT 
a.clientnumber, a.repitem, a.itemnumber, a.reptype, a.insdate, b.itemnumber, b.plantnumber, b.plantnumber, b.desc, b.serialnumber, b.statuslive
FROM reportitems a INNER JOIN itemdetails b 
ON b.itemnumber = a.itemnumber 
WHERE a.clientnumber = '".$_REQUEST['clientnumber']."' AND a.insdate >= '".strtotime($_REQUEST['start'])."' AND a.insdate <=  '".strtotime($_REQUEST['end'])."' AND b.statuslive='1' 
ORDER BY abs(a.repitem) DESC 
")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$ic=$stmt->get_result();$stmt->close();
?>
<div id="exportTitle" style="display:none; background-color:#353b50; color:#ffffff">
    <p style="font-size:12px"><b><?= $cn['clientname']?></b> Item inspection List : <?= $_REQUEST['start']?> - <?= $_REQUEST['end']?></p>
    
</div>

<table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive nowrap" width="100%">
    <thead class="table-dark">
        <tr>
            <th>Item</th>
            <th>Plant Number</th>
            <th>Serial Number</th>
            <th>Location</th>
            <th>Inspection Date</th>
        </tr>
    </thead>
    <tbody>
        <?php

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

echo '<tr><td>'.$id['desc'].'</td><td>'.$id['plantnumber'].'</td><td>'.$id['serialnumber'].'</td><td>'.getItemLocation($id['itemnumber'],'name').' - '.getItemLocation($id['itemnumber'],'postcode').'</td><td>'.date('d-m-Y',$id['insdate']).'</td></tr>';
}
?>
    </tbody>
</table>
<?php }

     $table = "datatable";
     $pageTitle="Inspection Report";
            $dateTarget = "4";
            $length = "25";
            $sortTarget = "";
            $sortCell = "nth-child(2)";
            $logo = "yes";
            $scroller = "";
            require("../tables/single_table.php");?>