<script type="text/javascript">   
function setDgHdr(){	
	$('#dg_hdr').datagrid({  	
		title:"Header",
		toolbar:"#toolbar_hdr",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:true,
		columns:[[  
			{field:'po_no',title:'PO No.',width:50},
			{field:'Fpo_date',title:'PO Date'},
			{field:'NmPrshn',title:'Supplier',width:80},
			{field:'currency',title:'Currency'},
			{field:'kurs',title:'Kurs'},
			{field:'Fdlv_date',title:'Dlv. Date'},
			{field:'notes',title:'NOTES.'},
		]],
		url: '<?=site_url($this->modul)?>/dataHdr',
		onSelect:function(index,row){
			//console.log(row);
			setDgDetUrl(row.po_id);
		}
	});
}
function setDgDet(){	
	$('#dg_det').datagrid({  	
		title:"Detail",
		toolbar:"#toolbar_det",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:true,
		columns:[[  
			{field:'child_no',title:'Child No',width:80,hidden:true},
			{field:'mat_id',title:'Item Code',width:70},
			// {field:'PartNo',title:'Size',width:100},
			// {field:'CF',title:'CF',width:50},
			{field:'NmBarang',title:'Item Description',width:80},			
			{field:'Sat',title:'UOM',width:40},
			{field:'Fqty',title:'Qty.',width:70,align:'right'},
			{field:'Fprice',title:'Price',width:80,align:'right'},
			{field:'amount',title:'Amount',width:80,align:'right'}
		]]
	});
}
function setDgHdrDet(){	
	$('#dg_hdrdet').datagrid({  	
		title:"Header",
		toolbar:"#toolbar_hdrdet",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:true,
		columns:[[  
			{field:'po_id',title:'ID',width:80,hidden:true},	
			{field:'po_no',title:'PO No.',width:50},
			{field:'Fpo_date',title:'PO Date',width:50},
			{field:'po_name',title:'PO Type',width:50},
			{field:'po_no',title:'PO No.',width:50},
			{field:'NmPrshn',title:'Shipper',width:80},			
			{field:'supl_do',title:'DO Shipper No',width:50},
			{field:'supl_inv',title:'Inv No.',width:50},
			{field:'currency',title:'Currency',width:50},
			{field:'kurs',title:'Kurs',width:50},
			{field:'UrJnsDok',title:'Jenis BC',width:50},
			{field:'NoDaf',title:'No. Dok. Pabean',width:50},
			{field:'FTgDaf',title:'Tgl. Dok. Pabean',width:50},
			{field:'child_no',title:'Child No',width:80,hidden:true},
			{field:'mat_id',title:'Item Code',width:100},
			{field:'NmBarang',title:'Item Description',width:200},			
			{field:'Sat',title:'UOM',width:40},
			{field:'Fqty',title:'Qty.',width:100,align:'right'},
			{field:'Fprice',title:'Price',width:100,align:'right'},
			{field:'amount',title:'Amount',width:100,align:'right'}
		]],
		url: '<?=site_url($this->modul)?>/dataHdrDet',
		onSelect:function(index,row){
			//console.log(row);
			setDgDetUrl(row.po_id);
		}
	});
}
function setDgDetUrl(id){	
	$('#po_id').val(id);
	$('#toolbar_det').show();
	$('#dg_det').datagrid({
		url: '<?=site_url($this->modul)?>/dataDet',
		queryParams: {
			po_id: id
		}
	});
}
function cari(q){
	$('#dg_hdr').datagrid({
		queryParams:{q:q}		
	});
}
function cari2(q){
	$('#dg_hdrdet').datagrid({
		queryParams:{q:q}		
	});
}
function cari_det(q){
	var row = $('#dg_hdr').datagrid('getSelected');
	if (row){
		$('#dg_det').datagrid({
			queryParams:{
				po_id: row.po_id,
				q: q
			}		
		});
	} else {
		$.messager.show({
			title: 'Info',
			msg: 'Data header belum dipilih'
		});	
	}
}
var url;
function tambah(){
	$('#dlg').dialog('open').dialog('setTitle','Tambah Header');
	$('#fm').form('clear');
	// $('#po_no').textbox('setValue', '<?= $rp ?>'); //input default po_no
	$('#po_date').datebox('setValue', '<?= date('d/m/Y')?>'); //input default po_date
	$('#dlv_date').datebox('setValue', '<?= date('d/m/Y') ?>'); //input default dlv_date
	
	url = '<?=site_url($this->modul)?>/getform/add';
}
function ubah(){
	var row = $('#dg_hdr').datagrid('getSelected');
	if (row){
		$('#dlg').dialog('open').dialog('setTitle','Ubah Header');
		$('#fm').form('load',row);
		url = '<?=site_url($this->modul)?>/getform/edit';
	}
}
function simpan(){
	$('#fm').form('submit',{
		url: url,
		onSubmit: function(){				
			return $(this).form('validate');
		},
		success: function(result){
			// alert(result);
			var result = eval('('+result+')');
			if (result.success){
				$('#dlg').dialog('close');		// close the dialog
				$('#dg_hdr').datagrid('reload');
				$.messager.show({
					title: 'Info',
					msg: result.msg
				});
				// var url = '<?= site_url($this->modul) ?>'; //redirect menggunakan time
				// 	setTimeout(function() {
				// 		location.replace(url);
				// }, 1000);
			} else {
				$.messager.show({
					title: 'Error',
					msg: result.msg
				});
			}
		}
	});
}
function validasi(strings){
	var i=0;
	var ch='';
	
	num=0;
	ucase=0;
	lcase=0;
	while (i <= strings.length){
		character = strings.charAt(i);
		if (!isNaN(character * 1)){
			num=1;
		}else{
			if (character == character.toUpperCase()) {
				ucase=1;
			}
			if (character == character.toLowerCase()){
				lcase=1;
			}
		}
		i++;
	}
	if (num==0 || ucase==0 || lcase==0){ 
		return false; 
	} else {
		return true;
	}
}
function hapus(){
	var row = $('#dg_hdr').datagrid('getSelected');
	if (row){
		$.messager.confirm('Confirm','Apakah anda yakin akan menghapus data ini?',function(r){
			if (r){
				$.post('<?=site_url($this->modul)?>/delPo',{id:row.po_id},function(result){
					var result = eval('('+result+')');
					if (result.success){
						$('#dg_hdr').datagrid('reload');
						$('#dg_det').datagrid('reload');
						$.messager.show({
							title: 'Info',
							msg: result.msg
						});
						// var url = '<?= site_url($this->modul) ?>'; //redirect menggunakan time
						// 	setTimeout(function() {
						// 		location.replace(url);
						// }, 1000);
					} else {
						$.messager.show({	// show error message
							title: 'Error',
							msg: result.msg
						});
					}
				});
			}
		});
	}
}
// function aksi(cmd){
// 	var opts = $('#dg').datagrid('options');
// 	q=$("#q").val();
// 	//alert(cmd);
// 	if (cmd=='print'){
// 		var win = window.open('<?=site_url($this->modul)?>/aksi/'+cmd+'?q='+q+'&page='+opts.pageNumber+'&rows='+opts.pageSize,'Popup','toolbar=no, location=no,status=no,menubar=no,scrollbars=yes,resizable=no');
// 		win.print();
// 	} else {
// 		openurl('<?=site_url($this->modul)?>/aksi/'+cmd+'?'+
// 		'q='+q+'&'+
// 		'page='+opts.pageNumber+'&'+
// 		'rows='+opts.pageSize);
// 	}
// }
//Detail
function barScan(){
	$('#dlg3').dialog('open').dialog('setTitle','Tambah Detail (Barcode Scanner)');
	$('#barcode').val('');
	$('#fifo').prop('checked', true);
	url = '<?=site_url($this->modul)?>/addBarScan';
}

