<?php
$login = SESSION;
$login = $_SESSION[$login]['login'];

$width = 0;
$vWdth = array();
$hed1 = array();
$hed2 = array();
$hedJ = 1;

foreach($this->data->set->display as $k => $row){
	$this->data->set->display[$k]['width'] = round($row['width'] / 2);
}
foreach($this->data->set->display as $row){
	$width = $width + ($row['width'] + 3);
	
	$id = $row['id'];
	$vWdth[$id]->width 	= $row['width'];
	$vWdth[$id]->dec 	= $row['dec'];
	
	$row2 = $row;
	$idH = $row['id'];
	if(!empty($row['induk'])){
		$idH = $row['induk'];
		$hedJ = 2;
	}else{
		$row2['nama'] = '';
	}
	if(empty($hed1[$idH])){
		$hed1[$idH] = $row;
	}
	$hed1[$idH]['widthH'] = $hed1[$idH]['widthH'] + ($row['width']);
	$hed1[$idH]['child'][] = $row;
	
	$hed2[] = $row2;
}

$rumus = new RumusReport;

$atrHit = array();
foreach($this->data->set->hitung as $r){
	$atrHit[$r] = $r;
}

$header = '
		<div class="_header">
			'.$login['pt'].'
		</div>
		<div class="_header _title">
			'.$this->data->set->modul.'
		</div>
		<div class="_header _filter">
			'.$this->data->filter.'
		</div>
	';
$footer = '<div class="_footer">Page {PAGENO}</div>';

$html = '';
$html.= '<table class="_table" cellspacing="0" cellpadding="0">';
$html.= '<thead>';

				if(count($hed1) > 0){
				$html.= '<tr>';
					foreach($hed1 as $inNm => $row){
						$clsHd = '_tableReportHead';
						$align = 'left';
						if($row['type'] == 'numeric'){
							$align = 'right';
						}
						$plusHed = 0;
						$rowspan = 1;
						$colspan = '';
						if(count($row['child']) > 1){
							$plusHed 	= (count($row['child']) * 4) - 2;
							$align 		= 'center';
							$colspan	= 'colspan="'.count($row['child']).'"';
						}else{
							$rowspan 	= 2;
							if($hedJ > 1){
								$clsHd = '_tableReportHead2';
							}
						}
						$row['widthH'] = $plusHed + $row['widthH'];
						
						$style = "width: ".$row['widthH']."px; align: $align";
						$html.= '<th rowspan="'.$rowspan.'" class="'.$clsHd.'" style="'.$style.'" '.$colspan.'>'.strtoupper($inNm).'</th>';
					}
				$html.= '</tr>';
				}
				if($hedJ > 1){
				$html.= '<tr>';
					foreach($hed2 as $row){
						$align = 'left';
						if($row['type'] == 'numeric'){
							$align = 'right';
						}
					
						$style = "width: ".$row['width']."px; align: $align";
						if(!empty($row['nama'])){
							$html.= '<th class="_tableReportHead" style="'.$style.'">'.$row['nama'].'</th>';
						}
					} 
				$html.= '</tr>';
				}
$html.= '</thead>';

