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


?>


<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 id="loadedContent">
    <div class="row">
        <div class="col-12 col-md-3 me-auto mb-3">
            <div id="clientSelect" class="input-group input-group-sm">
                <span class="input-group-text bg-info text-light" id="loadClientLabel">Load Client</span>
                <select class="form-control" data-mdb-size="sm" id="loadClientLocation" data-placeholder="search">

                    <option value=" 0"></option>
                    <?php while($pd=mysqli_fetch_array($cc)){?>
                    <option value="<?= $pd['clientnumber']?>"><?= $pd['clientname']?></option>
                    <?php }?>
                </select>
            </div>
        </div>
    </div>
    <div class="row mt-4">
        <div class="col-md-6 mb-3 text-center text-md-start">
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Filter Overdue Clients" class="btn color danger-color-dark text-light filterButtons btn-sm"
                    onClick="addSearch('PAST',0)">PAST</button>
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Filter Pending Clients" class="btn color warning-color-dark text-light filterButtons btn-sm"
                    onClick="addSearch('DUE',0)">DUE</button>
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Filter Clients not due" class="btn color success-color-dark text-light filterButtons btn-sm"
                    onClick="addSearch('CLEAR',0)">CLEAR</button>
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Reset Filters" class="btn color bg-white filterButtons btn-sm remove"
                    onClick="clearSearcher()"><i class="fad fa-refresh" aria-hidden="true"></i> Reset Filters</button>
        </div>
    </div>
    <?php
switch ($_REQUEST['mode']){
default:
if(file_exists(TENANT_URL.'/framework/dashboard_functions/location_report.txt')){
$record=file_get_contents(TENANT_URL.'/framework/dashboard_functions/location_report.txt');
}
if(file_exists('../../../'.TENANT_URL.'/framework/dashboard_functions/location_report.txt')){
    $record=file_get_contents('../../../'.TENANT_URL.'/framework/dashboard_functions/location_report.txt');
}
$record=explode("|",$record);


?>

    <table id="assetTable" class="dt-print-table table table-striped table-sm table-bordered table-hover responsive w-100" style="display:none">
        <thead>
            <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>
</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><span style="display:none">'.$cn.'</span><a href="index.php?t=includes/master&p=location_report.php&mode=client&clientnumber='.$cx.'">'.$cn.'</a></td>
<td>'.$ln.'</td>
<td>'.$p.'</td>
<td>'.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-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover" 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']);}
?>

<h4 class="text-center"><?=$sd['clientname']?></h2>
    <table id="assetTable" class="dt-print-table table table-striped table-sm table-bordered table-hover responsive w-100" style="display:none">
        <thead>
            <tr>
                <th>Client</th>
                <th>Location</th>
                <th>Post Code</th>
                <th>Date Due</th>
                <th>Status</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?= $output;?>
        </tbody>
    </table>

    <?php
break;
}?>


    <div class="row mt-4">
        <div class="col-md-6 mb-3 text-center text-md-start">
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Filter Overdue Clients" class="btn color danger-color-dark text-light filterButtons btn-sm"
                    onClick="addSearch('PAST',0)">PAST</button>
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Filter Pending Clients" class="btn color warning-color-dark text-light filterButtons btn-sm"
                    onClick="addSearch('DUE',0)">DUE</button>
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Filter Clients not due" class="btn color success-color-dark text-light filterButtons btn-sm"
                    onClick="addSearch('CLEAR',0)">CLEAR</button>
            <button type="button" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Reset Filters" class="btn color bg-white filterButtons btn-sm remove"
                    onClick="clearSearcher()"><i class="fad fa-refresh" aria-hidden="true"></i> Reset Filters</button>
        </div>
    </div>
    <?php 
$table="assetTable";
    $dateTarget="3";
    $sortTarget="1";
    $orderTarget="3";
    $length="25";
    $sortCell="";
    $logo="yes";
    $scroller="";
    $popup="true";
    ?>
    <?php 
if(file_exists(ROOT_URL.'/includes/tables/asset_table.php')){
require_once(ROOT_URL.'/includes/tables/asset_table.php');
}

if(file_exists('../tables/asset_table.php')){
    require_once('../tables/asset_table.php');
    }?>

    <script>
    <?php if (!empty($_REQUEST['search'])) { ?>

    var searchValue = '<?= $_REQUEST['search'] ?>';
    var searchInput = document.querySelector('.dt-search input');
    if (searchInput) {

        searchInput.value = searchValue;
        // Trigger a keyup event
        var event = new Event('keyup', {
            bubbles: true,
            cancelable: true,
        });
        searchInput.dispatchEvent(event);
    }

    <?php } ?>

    var locationSelect = document.getElementById('loadClientLocation');
    locationSelect.addEventListener('change', (e) => { // Correct event name
        var selection = e.target.value; // Get the selected value
        var name = e.target.options[e.target.selectedIndex].text; // Get the selected text
        var url = ROOT_URL + '/includes/locations/location_report.php?mode=client&clientnumber=' + encodeURIComponent(selection) + '&search=<?php echo isset($_REQUEST['search']) ? $_REQUEST['search'] : ''; ?>&clientname=' +
            encodeURIComponent(name) + '&tenant=' + TENANT_URL;
        killAllModals();
        setTimeout(function() {
            loadModalContent(url, 'popupBox', 'modal-xl', 1050, 'true', 'Inspections Due', 'close');
        }, 100);
    });
    </script>