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

 function getLastRep($item){
        $sql=mysqli_query($GLOBALS['con'],"SELECT `result`,`reptype` FROM `reportitems` WHERE `itemnumber`='".$item."' ORDER BY abs(repitem) DESC LIMIT 1");
        $id=mysqli_fetch_array($sql);
        if($id['result']=="DEFECTS" ){$col=" warning-color-dark";$tag="DEFECTS";$resultTitle="DEFECTS"; $resultText=$id['bdefect'];}
        if($id['result']=="URGENT" ){$col=" danger-color-dark";$tag="URGENT";$resultTitle="URGENT"; $resultText=$id['adefect'];}
        if($id['result']=="PASS" ){$col=" success-color-dark";$tag="PASS"; $resultTitle="PASS"; $resultText="The item passed examination<br>";}
        if($id['result']=="OBS" ){$col=" special-color";$tag="OBS"; $resultTitle="OBS"; $resultText=$id['obs'];}
        
        if($id['result']=="" ){$col=" secondary-color";$tag="P2UD"; $resultTitle="P2UD"; $resultText="No report or report is prior to update<br>";}
        if($id['reptype']=="518" ){$col=" stylish-color";$tag="NIR"; $resultTitle="NIR"; $resultText=$id['pnareason'];}
        if($id['reptype']=="503" ){$col=" stylish-color";$tag="PNA"; $resultTitle="PNA"; $resultText=$id['pnareason'];}
    
        $ret='<td class="text-center p-0" data-toggle="popover" data-placement="right" title="'.$resultTitle.'" data-content="'.$resultText.'"><span class="badge badge-pill d-block '.$col.'"  style="font-size:10px" >'.$tag.'</span></td>';
    return $ret;
    }

if(!empty($_REQUEST['location'])){$loc="AND a.locnumber='".$_REQUEST['location']."'"; $name="@ ".$_REQUEST['name'];}
$stmt = $GLOBALS['con']->prepare("SELECT 
a.itemnumber,
a.locnumber,
a.desc,
a.code,
a.currdate,
a.plantnumber,
a.serialnumber,
a.model,
a.dateofmanf,
a.manf,
a.duedate,
a.linked,
a.itemnotes,
a.statuslive,
b.locname,
b.locnotes,
b.postcode

FROM itemdetails a 
INNER JOIN locations b ON a.locnumber=b.locnumber 

WHERE a.clientnumber='".$_REQUEST['clientnumber']."' AND a.statuslive='1' $loc")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";}

if($id['result']=="DEFECTS" ){$col=" warning-color-dark";$tag="DEFECTS";$resultTitle="DEFECTS<br>".$id['bdefect'];}
if($id['result']=="URGENT" ){$col=" danger-color-dark";$tag="URGENT";$resultTitle="URGENT<br>".$id['adefect'];}
if($id['result']=="PASS" ){$col=" success-color-dark";$tag="PASS"; $resultTitle="PASS";}
if($id['result']=="OBS" ){$col=" special-color";$tag="OBS"; $resultTitle="OBS<br>".$id['obs'];}
if($id['reptype']=="503" ){$col=" stylish-color";$tag="PNA"; $resultTitle="PNA<br>".$id['pnareason'];}
if($id['result']=="" ){$col="  secondary-color";$tag="P2UD"; $resultTitle="P2UD";}


$ret.='
<tr>
<td class="'.$stat.'" ><div class="'.$stat.'"  style="font-size:0px" >'.$statTitle.'</div></td>
<td>'.$id['itemnumber'].'</td>
<td>'.$id['desc'].'</td>
<td>'.$id['plantnumber'].'</td>
<td>'.$id['serialnumber'].'</td>
<td>'.$id['locname'].'</a></td>
<td>'.$id['postcode'].'</a></td>';

if(empty($id['duedate'])){$ret.='<td>No date stored</td>'; }
else{$ret.='<td>'.date('d-m-Y',$id['duedate']).'</td>';}
$ret.=getLastRep($id['itemnumber']);
$ret.='</tr>';
}

$pdf='

<table class="table table-sm" width="100%">
<tbody>
<tr valign="top">
<td width="80%" valign="top"><h3>Schedule - '.getClientName($_REQUEST['clientnumber']).' '.$name.'</h3></td>
<td width="20%" align="right" valign="top" style="font-family: serif; font-size: 8pt; color: #000000; font-weight: bold;">'.getClientAddress($_REQUEST['clientnumber']).'</td>
</tr>
</tbody>
</table>

<table id="coverDetails" class="table table-sm table-striped" width="100%">
<thead class="headRow text-dark">
<tr class="headrow">
<th class="no-sort noExport text-center p-0" width="20px">&nbsp;</th>
<th>Item ID</th>
<th>Description</th>
<th>Plant Number</th>
<th>Serial Number</th>
<th>Location</th>
<th>Post Code</th>
<th class="date-uk">Due Date</th>
<th class="no-sort noExport">Last Report</th>
</tr>
</thead>
<tbody>
'.$ret.'
</tbody>
</table>';


echo $pdf;
?>