<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" href="<?php echo $this->baseUrl ?>assets/pdf/jspdf/libs/pure-min.css">
    <link rel="stylesheet" href="<?php echo $this->baseUrl ?>assets/pdf/jspdf/libs/grids-responsive-min.css">

    <title><?php echo $this->data->set->modul ?></title>

    <style>
        * {
            box-sizing: border-box;
        }

        html, body {
            height: 100%;
            overflow: hidden;
        }

        .navbar {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            background: #e74c3c;
            border-bottom: 5px solid #c0392b;
            height: 50px;
            white-space: nowrap;
            overflow-x: auto;
            overflow-y: hidden;
            padding: 0 10px;
        }

        .navbar h1 {
            font-size: 20px;
            color: #fff;
        }

        .menu {
            padding: 0;
            list-style: none;
        }

        .menu li {
            vertical-align: top;
        }

        .menu li a {
            text-decoration: none;
            color: #fff;
            font-weight: bold;
            font-family: sans-serif;
            padding: 10px 0;
            line-height: 25px;
        }

        .menu li a:hover {
            font-style: italic;
        }

        #panel {
            background: #141f2b;
            padding: 10px;
            height: 100%;
        }

        #panel .editor {
            background: #fff;
        }

        #wrapper {
            overflow: hidden;
            height: 100%;
            background: rgba(193, 193, 193, 1);
        }

        #output {
            width: 100%;
            height: 100%;
            background: rgba(193, 193, 193, 1);
        }
    </style>

</head>
<body>

<div class="pure-g" style="width: 100%; height: 100%;">
    <div id="wrapper" class="<?php echo $this->baseUrl ?>assets/pdf/jspdf/pure-u-1 pure-u-md-4-5" style="width: 100%">
        <iframe id="output"></iframe>
    </div>
</div>

<script src="<?php echo $this->baseUrl ?>assets/pdf/jspdf/libs/jspdf.min.js"></script>

<script>if (window.define) delete window.define.amd;</script>

<script src="<?php echo $this->baseUrl ?>assets/pdf/jspdf/libs/faker.min.js"></script>
<script src="<?php echo $this->baseUrl ?>assets/pdf/jspdf/libs/jspdf.plugin.autotable.src.js"></script>

<?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;
}
?>
<script type="text/javascript">
var getWidth = function(){
	_res = [];
	<?php
	if(count($hed1) > 0){
		foreach($hed1 as $inNm => $row){
			$row['widthH'] = $plusHed + $row['widthH'];
		?>	
			// _res = ['<?php echo $inNm ?>': {columnWidth: '<?php echo $row['widthH'] ?>'}],
		<?php
		}
	}
	?>
	return _res;
}


var examples = {};

examples.auto = function () {
    var doc = new jsPDF('p', 'pt');
	
    var header = function (data) {
        doc.setTextColor(40);
        doc.setFontStyle('bold');
		
        doc.setFontSize(10);
		doc.text("<?php echo $login['pt'] ?>", 9, 20);
		
        doc.setFontSize(9);
		doc.text("<?php echo $this->data->set->modul ?>", 9, 30);
		
        doc.setFontSize(8);
		doc.text("<?php echo $this->data->filter ?>", 9, 40);
    };

    var totalPagesExp = "{total_pages_count_string}";
    var footer = function (data) {
        var str = "Page " + data.pageCount;

        if (typeof doc.putTotalPages === 'function') {
            str = str + " of " + totalPagesExp;
        }
        doc.setFontSize(8);
        doc.text(str, data.settings.margin.left, doc.internal.pageSize.height - 10);
    };
	
	
	_sizeFont = 7;
	_padding  = 1;
	<?php 
	if(!empty($this->data->set->fontSize)){ 
		$size = $this->data->set->fontSize - 5;
	?>
		_sizeFont = '<?php echo $size ?>';
	<?php } ?>
	
	<?php 
	if(!empty($this->data->set->padding)){ 
	?>
		_padding = '<?php echo $this->data->set->padding ?>';
	<?php } ?>
	
    var options = {
		themes: 'striped',
        beforePageContent: header,
        afterPageContent: footer,
        font: 'arial',
        fontSize: _sizeFont,
		cellPadding: _padding,
        margin: {top: 50, left: 10, bottom: 20, right: 10},
		<?php if($this->data->set->pdfResize == true){ ?>
			columnStyles: {
						<?php
						if(count($hed1) > 0){
							foreach($hed1 as $inNm => $row){
								$row['widthH'] = $plusHed + $row['widthH'];
							?>	
								<?php echo $inNm ?>: {columnWidth: <?php echo $row['widthH'] ?>},
							<?php
							}
						}
						?>
			},
		<?php } ?>
    };
    doc.autoTable(getColumns(), getData(), options);
	
    if (typeof doc.putTotalPages === 'function') {
        doc.putTotalPages(totalPagesExp);
    }
	
    return doc;
};


