<?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('../../includes/master/form_functions.php');
 if(!file_exists(TENANT_URL.'/output/reports/'.$_REQUEST['clientnumber'])){
    mkdir(TENANT_URL.'/output/reports/'.$_REQUEST['clientnumber']);	
    }
//$_REQUEST['repnumber']=1499725635;
//$_REQUEST['item']=315;
//$_REQUEST['form']=503;


foreach($_REQUEST['items'] as $item){ 
    
$tda=explode("-",$item);

$rno=getItemReportType($tda[0]);

$rx=mysqli_query($GLOBALS['con'],"SELECT * FROM `reportitems` WHERE `repnumber`='".$tda[0]."' AND `itemnumber`='".$tda[1]."' ")or die(mysqli_error($GLOBALS['con']));
$cx=mysqli_fetch_array($rx);

$contract=getItemOwner($cx['itemnumber']);
$clt=explode("|",$contract);
$details=getItemDetailed($tda[0],$cx['itemnumber']);
$det=explode("|",$details);
$post='
<table class="tableClientDetails" width="100%">
<tr valign="top">
<td width="30%" valign="top">
<table width="100%">
<tr><th width="25%" class="themeColour">Owner</th><td>'.strtoupper($clt[0]).'</td></tr>
<tr valign="top"><th valign="top"></th><td valign="top">'.getClientAddress($cx['clientnumber'],'address').'</td></tr>
</table>
</td>

<td valign="top">
<table id="clientDetails" width="100%">
<tr valign="top"><th width="20%" class="themeColour">Location</th><td>'.strtoupper(getItemLocation($tda[1],'name')).'</td></tr>
<tr valign="top"><th valign="top"></th><td valign="top">'.getItemLocation($tda[1],'address').'</td></tr>
</table>
</td>

<td valign="top">
<table id="repDetails" class="table-sm" width="100%">
<tr><td align="right"><i>'.nl2br(getItemReportDesc($tda[0],'regs')).'</i></td></tr>
<tr><td align="right" style="font-size:12px"><strong>'.getItemReportDesc($tda[0],'formtype').'</strong></td></tr>
<tr><td align="right"><small><i>Contract Number:</i> '.$clt[1].'</small></td></tr>
<tr><td align="right"><small><i>Report Number:</i> '.$tda[0].'</small></td></tr>
</table>
</td>
</tr>
</table>
';
if($rno=="503"){
    
 $multirow='
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td width="50%" valign="top">
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td style="border-right:1px solid #000000" width="20%" valign="top"><strong>Dates</strong><br>Last (if known)<br>Current<br>Next<br>Item Ref:</td>
<td style="border-right:1px solid #000000" valign="top"><strong>Item Details</strong><br>1: Description; 2: Plant Number; 3: Serial Number; 4:Manufacturer; 5: Date of Manufacture; 6: Model; 7: Sublocation; 8: Detailed Location</td>
<td style="border-right:1px solid #000000" width="20%" valign="top"><strong>Safe Working Load</strong></td>
</tr>
</table>
</td>

<td width="50%" valign="top">
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td valign="top"><b>Reason(s) for being reported as "Not Available"</b> </td>
</tr>
</table>
</td>
</tr>
</table>
<br>
';   
}
if($rno=="502"){
    
 $multirow='
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td width="50%" valign="top">
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td style="border-right:1px solid #000000" width="20%" valign="top"><strong>Dates</strong><br>Last (if known)<br>Current<br>Next<br>Item Ref:</td>
<td style="border-right:1px solid #000000" valign="top"><strong>Item Details</strong><br>1: Description; 2: Plant Number; 3: Serial Number; 4:Manufacturer; 5: Date of Manufacture; 6: Model; 7: Sublocation; 8: Detailed Location</td>
<td style="border-right:1px solid #000000" width="20%" valign="top"><strong>Safe Working Load</strong></td>
</tr>
</table>
</td>

<td width="50%" valign="top">
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td valign="top"><b>A:</strong> Defects which could cause danger to persons. Details of repairs, renewals or alterations necessary to rectify such defects and the time in which they should be completed.<br>
<strong>B:</strong> Other Defects requiring attention.<br>
<strong>C:</strong> Observations and Recommendations.<br>
<b>*Reason(s) for being reported as "Partial"</b> </td>
</tr>
</table>
</td>
</tr>
</table>
<br>
';   
}
if($rno!="502" && $rno!="503"){
$multirow='
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td width="50%" valign="top">
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td style="border-right:1px solid #000000" width="20%" valign="top"><strong>Dates</strong><br>Last (if known)<br>Current<br>Next<br>Item Ref:</td>
<td style="border-right:1px solid #000000" valign="top"><strong>Item Details</strong><br>1: Description; 2: Plant Number; 3: Serial Number; 4:Manufacturer; 5: Date of Manufacture; 6: Model; 7: Sublocation; 8: Detailed Location</td>
<td style="border-right:1px solid #000000" width="20%" valign="top"><strong>Safe Working Load</strong></td>
</tr>
</table>
</td>

<td width="50%" valign="top">
<table id="repText" style="width:100%" cellpadding="2">
<tr>
<td valign="top"><b>A:</strong> Defects which could cause danger to persons. Details of repairs, renewals or alterations necessary to rectify such defects and the time in which they should be completed.<br>
<strong>B:</strong> Other Defects requiring attention.<br>
<strong>C:</strong> Observations and Recommendations.</td>
</tr>
</table>
</td>
</tr>
</table>
<br>
';
}}
$pdf.'
<div style="display:block; border-top: 1px solid #06C; margin-top:20px;  margin-bottom:20px"></div>

