<style>
    .datatables{
        border: 2px solid #444 !important;
    }
    .datatables th, .datatables td{
        border: 1px solid #444 !important;
    }
    .tableTitle{
        font-size: 1.5em;
        font-weight: bold;
    }
    .tableTitle2{
        font-size: 1.2em;
        font-weight: bold;
    }
    .tableheader1{
        width: 20%;
        font-weight: bold;
    }
    .tableheader2{
        font-weight: bold;
    }
    .subheader{
        font-weight: bold;
        text-align: center;
    }
    .subheader2{
        font-weight: bold;
    }
</style>
<script>
    $(function(){
        $('.datatables').DataTable();
    })
</script>
<?php
if ( ! function_exists('GetStateName')) {
	function GetStateName($s) {
    $CI = & get_instance();
		$t = $CI->general_model->get_all('_ref_state');

		if(!$t) return NULL;
		$r = NULL;
		foreach($t as $key=>$value) {
			$i = (isset($value->fldid)) ? $value->fldid : 0;
			$d = (isset($value->fldname)) ? $value->fldname : 0;
			if($i == $s) {
				$r = $d;
				break;
			}
		}
		return $r;
	}
}

if ( ! function_exists('GetMethodScoring')) {
	function GetMethodScoring($b, $m) {
		if(!$m) return "SCORETENNULL";
		if(!$b) return "SCORETENNULL";
		$score = NULL;
		foreach($m as $key=>$value) {
			if(strtoupper($b) == strtoupper($value['methodCode'])) {
				$score = strtoupper($value['methodScoring']);
			}
			if ($score) break;
		}

		return ($score) ? $score : "SCORETENNULL";
	}
}
?>
<section class="container"><br><br><br>
	<div class="row printbreak">
		<div class="col-xs-12">
			<div class="tableTitle">1. MAKLUMAT SYARIKAT</div>
			<table class="table table-bordered datatables">
				<tr>
					<th class="tableheader1 text-right">Nama Syarikat</th>
					<td class="" colspan="2"><?php echo $info['companyName']; ?>&nbsp;(<?php echo $info['companyNo']; ?>)</td>
				</tr>
				<tr>
					<th class="tableheader1 text-right" rowspan="2">Lokasi</th>
					<td class=" subheader">Ibu Pejabat</td>
					<td class=" subheader">Cawangan</td>
				</tr>
				<tr>
					<td class="">
					<?php
					if($info['address']['perm']) {
						?>
						<div class="row">
							<div class="col-md-4"><strong>Alamat Berdaftar</strong></div>
							<div class="col-md-8">
							<?php
								$address  = $info['address']['perm']['address'];
								$address .= ($info['address']['perm']['city']) ? ", ".$info['address']['perm']['city'] : NULL;
								$address .= ($info['address']['perm']['postCode']) ? ", ".$info['address']['perm']['postCode'] : NULL;
								$address .= ($info['address']['perm']['stateID']) ? " ".(GetStateName($info['address']['perm']['stateID'])) : NULL;
								echo ucwords(strtolower($address));
							?>
							</div>
						</div>
						<?php
					}
					if($info['address']['corres']) {
						?>
						<div class="row">
							<div class="col-md-4"><strong>Alamat Surat-menyurat</strong></div>
							<div class="col-md-8">
							<?php
								$address  = $info['address']['corres']['address'];
								$address .= ($info['address']['corres']['city']) ? ", ".$info['address']['corres']['city'] : NULL;
								$address .= ($info['address']['corres']['postCode']) ? ", ".$info['address']['corres']['postCode'] : NULL;
								$address .= ($info['address']['corres']['stateID']) ? " ".(GetStateName($info['address']['corres']['stateID'])) : NULL;
								echo ucwords(strtolower($address));
							?>
							</div>
						</div>
						<?php
					}
					?>
					</td>
					<td class="">
					<?php
					if($branches) {
						foreach($branches as $key=>$branch) {
						?>
						<div class="row">
							<div class="col-md-12">
							<strong><?php echo ($key+1); ?>)&nbsp;</strong>
							<?php
								$address  = $branch['address'];
								$address .= ($branch['city']) ? ", ".$branch['city'] : NULL;
								$address .= ($branch['postCode']) ? ", ".$branch['postCode'] : NULL;
								$address .= ($branch['stateID']) ? " ".(GetStateName($branch['stateID'])) : NULL;
								echo ucwords(strtolower($address));
							?>
							</div>
						</div>
						<?php
						}
					}
					?>
					</td>
				</tr>
				<tr>
					<th class="tableheader1 text-right">Hubungi</th>
					<td class="">
						<div><strong>No Telefon : </strong><?php echo ($info['contact'] && $info['contact']['phoneNo']) ? $info['contact']['phoneNo'] : '-'; ?></div>
						<div><strong>No Fax : </strong><?php echo ($info['contact'] && $info['contact']['faxNo']) ? $info['contact']['faxNo'] : '-'; ?></div>
					</td>
					<td class="">
						<div><strong>Emel : </strong><?php echo ($info['contact'] && $info['contact']['email']) ? $info['contact']['email'] : '-'; ?></div>
						<div><strong>Laman Web : </strong><?php echo ($info['contact'] && $info['contact']['website']) ? $info['contact']['website'] : '-'; ?></div>
					</td>
				</tr>
			</table>
		</div>
	</div>

	<?php
	if($review['method']) {
		foreach($review['method'] as $key=>$value) {
		?>
		<div class="row printbreak">
			<div class="col-xs-12">
				<?php echo ($key == 0) ? "<div class=\"tableTitle\">2. RINGKASAN PEMARKAHAN</div><br /><br />\n" : NULL; ?>
				<table class="table table-bordered datatables">
					<tr>
							<th class="tableheader2 text-center" width="60%" colspan="2">KRITERIA</th>
							<th class="tableheader2 text-center" width="40%" colspan="3">MARKAH DIPEROLEHI</th>
					</tr>
					<tr>
							<td class="subheader2" colspan="2"><?php echo $value['methodCode'] . ". " . $value['methodDesc']; ?></td>
							<td class="subheader" width="5%">Skema Markah</td>
							<td class="subheader" width="5%">Markah</td>
							<td class="subheader" width="30%">Ulasan/Rujukan</td>
					</tr>
					<?php
					$total = 0;
					if($review['details'] && $review['details'][$value['methodCode']]) {
						foreach($review['details'][$value['methodCode']] as $index=>$item) {
							$text = nl2br($item['text']);
							if(isset($item['children'])) {
								$text .= "<br />";
								foreach($item['children'] as $child) {
									$text .= "<br />";
									$text .= $child['code'].". ".nl2br($child['text']);
								}
							}
							?>
							<tr>
									<td class="subheader"><?php echo ($index+1); ?></td>
									<td class=""><?php echo $text; ?></td>
									<td class="text-center"><?php echo ($value['methodScoring'] == "SCORETENNULL") ? 10 : 5; ?></td>
									<td class="text-center">
										<?php
										if ($item['answer'] && $item['answer']['status']) {
											if (strtoupper($value['methodScoring']) == "SCORETENNULL") {
												if(strtoupper($item['answer']['status']) == "MEMATUHI") {
													echo "10";
													$total += 10;
												} else if(strtoupper($item['answer']['status']) == "TIDAK MEMATUHI") {
													echo "0";
												} else {
													echo "-";
												}
											} else {
												$mark = NULL;
												if($item['answer']['status'] == 'TIDAK MEMATUHI') $mark = 0;
												if($item['answer']['status'] == 'MEMATUHI') $mark = ($item['answer']['mark']) ? $item['answer']['mark']:NULL;
												if($item['answer']['status'] == 'SEDANG DIPROSES') $mark = "-";
												$total += ($mark && $mark != "-") ? $mark : 0;
												echo $mark;
											}
										} else {
											echo "-";
										}
										?>
									</td>
									<td class=""><?php echo $item['answer']['comment']; ?></td>
							</tr>
							<?php
						}
					}
					$review['method'][$key]['total'] = $total;
					?>
					<tr>
							<td class="subheader2" colspan="2">JUMLAH KESELURUHAN MARKAH</td>
							<td class="subheader" width="5%"><?php echo (($value['methodScoring'] == "SCORETENNULL") ? 10 : 5) * (($review['details'] && $review['details'][$value['methodCode']]) ? count($review['details'][$value['methodCode']]) : 0); ?></td>
							<td class="subheader" width="5%"><?php echo $total; ?></td>
							<td class="subheader" width="30%"></td>
					</tr>
				</table>
			</div>
		</div>
		<?php
		}
	}

	if($review['method']) {
		?>
		<br /><br />
		<div class="row">
			<div class="col-xs-12">
				<div class="tableTitle">3. RINGKASAN MARKAH AUDIT</div>
				<table class="table table-bordered datatables">
					<tr>
						<th class="tableheader2 text-center" colspan="2">Bahagian</th>
						<th class="tableheader2 text-center">Skema Markah</th>
						<th class="tableheader2 text-center">Markah Diperolehi</th>
					</tr>
					<?php
					$grand['scheme'] = 0;
					$grand['total'] = 0;
                    $abcdef = array();
					foreach($review['method'] as $key=>$value) {
						$scheme = (($value['methodScoring'] == "SCORETENNULL") ? 10 : 5) * (($review['details'] && $review['details'][$value['methodCode']]) ? count($review['details'][$value['methodCode']]) : 0);
                        $abcdef[] = $value;
						$grand['scheme'] += $scheme;
						$grand['total'] += $value['total'];
						?>
						<tr>
								<td class="subheader" width="5%"><?php echo $value['methodCode']; ?></td>
								<td><?php echo $value['methodDesc']; ?></td>
								<td class="subheader"><?php echo $scheme; ?></td>
								<td class="text-center"><?php echo $value['total']; ?></td>
						</tr>
						<?php
					}
					?>
					<tr>
							<td class="subheader2" colspan="2">JUMLAH KESELURUHAN MARKAH</td>
							<td class="subheader" width="5%"><?php echo $grand['scheme']; ?></td>
							<td class="subheader" width="5%"><?php echo $grand['total']; ?></td>
					</tr>
				</table>
			</div>
			<div class="col-xs-12">*** Syarikat dikira gagal sekiranya Bahagian A kurang daripada 90 markah.</div>
		</div>
		<br /><br />
		<?php
	}
	?>

	<div class="row">
		<?php
		// CHECK PASS/FAIL
		$pf = "LULUS";
		foreach($review['method'] as $key=>$value) {
			$scheme = (($value['methodScoring'] == "SCORETENNULL") ? 10 : 5) * (($review['details'] && $review['details'][$value['methodCode']]) ? count($review['details'][$value['methodCode']]) : 0);
			$mark = $value['total'];
			if(($value['methodScoring'] == "SCORETENNULL") && $mark < $scheme) {
				$pf = "GAGAL";
			}
		}
		?>
		<div class="col-xs-12">
			<div class="tableTitle">GRED SYARIKAT</div>
			<?php
			if($grades) {
				?>
				<table class="table table-bordered datatables">
					<tr>
						<th class="tableheader" width="20%">JULAT MARKAH</th>
						<?php
						foreach($grades as $grade) {
							?><td class="tableheader text-center" width="20%"><?php echo $grade['min']." - ".$grade['max']; ?></td><?php
						}
						?>
					</tr>
					<tr>
						<th class="subheader2">GRED</th>
						<?php
						foreach($grades as $grade) {
							?><td class="subheader text-center" width="20%"><?php echo $grade['grade']; ?></td><?php
						}
						?>
					</tr>
					<tr>
						<th class="subheader2">KEPUTUSAN</th>
						<?php
						foreach($grades as $grade) {
							?><td class="text-center" width="20%"><?php echo $grade['result']; ?></td><?php
						}
						?>
					</tr>
					<tr>
						<th class="subheader2">CADANGAN</th>
						<?php
						foreach($grades as $grade) {
							?><td class="text-center" width="20%"><?php echo $grade['suggestion']; ?></td><?php
						}
						?>
					</tr>
					<tr>
						<th>GRED SYARIKAT</th>
						<?php
						$gradeReceived = NULL;
						$gradeSuggestion = NULL;
                        // var_dump( $abcdef[0]['total']);
                        if ($abcdef[0]['total'] < 90) { ?>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td class="text-center"><i class="fa fa-check"></i></td>
                        <?php
                        $gradeReceived = 'D'; $gradeSuggestion = 'TIDAK LAYAK';
                        } else {
                            foreach($grades as $grade) {
    							if($grand['total'] >= $grade['min'] && $grand['total'] <= $grade['max']) {
    								?><td class="text-center"><i class="fa fa-check"></i></td><?php $gradeReceived = $grade['grade']; $gradeSuggestion = $grade['suggestion'];
    							} else {
    								?><td></td><?php
    							}
    						}
                        }
						$pf = ($gradeReceived == "D") ? 'GAGAL' : $pf;
						?>
					</tr>
				</table>
				<?php
			}
			?>
			<br/>
			<div style="font-size:1.2em;">
				KEPUTUSAN MARKAH AUDIT SYARIKAT ADALAH
					<small style="text-decoration: underline; font-size: 0.85em; margin: 0 5px;">&nbsp;&nbsp;&nbsp;<?php echo $grand['total']; ?>&nbsp;&nbsp;&nbsp;</small>
						DAN SYARIKAT <?php echo strtoupper($info['companyName']); ?> <small style="text-decoration: underline; font-size: 0.85em; margin: 0 5px;">&nbsp;&nbsp;&nbsp;<?php echo $gradeSuggestion; ?>&nbsp;&nbsp;&nbsp;</small>.
                        <?php if ($pf == 'GAGAL') { ?>
                            SYARIKAT DIKIRA GAGAL SEKIRANYA BAHAGIAN A KURANG DARIPADA 90 MARKAH.
                        <?php } ?>
			</div>
		</div>
	</div>
	<br /><br />

	<div class="row" style="page-break-after: avoid;">
		<div class="col-xs-12">
			<div class="tableTitle">TANDATANGAN</div>
			<table class="table table-bordered datatables" width = "100%">
				<tr>
					<th class="subheader" width="25%" colspan="2">JURUAUDIT I</th>
					<th class="subheader" width="25%" colspan="2">JURUAUDIT II</th>
					<th class="subheader" width="25%" colspan="2">DISEMAK</th>
					<th class="subheader" width="25%" colspan="2">DIPERAKUI</th>
				</tr>
				<tr>
					<td class="text-center">TANDATANGAN</td>
					<td class="text-center">TARIKH</td>
					<td class="text-center">TANDATANGAN</td>
					<td class="text-center">TARIKH</td>
					<td class="text-center">TANDATANGAN</td>
					<td class="text-center">TARIKH</td>
					<td class="text-center">TANDATANGAN</td>
					<td class="text-center">TARIKH</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</table>
		</div>
	</div>
</section>