function tambah2() {
		var rh = $('#dg_hdr').datagrid('getSelected');
		if (rh) {
			$('#dlg2').dialog('open').dialog('setTitle', 'Tambah Detail');
			$('#fm2').form('clear');
			url = '<?= site_url($this->modul) ?>/getformDet/add';
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data header harus dipilih terlebih dahulu'
			});
		}
	}

function ubah2(){
	var row = $('#dg_det').datagrid('getSelected');
	if (row){
		$('#dlg2').dialog('open').dialog('setTitle','Ubah Detail');
		$('#fm2').form('load',row);
		url = '<?=site_url($this->modul)?>/getformDet/edit';
	}
}
function simpan2(){
	$('#fm2').form('submit',{
		url: url,
		onSubmit: function(param){				
			param.po_id = $('#po_id').val();
			return $(this).form('validate');
		},
		success: function(result){
			//alert(result);
			var result = eval('('+result+')');
			if (result.success){
				$('#dlg2').dialog('close');		// close the dialog
				$('#dg_det').datagrid('reload');
				//$('#dg_hdr').datagrid('reload');
				$.messager.show({
					title: 'Info',
					msg: result.msg
				});
			} else {
				$.messager.show({
					title: 'Error',
					msg: result.msg
				});
			}
		}
	});
}

function hapus2(){
	var row = $('#dg_det').datagrid('getSelected');
	if (row){
		$.messager.confirm('Confirm','Apakah anda yakin akan menghapus data ini?',function(r){
			if (r){
				$.post('<?=site_url($this->modul)?>/delDet1',{id:row.id},function(result){
					var result = eval('('+result+')');
					if (result.success){
						$('#dg_det').datagrid('reload');
						$.messager.show({
							title: 'Info',
							msg: result.msg
						});
					} else {
						$.messager.show({	// show error message
							title: 'Error',
							msg: result.msg
						});
					}
				});
			}
		});
	}
}
function simpan3(){
	$('#fm3').form('submit',{
		url: url,
		onSubmit: function(param){				
			param.po_id = $('#po_id').val();
			return $(this).form('validate');
		},
		success: function(result){
			alert(result);
			/*var result = eval('('+result+')');
			if (result.success){
				$('#dlg2').dialog('close');		// close the dialog
				$('#dg_det').datagrid('reload');
				$.messager.show({
					title: 'Info',
					msg: result.msg
				});
			} else {
				$.messager.show({
					title: 'Error',
					msg: result.msg
				});
			}*/
		}
	});
}
function insert_det(row){
	$('#PartNo').textbox('setValue',row.PartNo);
	$('#CF').textbox('setValue',row.CF);
	$('#Sat').textbox('setValue',row.Sat);
	if ($('#aksi').val()=='t'){
		$('#price').numberbox('setValue',row.Harga);
	}
}
function printSlip(){
	var row = $('#dg_hdr').datagrid('getSelected');
	if (row){
		openurl("<?=site_url($this->modul);?>/slip/"+row.po_id);
	}
}
function aksi(cmd){
	var opts = $('#dg_hdrdet').datagrid('options');
	q=$("#q").val();
	
	if (cmd=='print'){
		var win = window.open('<?=site_url($this->modul)?>/aksi/cetak?q='+q+'&page='+opts.pageNumber+'&rows='+opts.pageSize,'Popup','toolbar=no, location=no,status=no,menubar=no,scrollbars=yes,resizable=no');
		win.print();
	} else if (cmd=='xls'){
		openurl('<?=site_url($this->modul);?>/aksi/excel?q='+q+'&page='+opts.pageNumber+'&rows='+opts.pageSize);		
	}
}
</script>	