var getColumns = function () {
    return [
	<?php 
	foreach($this->data->set->display 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";
		
		$inNm = $row['nama'];
	?>	
		{title: "<?php echo strtoupper($inNm) ?>", dataKey: "<?php echo $row['id'] ?>"},
	<?php
	}
	?>
    ];
};

// Uses the faker.js library to get random data.
function getData(rowCount) {
    rowCount = rowCount || 4;
    var sentence = faker.lorem.words(12);
    var data = [];

	<?php
	$no = 0;
	$grup = '';
	$vHit = array();
	$grbB = $this->data->set->groupBy;
	$repo = new Report;
	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){
						?>
							// <tr>
								<?php 
								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;";
										}
						?>
							// <td colspan="<?php echo $rHit->cl ?>" class="_tableReportTd">
								// <?php echo $nm ?>">
							// </td>
						<?php 
							}
						} 
						?>
					// </tr>
				<?php
				}
				
			}
			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;
					}
				}
			?>
			// <tr>
				// <td class="_tableReportTdGrup" colspan="<?php echo count($this->data->set->display) ?>" >
					// <?php echo $nmGrup ?>
				// </td>
			// </tr>
	<?php
		}
		$grup = $i->$grbB;
		if(count($this->data->set->groupByFo) > 0 and $no > 1){
			$grupFotDis = $repo->setFoter2($this->data->set, $iNew);
	?>
		// <tr>
			// <td class="_tableReportTdGrup" colspan="<?php echo count($this->data->set->display) ?>">
				// <?php echo $grupFotDis ?>
			// </td>
		// </tr>
	<?php
					}
				}
			}
		}
		if($no < 10000000000000000){
	?>
		data.push({
		<?php
		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 = '';
				}
			}
			
		?>
			'<?php echo $row['id'] ?>': '<?php echo $val ?>',
		<?php 
		}
		?>
        });
	<?php
		$iNew = $i;
		}
	}
	?>

    return data;
}

function shuffleSentence(words) {
    words = words || faker.lorem.words(8);
    var str = faker.helpers.shuffle(words).join(' ').trim();
    return str.charAt(0).toUpperCase() + str.slice(1);
}
</script>

<script src="<?php echo $this->baseUrl ?>assets/pdf/jspdf/examples.js"></script>

<script>
    window.onhashchange = function () {
        update();
    };

    function update(shouldDownload) {
        var funcStr = window.location.hash.replace(/#/g, '') || 'auto';
        var doc = examples[funcStr]();

        doc.setProperties({
            title: 'Example: ' + funcStr,
            subject: 'A jspdf-autotable example pdf (' + funcStr + ')'
        });

        if (shouldDownload) {
            doc.save('table.pdf');
        } else {
            document.getElementById("output").src = doc.output('datauristring');
        }
    }

    update();
</script>

</body>
</html>