<?php 
$db = $this->data->db->getStructure();
$funct = new DisplayFunc;
$kode = new Kode;
$control = $kode->getControl();
$this->data->data->control = $control[$this->data->data->control];
 ?>
<script type="text/javascript">
$(document).ready(function(){
$('#_pk').val('<?php echo str_ireplace("/", "__--__", $this->data->data->pk) ?>');
$('#_nodb').val('<?php echo $this->data->data->_no ?>');
$('#_noUrut').val("<?php echo $this->data->data->_no ?>");
<?php 
if($this->data->settingPage == 'false'){ 
	$this->getParameter['controller'] = $this->data->controller;
}
?>
$('#<?php echo $this->getParameter['controller'] ?>Prev').attr("urut", "<?php echo (int)$this->data->pageIn->prev ?>");
$('#<?php echo $this->getParameter['controller'] ?>Next').attr("urut", "<?php echo (int)$this->data->pageIn->next ?>");
<?php

foreach($this->data->form as $row):
	if($row['input']['attr'] != false){
		$key = $row['input']['attr']['name'];
		$dis = $row['input']['attr']['display'];
		$type = $db->$key;
		
		if($key == 'chtime'){
			if(!empty($this->data->data->$key)){
				$this->data->data->$key = date("d/m/Y H:i", strtotime($this->data->data->$key));
			}
		}else
		if(strtoupper($row['input']['attr']['mode']) == strtoupper('date')){
			if(!empty($this->data->data->$key)){
				$this->data->data->$key = date("d/m/Y", strtotime($this->data->data->$key));
			}
		}else
		if(strtoupper($row['input']['attr']['mode']) == strtoupper('datetime')){
			if(!empty($this->data->data->$key)){
				$this->data->data->$key = date("d/m/Y H:i", strtotime($this->data->data->$key));
			}
		}else
		if($type['type'] == 'timestamp without time zone'){
			if(!empty($this->data->data->$key)){
				$this->data->data->$key = date("d/m/Y", strtotime($this->data->data->$key));
			}
		}else
		if(!empty($row['input']['attr']['DisplayFunc'])){
			$func = $row['input']['attr']['DisplayFunc'];
			$this->data->data->$key = $funct->$func($this->data->data->$key);
		}
?>
	$('#<?php echo $key ?>').val(unescape("<?php echo rawurlencode($this->data->data->$key) ?>"));
	
	_dis = '<?php echo $dis ?>';
	if(_dis !== ''){
		displayFunction(_dis, "<?php echo $key ?>");
	}
	
	
<?php 
	}
endforeach; 
?>
	
	<?php if(!empty($this->data->detail)){ ?>
		_createDetailInDb = function(_tabDet){
			<?php
				$baris = 1;
				$dataNotDup = array();
				foreach($this->data->dataDetail 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);
						}
						
						if($row['noduplicate'] == true){
							if(empty($dataNotDup) or $dataNotDup[$idDet] != $r->$idDet){
								$dataNotDup[$idDet] = $r->$idDet;
							}else{
								$r->$idDet = '';
							}
						}
						$_imgSel = false;
						if(!empty($row['select'])){
							$_imgSel = true;
						}
						
						$after = $row['after'];
						$after = explode("(", $after);
						$after = trim(strtolower($after[0]));
						$_imgLod = false;
						if($after == 'load'){
							$_imgLod = true;
						}
						
						if($row['mode'] == 'date'){
							if(!empty($r->$idDet)){
								$r->$idDet = date("d/m/Y", strtotime($r->$idDet));
							}
						}
					?>
						_btnSel = '';
						<?php if($_imgSel == true){ ?>
							_btnSel = '<input row="<?php echo $rows ?>" disabled col="<?php echo $baris ?>" idd="<?php echo $idDet.'_'.$baris ?>" class="_imgCariD _buttonSelectD" value="" type="button">';
						<?php } ?>
						<?php if($_imgLod == true){ ?>
							_btnSel = '<input row="<?php echo $rows ?>" disabled col="<?php echo $baris ?>" _idd="<?php echo $idDet.'_'.$baris ?>" _nno="<?php echo $idDet.'_'.$baris ?>" class="_imgCariD _buttonLoadD" value="" _ttb="&pkBrowse=&get_def=&get_def=undefined" type="button">';
						<?php } ?>
						_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 ?>>'+_btnSel+'</td>';
					<?php 
						$rows++;
					} 
					?>
					_tabDet+= '<tr>';
			<?php 
					$baris++;
				}
			?>
			return _tabDet;
		}
		_createDetailAwal = function(){
			_tabDet = '<tr id="barisDet_1">';

			<?php 
			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 
			}
			
			$rows = 1;
			$baris = 1;
			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);
				}

				$_imgSel = false;
				if(!empty($row['select'])){
					$_imgSel = true;
				}
				$after = $row['after'];
				$after = explode("(", $after);
				$after = trim(strtolower($after[0]));
				$_imgLod = false;
				if($after == 'load'){
					$_imgLod = true;
				}
			?>
				_btnSel = '';
				<?php if($_imgSel == true){ ?>
					_btnSel = '<input row="<?php echo $rows ?>" disabled col="<?php echo $baris ?>" idd="<?php echo $idDet.'_'.$baris ?>" class="_imgCariD _buttonSelectD" value="" type="button">';
				<?php } ?>
				<?php if($_imgLod == true){ ?>
					_btnSel = '<input row="<?php echo $rows ?>" disabled col="<?php echo $baris ?>" _idd="<?php echo $idDet.'_'.$baris ?>" _nno="<?php echo $idDet.'_'.$baris ?>" class="_imgCariD _buttonLoadD" value="" _ttb="&pkBrowse=&get_def=&get_def=undefined" type="button">';
				<?php } ?>
				_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 ?>>'+_btnSel+'</td>';
			<?php 
				$rows++;
			} 
			?>
			_tabDet+= '<tr>';
			return _tabDet;
		}
		_makeLoadDetail = function(){
			
			<?php if(count($this->data->dataDetail) > 0){ ?>
				$('#_lastBarisDet').val('<?php echo (count($this->data->dataDetail) + 1) ?>');
				_tabDet = _createDetailInDb('');
			<?php }else{ ?>
				$('#_lastBarisDet').val(2);
				_tabDet = _createDetailAwal('');
			<?php } ?>
			
			$('#_detBody').html(_tabDet);
			setDec();
		}
		_makeLoadDetail();
	<?php } ?>
	<?php if(!empty($this->data->funct)){ ?>
		funct('<?php echo $this->data->funct ?>');
	<?php } ?>
	setDec();
})
</script>