<?php
$divDet = $this->getParameter['controller'].'Detail';
$kode = new Kode;
?>
<script type="text/javascript">
$(document).ready(function(){

	_div = '<?php echo $divDet ?>';
	_tabDet = '<table id="_gridDetail" class="_gridDetail" cellspacing="0" cellpadding="0" mode="<?php echo strtoupper($this->data->detail->mode) ?>">';
	_tabDet+= '<thead>';
	_tabDet+= '<tr>';
	<?php if($this->data->detail->autoNo == true){ ?>
		_tabDet+= '<th style="width: 30px"><input disabled type="text" class="_detHead" style="width: 30px" value="No"></th>';
	<?php } ?>

	<?php 
	$lastNoLeft = 0;
	foreach($this->data->detail->attr as $row){ 
		if(strtoupper($row['type']) != strtoupper('hidden')){
	?>
		_tabDet+= '<th style="width: <?php echo $row['width'] ?>px"><input disabled type="text" class="_detHead" style="width: <?php echo $row['width'] ?>px" value="<?php echo $row['label'] ?>"></th>';
	<?php 
		}
		$lastNoLeft++;
	} 
	$barisDet = 1;
	?>
	$('#_lastNoUrutLeft').val('<?php echo $lastNoLeft ?>');
	_tabDet+= '</tr>';
	_tabDet+= '</thead>';
	_tabDet+= '<tbody id="_detBody">';
	_tabDet+= '</tbody>';
	_tabDet+= '</table>';
	$('#'+_div).html(_tabDet);

		_createDetailInDb = function(_tabDet){
			<?php
				$baris = 1;
				foreach($this->data->data as $r){
				?>
					_tabDet+= '<tr id="barisDet_<?php echo $baris ?>">';
					<?php
					if($this->data->detail->autoNo == true){ 
						$rows = 0;
					?>
						_tabDet+= '<td style="width: 30px;" class="_detTdItem"><input type="text" class="_detTd" style="width: 30px" name="no[<?php echo $baris ?>]" autocomplete="off" value="<?php echo $baris ?>" id="_no_<?php echo $baris ?>" readonly></td>';
					<?php 
					}
					$rows = 1;
					foreach($this->data->detail->attr as $keyD => $row){ 
						$idDet = $row['attr'];
						$type = "text";
						if(!empty($row['type'])){
							$type = $row['type'];
						}
						$dis = '';
						if(strtoupper($type) == strtoupper('hidden')){
							$dis = '; display: none';
						}else{
							$tambah = $kode->setAttrDetail($row, $baris);
						}
						$r->$idDet = str_ireplace('"', "*_petik_*", $r->$idDet);
						if(strtoupper($row['mode']) == "DATE"){
							if(!empty($r->$idDet)){
								$r->$idDet = date("d/m/Y", strtotime($r->$idDet));
							}
						}
					?>
						_tabDet+= '<td style="width: <?php echo $row['width'] ?>px; <?php echo $dis ?>" class="_detTdItem"><input type="<?php echo $type ?>" row="<?php echo $rows ?>" col="<?php echo $baris ?>" class="text _detTd _detTd_<?php echo $baris ?>_<?php echo $rows ?> <?php echo $row['class'] ?>" style="width:<?php echo ($row['width']) ?>px" name="<?php echo $idDet ?>[<?php echo $baris ?>]" id="<?php echo $idDet.'_'.$baris ?>" autocomplete="off" '+" title='<?php echo $r->$idDet ?>' value='<?php echo $r->$idDet ?>' "+' dec="<?php echo (int)$row['dec'] ?>" disabled _mode="cursor" <?php echo $tambah ?>></td>';
					<?php 
						$rows++;
					} 
					?>
					_tabDet+= '<tr>';
			<?php 
					$baris++;
				}
			?>
			
			_tabDet = str_ireplace('*_petik_*', '"', _tabDet);
			
			return _tabDet;
		}
	
	_createDetailAwal = function(){
		_tabDet = '<tr id="barisDet_1">';

		<?php 
		$rows = 1;
		$baris = 1;
		if($this->data->detail->autoNo == true){ ?>
			_tabDet+= '<td style="width: 30px;" class="_detTdItem"><input type="text" class="_detTd" style="width: 30px" name="no[<?php echo $baris ?>]" id="_no_<?php echo $baris ?>" autocomplete="off" value="1" readonly></td>';
		<?php 
		}
		?>
		
		<?php 
		foreach($this->data->detail->attr as $keyD => $row){ 
			$idDet = $row['attr'];
			$type = "text";
			if(!empty($row['type'])){
				$type = $row['type'];
			}
			
			$clsEx = explode(" ", $row['class']);
			foreach($clsEx as $rCls){
				if(strtoupper($rCls) == strtoupper('decimal')){
					if(empty($r->$idDet)){
						$r->$idDet = 0;
					}
					$r->$idDet = number_format($r->$idDet, (int)$row['dec']);
				}
			}
			
			$dis = '';
			if(strtoupper($type) == strtoupper('hidden')){
				$dis = '; display: none';
			}else{
				$tambah = $kode->setAttrDetail($row, $baris);
			}
		?>
			_tabDet+= '<td style="width: <?php echo $row['width'] ?>px; <?php echo $dis ?>" class="_detTdItem"><input type="<?php echo $type ?>" row="<?php echo $rows ?>" col="<?php echo $baris ?>" class="text _detTd _detTd_<?php echo $baris ?>_<?php echo $rows ?> <?php echo $row['class'] ?>" style="width:<?php echo ($row['width']) ?>px" name="<?php echo $idDet ?>[<?php echo $baris ?>]" id="<?php echo $idDet.'_'.$baris ?>" autocomplete="off" value="<?php echo $r->$idDet ?>" title="<?php echo $r->$idDet ?>" disabled _mode="cursor" <?php echo $tambah ?>></td>';
		<?php 
			$rows++;
		} 
		?>
		
		_tabDet+= '<tr>';
		
		return _tabDet;
	}
	
	<?php if(count($this->data->data) > 0){ ?>
		$('#_lastBarisDet').val('<?php echo (count($this->data->data) + 1) ?>');
		_tabDet = _createDetailInDb('');
	<?php }else{ ?>
		$('#_lastBarisDet').val(2);
		_tabDet = _createDetailAwal('');
	<?php } ?>
	<?php 
	$lastNoLeft = 0;
	foreach($this->data->detail->attr as $row){ 
		$lastNoLeft++;
	} 
	?>
	$('#_lastNoUrutLeft').val('<?php echo $lastNoLeft ?>');
	
	$('#_detBody').html(_tabDet);

	_act = $('#_prosesAction').val();
	if(_act === 'simpan' || _act === 'update'){
		$(".text, .select, .textarea").removeAttr("disabled");
	}
	<?php if(!empty($this->data->funct)){ ?>
		funct('<?php echo $this->data->funct ?>');
	<?php } ?>
	setDec();
})
</script>