<?php
$kode = new Kode;
?>
<script type="text/javascript">
$(document).ready(function(){
		_createDetailInDb = function(_tabDet){
			<?php
				$baris = 1;
				foreach($this->data->dataDetail as $r){
			?>
					_tabDet+= '<tr id="barisDet_<?php echo $baris ?>">';
					
					_mode = $('#_gridDetail').attr("mode");
					if(_mode !== 'VIEW'){
						_tabDet+= '<td class="_detTdItem"><div style="width: 30px;"><input type="button" class="delete _delDetGrid2020" col="<?php echo $baris ?>" id="_delDet_<?php echo $baris ?>"></div></td>';
					}
					<?php if($this->data->detail->autoNo == true){ ?>
						_tabDet+= '<td class="_detTdItem"><div style="width: 30px;"><input type="text" class="_detTd" style="width: 30px; text-align: center" name="no[<?php echo $baris ?>]" id="_no_<?php echo $baris ?>" autocomplete="off" value="<?php echo $baris ?>" readonly></div></td>';
					<?php 
					}
					$rows = 1;
					foreach($this->data->detail->attr as $keyD => $row){ 
						$idDet = $row['attr'];
						$type = "text";
						if(!empty($row['type'])){
							$type = $row['type'];
						}
						if(strtoupper($row['mode']) == "DATE"){
							if(!empty($r->$idDet)){
								$r->$idDet = date("d/m/Y", strtotime($r->$idDet));
							}
						}
						$dis = '';
						$cek = '';
						if(strtoupper($type) == strtoupper('hidden')){
							$dis = '; display: none';
						}else
						if(strtoupper($type) == strtoupper('checkbox')){
							if(!empty($r->$idDet)){
								$cek = 'checked';
							}
						}else{
							$tambah = $kode->setAttrDetail($row, $baris);
						}
						$r->$idDet = str_ireplace('"', "*_petik_*", $r->$idDet);
						
						$clsUp = '';
						$wrnUp = '';
						if($r->update == true){
							$clsUp = '_detUpdate';
							$wrnUp = '; background-color: #ef7d6b;';
						}
						if(strtoupper($type) != strtoupper('hidden')){
							$_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;
							}
							$marBtnLeft = $row['width'] - 1020;
					?>
						_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" style="margin-left: <?php echo $marBtnLeft ?>px">';
						<?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" style="margin-left: <?php echo $marBtnLeft ?>px">';
						<?php } ?>
						<?php
						if(strtoupper($type) == strtoupper('checkbox')){
						?>
							_tabDet+= '<td style="<?php echo $wrnUp ?>" class="_detTdItem"><div style="width: <?php echo $row['width'] ?>px; text-align: center"><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'] ?>  <?php echo $clsUp ?>" style="margin-top: -10px; <?php echo $wrnUp ?>" name="<?php echo $idDet ?>[<?php echo $baris ?>]" id="<?php echo $idDet.'_'.$baris ?>" autocomplete="off" '+" title='<?php echo $r->$idDet ?>'  "+'  disabled <?php echo $tambah ?> <?php echo $cek ?>></div></td>';
						<?php }else{ 
							if(preg_match("/decimal/i", $row['class'])) {
								$widthIn = $row['width'];
							}else{
								$widthIn = 1000;
							}
						?>
							_tabDet+= '<td style="<?php echo $wrnUp ?>" class="_detTdItem"><div style="width: <?php echo $row['width'] ?>px;"><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'] ?>  <?php echo $clsUp ?>" style="width: <?php echo $widthIn ?>px <?php echo $wrnUp ?>" 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+'</div></td>';
						<?php } ?>
					<?php }else{ ?>
						_tabDet+= '<input type="hidden" row="<?php echo $rows ?>" col="<?php echo $baris ?>" class="text _detTd _detTd_<?php echo $baris ?>_<?php echo $rows ?> <?php echo $row['class'] ?>  <?php echo $clsUp ?>" style="width:<?php echo ($row['width']) ?>px <?php echo $wrnUp ?>" 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 ?>>';
					<?php 
						}
						$rows++;
					} 
					?>
					_tabDet+= '<tr>';
			<?php 
					$baris++;
				}
			?>

			_tabDet = str_ireplace('*_petik_*', '"', _tabDet);
			
			return _tabDet;
		}
		_createDetailAwal = function(){
			_tabDet = '<tr id="barisDet_1">';
			_mode = $('#_gridDetail').attr("mode");
			if(_mode !== 'VIEW'){
				_tabDet+= '<td class="_detTdItem"><div style="width: 30px;"><input type="button" class="delete _delDetGrid2020" col="<?php echo $baris ?>" id="_delDet_<?php echo $baris ?>"></div></td>';
			}
			<?php if($this->data->detail->autoNo == true){ ?>
				_tabDet+= '<td class="_detTdItem"><div style="width: 30px;"><input type="text" class="_detTd" style="width: 30px; text-align: center" name="no[<?php echo $baris ?>]" id="_no_<?php echo $baris ?>" autocomplete="off" value="<?php echo $baris ?>" readonly></div></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;
					}
				
				if(strtoupper($type) == strtoupper('hidden')){
					$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="<?php echo $dis ?>" class="_detTdItem"><div style="width: <?php echo $row['width'] ?>px;"><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+'</div></td>';
			<?php }else{ ?>
				_tabDet+= '<input type="hidden" 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 ?>>';
			<?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();
			display('<?php echo $this->data->funct ?>', 1);
		}
		_makeLoadDetail();
		
		if($('#_prosesAction').val() === ''){
		
		}else{
			$('._detTd').removeAttr("disabled");
		}
})
</script>