$html.= '<tbody>';

				$no = 0;
				$grup = '';
				$vHit = array();
				$grbB = $this->data->set->groupBy;
				$repo = new Report;
				$nomWarna = 1;
				foreach($this->data->data as $n => $i){
					$no = $no + 1;
					if(count($this->data->set->hitung) > 0 or !empty($grbB)){
						if(!empty($grbB)){
							if($grup != $i->$grbB){
								if($n > 0){
									if(count($this->data->set->hitung) > 0){
										$html.= '<tr>';
										foreach($this->data->hit as $kHit => $rHit){ 
											if($rHit->cl > 0){
												$nm = $rHit->nm;
												$_idG = $rHit->nm;
												$pd = '';
												if(!empty($nm)){
													$nm = $vHit[$grup]->$nm;
													$nm = number_format($nm, (int)$vWdth[$_idG]->dec);
												}
												if($kHit == 0){
													$nm = 'Total';
													$pd = "; padding: 2px;";
												}
												$color = '';
												if($nomWarna % 2 == 0){
													$color = '_colour';
												}
												$nomWarna++;
												$html.= '<td colspan="'.$rHit->cl.'" class="_tableReportTdGrup '.$color.'">'.$nm.'</td>';
											}
										} 
										$html.= '</tr>';
									}
							
						}
						if(count($this->data->set->groupDis) > 0){
							$nmGrup = '';
							foreach($this->data->set->groupDis as $kGrup => $nGrup){
								if(!empty($nmGrup)){
									$nmGrup.= ', ';
								}
								if(!empty($kGrup)){
									$nmGrup.= $kGrup.' : '.$i->$nGrup;
								}else{
									$nmGrup.= $i->$nGrup;
								}
							}
						$color = '';
						if($nomWarna % 2 == 0){
							$color = '_colour';
						}
						$nomWarna++;
						$html.= '<tr>';
						$html.= '<td class="_tableReportTdGrup '.$color.'" colspan="'.count($this->data->set->display).'">'.$nmGrup.'</td>';
						$html.= '</tr>';
					}
					$grup = $i->$grbB;
					if(count($this->data->set->groupByFo) > 0 and $no > 1){
						$grupFotDis = $repo->setFoter2($this->data->set, $iNew);
						$color = '';
						if($nomWarna % 2 == 0){
							$color = '_colour';
						}
						$nomWarna++;
						$html.= '<tr>';
						$html.= '<td class="_tableReportTdGrup '.$color.'" colspan="'.count($this->data->set->display).'">'.$grupFotDis.'</td>';
						$html.= '</tr>';
								}
							}
						}
					}
					if($no < 10000000000000000){
					$html.= '<tr>';
					$color = '';
					if($nomWarna % 2 == 0){
						$color = '_colour';
					}
					$nomWarna++;
					foreach($this->data->set->display as $row){
						$align = '';
						$id = $row['id']; 
						
						$val = $i->$id;
						if(!empty($row['set'])){
							$funct = 'set'.$row['set'];
							$val = $rumus->$funct($i);
						}
						
						if(!empty($atrHit[$id])){
							$idGrpH = '';
							if(!empty($grbB)){
								$idGrpH = $i->$grbB;
							}
							$vHit[$idGrpH]->$id = $vHit[$idGrpH]->$id + $val;
						}
						
						if($row['type'] == 'numeric'){
							$align = 'right';
							$val = number_format($val, (int)$row['dec']);
						}
						
						if($row['_number'] == true){
							$val = $no;
						}
								
						if($row['noduplicate'] == true){
							if($sama[$id] != $val){
								$sama[$id] = $val;
								
							}else{
								$val = '';
							}
						}
						$html.= '<td class="_tableReportTd '.$color.'">'.$val.'</td>';
					}
					$html.= '</tr>';
					$iNew = $i;
					}
				}

				if(count($this->data->set->hitung) > 0 or !empty($grbB)){
					if(!empty($grbB)){
							if(count($this->data->set->hitung) > 0){
								$html.= '<tr>';
								foreach($this->data->hit as $kHit => $rHit){ 
									if($rHit->cl > 0){
										$nm = $rHit->nm;
										$_idG = $rHit->nm;
										$pd = '';
										if(!empty($nm)){
											$nm = $vHit[$grup]->$nm;
											$nm = number_format($nm, (int)$vWdth[$_idG]->dec);
										}
										if($kHit == 0){
											$nm = 'Total';
											$pd = "; padding: 2px;";
										}
										$color = '';
										if($nomWarna % 2 == 0){
											$color = '_colour';
										}
										$nomWarna++;
										$html.= '<td colspan="'.$rHit->cl.'" class="_tableReportTdGrup '.$color.'">'.$nm.'</td>';
									}
								} 
								$html.= '</tr>';
							}
							if(count($this->data->set->groupDis) > 0){
								$nmGrup = '';
								foreach($this->data->set->groupDis as $kGrup => $nGrup){
									if(!empty($nmGrup)){
										$nmGrup.= ', ';
									}
									if(!empty($kGrup)){
										$nmGrup.= $kGrup.' : '.$i->$nGrup;
									}else{
										$nmGrup.= $i->$nGrup;
									}
								}
								$color = '';
								if($nomWarna % 2 == 0){
									$color = '_colour';
								}
								$nomWarna++;
								$html.= '<tr>';
								$html.= '<td class="_tableReportTdGrup '.$color.'" colspan="'.count($this->data->set->display).'">'.$nmGrup.'</td>';
								$html.= '</tr>';
							}
							if(count($this->data->set->groupByFo) > 0){
								$grupFotDis = $repo->setFoter2($this->data->set, $iNew);
								$color = '';
								if($nomWarna % 2 == 0){
									$color = '_colour';
								}
								$nomWarna++;
								$html.= '<tr>';
								$html.= '<td class="_tableReportTdGrup '.$color.'" colspan="'.count($this->data->set->display).'">'.$grupFotDis.'</td>';
								$html.= '</tr>';
						}
					}
				}
$html.= '</tbody>';
$html.= '</table>';

if(!empty($this->data->set->footer) and $this->data->set->footer != false and count($this->data->set->footer) > 0){
	foreach($this->data->set->footer as $row){
		$html.= '<pagebreak />';
		$html.= '<hr>';
		$html.= '<div class="_bodyLap">';
		$html.= '<div class="_headerFot">'.$row->header.'</div>';
		$html.= $row->body;
		$html.= $row->footer;
		$html.= "<div>";
	}
}

include(BASEPATH."assets/pdf/mpdf/mpdf.php");

$mpdf=new mPDF('c','A4','','',2,2,2,10); 

$stylesheet = file_get_contents(BASEPATH.'assets/pdf/mpdf/examples/apankReport.css');
$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text

$mpdf->margin_header = 2;
$mpdf->SetHTMLHeader($header);
$mpdf->margin_footer = 2;
$mpdf->SetHTMLFooter($footer);


$mpdf->SetDisplayMode('fullpage');
$mpdf->SetRightMargin(2);
$mpdf->SetLeftMargin(2);
$mpdf->SetTopMargin(20);
$mpdf->WriteHTML($html,2);
$mpdf->simpleTables = true;

$mpdf->Output('mpdf.pdf','I');
exit;

?>