';

foreach($_REQUEST['items'] as $item){
$tda=explode("-",$item);

$rno=getItemReportType($tda[0]);
$rx=mysqli_query($GLOBALS['con'],"SELECT * FROM `reportitems` WHERE `repnumber`='".$tda[0]."' AND `itemnumber`='".$tda[1]."' ")or die(mysqli_error($GLOBALS['con']));
$cx=mysqli_fetch_array($rx);

$ix=mysqli_query($GLOBALS['con'],"SELECT * FROM `itemdetails` WHERE `itemnumber`='".$tda[1]."' ")or die(mysqli_error($GLOBALS['con']));
$id=mysqli_fetch_array($ix);

$lx=mysqli_query($GLOBALS['con'],"SELECT `locname` FROM `locations` WHERE `locnumber`='".$id['locnumber']."' ")or die(mysqli_error($GLOBALS['con']));
$ld=mysqli_fetch_array($lx);

/////////////////////////

   
$data = getCompanyDetails('web') . '/preview.php?repnumber=' . $tda[0];
$itemId = $tda[1];

// Use cURL to call the external PHP script using localhost
$ch = curl_init();

// Build the URL using localhost (make sure your server supports it)
$qrCodeUrl = 'https://enrepcore.enrep.co.uk/functions/generators/qrGen.php'; // Adjust path as needed

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $qrCodeUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Pass data as GET parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'data' => $data,
    'item' => $itemId,
    'tenant' => $_REQUEST['tenant']
]));

// Execute the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    //echo 'QR code generated successfully for item: ' . $itemId . '<br>';
}

// Close the cURL session
curl_close($ch);


$pdf.='
<table width="100%" style="margin-bottom:20px">
<tbody>
<tr class="rowtitle">
<td class="celltitle">Item Number: <strong>'.$cx['itemnumber'].'</strong></td>
<td class="celltitle" align="right">Report Number: <strong><a class="celllink" href="https://[REPORT_PATH]/preview.php?repnumber='.$cx['repnumber'].'" alt="View Individual Report" title="View Individual Report">'.$cx['repnumber'].'</a></strong></td></tr>
<tr>

<td width="50%" valign="top" style="border:1px solid #1B316E;">
<table id="repText" width="100%">
<tbody>
<tr>
<td style="border-right:1px solid #1B316E" rowspan="8"  width="20%" valign="top">';
if($cx['olddate']!="Not Known" && $cx['olddate']!="" && $cx['olddate']!="0"){$pdf.=date('d-m-Y',$cx['olddate']).'<br>';}else{$pdf.='Not Known<br>';}
if($cx['insdate']!="" && $cx['insdate']!="0"){$pdf.=date('d-m-Y',$cx['insdate']).'<br>';}else{$pdf.='Not Known<br>';}
if($cx['nextDate']!="" && $cx['nextDate']!="0"){$pdf.=date('d-m-Y',$cx['nextDate']).'<br>';}else{$pdf.='Not Known<br>';}

