<?php
if(isset($_GET['acc'])) {
	$ACCESSNUMBER = $_GET['acc'];
	$pdf = 0;
	if(isset($_GET['pdf'])) { $pdf = $_GET['pdf']; }
} else {
	$file = $argv[1] ;
	$filename = explode('\\',$file);
	$filename = $filename[1];
	$ACCESSNUMBER = $filename;
	$pdf = 1;
}

if(isset($_GET['eng'])) { $eng = 1; } else { $eng = 0;}


include("config.php");
include("_function.php");

$raw = "<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
if($pdf==0) { $raw.= "<link rel='stylesheet' href='/spooler_db/normalize.min.css' /> <link rel='stylesheet' href='/spooler_db/style.css' />"; }
else { $raw .= "<link rel='stylesheet' href='../normalize.min.css' /> <link rel='stylesheet' href='../pdf.css' />"; }
$raw .= "</head>
<body>";

$HOSTNUMBER = getHost($conn, $ACCESSNUMBER);
$result = getResult($conn, $ACCESSNUMBER,$eng);
$info = getData2($conn,$ACCESSNUMBER);
$notes = getNotes($conn, $ACCESSNUMBER);
$collData = getCollData($conn, $ACCESSNUMBER);
$recvData = getRecvData($conn, $ACCESSNUMBER);
$noSample = getNoSample($conn,$ACCESSNUMBER);
if( $noSample == '' ) {
	$status = getStatus($conn, $ACCESSNUMBER);
} else {
	$status = "PENDING";
}
$valBy = getValBy($conn, $ACCESSNUMBER);
$date = date('d-m-Y H:i');
$npage = count($result);
$i=1;

foreach($result as $page) {
$raw .= "<div id='page'>
<div id=pagetop style='height:0.01cm'> </div>";
if($pdf==1) { $raw .= "<img src='../gleneagleshdr.png' class='img'/>"; }
$raw .= "<div id='dinfo'>
$info
</div>
<div id='dresult'>
<table class='result'>
	<colgroup>
		<col style='width:26%;'></col> 
		<col style='width:10%;'></col> 
		<col style='width:15%;'></col> 
		<col style='width:12%;'></col> 
		<col style='width:10%;'></col> 
		<col style='width:15%;'></col> 
		<col style='width:12%;'></col> 
	</colgroup>
	<tr> <th rowspan='2' style='border-right:solid 1px black; border-bottom:solid 1px black;'>TEST</th>  
		<th colspan='3' style='border-right:solid 1px black;'>CONVENTIONAL</th> <th colspan='3'>INTERNATIONAL</th> </tr>
	<tr style='border-bottom:solid 1px black;'> <th>RESULT</th> <th>REF. RANGES</th> <th style='border-right:solid 1px black;'>UNIT</th> <th>RESULT</th> <th>REF. RANGES</th> <th>UNIT</th> </tr>
	$page
";
// lastpage show note
if($i != $npage) {
	$raw.="</table>";
} else { 
	$raw .= "$noSample</table>
	<table> <tr><td>Note :</td> <td><pre>$notes</pre></td></tr>
	</table>";
}
$raw .= "</div>";
$raw .= "<div id='footer'>
	<table class='footer'>
		<tr> <td>";
if($i == $npage) { $raw .= "Status : $status"; }
$raw .= "<pre class='small'>Collected on $collData
Received on $recvData</pre>
Page $i/$npage Printed By : $valBy $date </td>";
if($pdf!=1) {
$raw .="
<td class='right'><pre>

(__________________)
Authorised Signature
</pre></td>";
} else {
	$raw.="<td class='right'><pre><b>&rdquo;This result is valid without signature.&rdquo;</b></pre></td>";
}
$raw .="
		</tr>
	</table>
</div>
";
//echo "<img src='gleneaglesftr.png' class='footer-img'/>";
if($pdf==1) { $raw .="<img src='../gleneaglesftr.png' class='img'/>"; }
$raw .= "</div>";
$i+=1;
}
$raw .="</body>";

//echo $raw;

if($pdf == 0) { echo $raw; }
else {
	if(!isset($_GET['dl'])) {
		$file = fopen("process_pdf/$HOSTNUMBER.html","w");
		fwrite($file, $raw);
		fclose($file);
	} else { echo $raw; }
}

?>