$pdf.='
<img src="../../..' . TENANT_URL . '/images/' . $itemId . '.png" height="40px">
</td>
<td style="border-right:1px solid #1B316E">1: '.$id['desc'].'</td>
<td rowspan="8"  width="20%" valign="top">'.$id['swl'].'</td>
</tr>
<tr><td style="border-right:1px solid #1B316E">2: '.$id['plantnumber'].'</td></tr>
<tr><td style="border-right:1px solid #1B316E">3: '.$id['serialnumber'].'</td></tr>
<tr><td style="border-right:1px solid #1B316E">4: '.$id['manf'].'</td></tr>
<tr><td style="border-right:1px solid #1B316E">5: ';
if($id['dateofmanf']!=""){$pdf.=$id['dateofmanf'];}
else{$pdf.='None Recorded';}
$pdf.='</td></tr>';

$pdf.='
<tr><td style="border-right:1px solid #1B316E">6: '.$id['model'].'</td></tr>
<tr><td style="border-right:1px solid #1B316E">7: '.$id['subloc'].'</td></tr>
<tr><td style="border-right:1px solid #1B316E">8: '.$ld['locname'].'</td></tr>
</tbody>
</table>
</td>
';
if($rno=="503"){
    $pdf.='
<td width="50%" valign="top" height="100%" style="border:1px solid #1B316E; height:100%">
<table id="repText"  width="100%" height="100%">
<tr><td> '.nl2br($cx['pnareason']).'</td></tr>
</table> 
</td>
';

}
if($rno=="502"){
    $pdf.='
<td width="50%" valign="top" height="100%" style="border:1px solid #1B316E; height:100%">
<table id="repText"  width="100%" height="100%">
<tr><td>A: '.nl2br($cx['adefect']).'</td></tr>
<tr><td>B: '.nl2br($cx['bdefect']).'</td></tr>
<tr><td>C: '.nl2br($cx['obs']).'</td></tr>
<tr><td><br>*'.nl2br($cx['partialReason']).'</td></tr>
</table> 
</td>
';

}
if($rno!="502" && $rno!="503"){
$pdf.='
<td width="50%" valign="top" height="100%" style="border:1px solid #1B316E; height:100%">
<table id="repText"  width="100%" height="100%">
<tr><td>A: '.nl2br($cx['adefect']).'</td></tr>
<tr><td>B: '.nl2br($cx['bdefect']).'</td></tr>
<tr><td>C: '.nl2br($cx['obs']).'</td></tr>
</table> 
</td>
';

}
$pdf.='
</tr>
</tbody>
</table>
';

$_REQUEST['repnumber']=$cx['repnumber'];
}

if($rno=="503"){
    $orientation="P";
    $footerTemplate="footer_pna.php";
}
else{
    $orientation="P";
    $footerTemplate="footer_generic.php";   
}

$multipath=strtotime('now').'_multi_'.$cx['esname'];

if($cx['olddate']=="" || $cx['olddate']=="Not Known"){$olddate="Not Known";}
else{$olddate=date('d-m-Y',$cx['olddate']) ;}

if($_REQUEST['mode']=="preview"){
$filestore='../../..'.TENANT_URL.'/output/reports/'.$_REQUEST['clientnumber'].'/preview.pdf';
$orientation="L";
}
else{
if($_REQUEST['mode']=="hse"){
$filestore='../../..'.TENANT_URL.'/output/reports/'.$_REQUEST['clientnumber'].'/'.$_REQUEST['repnumber'].'_'.$cx['esname'].'_HSE.pdf';
$orientation="L";
}
else{
$filestore='../../..'.TENANT_URL.'/output/reports/'.$_REQUEST['clientnumber'].'/'.$multipath.'.pdf';
}
}


$_REQUEST['repnumber']=$cx['repnumber'];
include('../../includes/engines/ext.pdf.engine');
echo $multipath;

?>