<script type="text/javascript">   
function editForm() {
	$('#ref_id').combogrid('readonly');
	$('#partner_id').combogrid('readonly');
	$('#currency').combogrid('readonly');
}
function addForm() {
	$('#ref_id').combogrid('readonly',false);
	$('#partner_id').combogrid('readonly',false);
	$('#currency').combogrid('readonly',false);
}
function setDgHdr(){	
	$('#dg_hdr').datagrid({  	
		title:"Header",
		toolbar:"#toolbar_hdr",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:true,
		columns:[[  
			{field:'id',title:'ID',width:80,hidden:true},
			{field:'no',title:'AR No.',width:50},
			{field:'Fdate',title:'AR Date',width:50},
			// {field:'NmPrshn',title:'Customer',width:80},			
			// {field:'matout_no',title:'DO No.',width:50},
			// {field:'currency',title:'Currency',width:50},
			// {field:'rate',title:'Rate',width:50},
			// {field:'ftot_amount',title:'Amount',width:50},
			{field:'facctnamed',title:'Account',width:50},
			{field:'description',title:'Description',width:50},
			{field:'created_journal',title:'Created Journal',width:50},
			{field:'tax',title:'Tax',width:30}			
		]],
		url: '<?=site_url($this->modul)?>/dataHdr',
		onSelect:function(index,row){
			//console.log(row);
			setDgDetUrl(row.id);
		}
	});
}
function setDgDet(){	
	$('#dg_det').datagrid({  	
		title:"Detail",
		toolbar:"#toolbar_det",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:true,
		columns: [
				[{
						field: 'matout_id',
						title: 'ID',
						width: 80,
						hidden: true
					},
					{
						field: 'id_det',
						title: 'ID',
						width: 110
					},
					{
						field: 'matout_no',
						title: 'Outgoing No.',
						width: 110
					},
					{
						field: 'Fmatout_date',
						title: 'Outgoing Date',
						width: 80
					},
					{
						field: 'ref_no',
						title: 'SO No.',
						width: 80
					},
					{
						field: 'partial',
						title: 'Partial',
						width: 40
					},
					{
						field: 'matout_name',
						title: 'Outgoing Type',
						width: 50
					},
					{
						field: 'NmPrshn',
						title: 'Receiver',
						width: 80
					},
					{
						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: 'NoAju',
						title: 'No. Pengajuan',
						width: 50
					},
					{
						field: 'FTgAju',
						title: 'Tgl. Pengajuan',
						width: 50
					},
					{
						field: 'tot_amount',
						title: 'Total Amount',
						width: 80
					},
					{
						field: 'created_journal',
						title: 'Created Journal',
						width: 40
					},
					{
						field: 'transfer_ceisa',
						title: 'Transfer Ceisa',
						width: 40
					}
				]
			],
	});
}
function setDgHdrDet(){	
	$('#dg_hdrdet').datagrid({  	
		title:"Header",
		toolbar:"#toolbar_hdrdet",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:true,
		columns:[[  
			{field:'id',title:'ID',width:80,hidden:true},	
			{field:'no',title:'AR No.',width:50},
			{field:'Fdate',title:'AR Date',width:50},
			{field:'matout_no',title:'DO No.',width:50},
			{field:'NmPrshn',title:'Customer',width:80},			
			{field:'currency',title:'Currency',width:50},
			{field:'rate',title:'Rate',width:50},
			{field:'item_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:'Famount',title:'Amount',width:100,align:'right'}
		]],
		url: '<?=site_url($this->modul)?>/dataHdrDet',
		onSelect:function(index,row){
			//console.log(row);
			// setDgDetUrl(row.id);
		}
	});
}
function setDgDetUrl(id){	
	$('#id').val(id);
	$('#toolbar_det').show();
	$('#dg_det').datagrid({
		url: '<?=site_url($this->modul)?>/dataDet',
		queryParams: {
			q: id
		}
	});
}
function setItemUrl(do_id = 'empty'){
	$('#item_id').combogrid({
		url: '<?=site_url('general/gridBarangDO')?>/'+do_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: {
					matout_id: row.matout_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');
	// $('#no').textbox('setValue', '<?= $ra ?>');
	$('#date').datebox('setValue', '<?= date('d/m/Y')?>');
	
	url = '<?=site_url($this->modul)?>/getform/add';
}
function ubah(){
	var row = $('#dg_hdr').datagrid('getSelected');
	if (row){
		url = '<?=site_url($this->modul)?>/getform/edit';
		// alert('t'+url);
		$('#dlg').dialog('open').dialog('setTitle','Ubah Header');
		$('#fm').form('load',row);
	}
}
function simpan(){
	// alert('ttt'+url);
	$('#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)?>/del',{id:row.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');
		$('#arap_id').val(rh.id);
		setItemUrl(rh.ref_id);
		url = '<?=site_url($this->modul)?>/getformDet/add';
	} else {
		$.messager.show({
			title: 'Info',
			msg: 'Data header harus dipilih terlebih dahulu'
		});
	}
}
function ubah2(){
	var rh = $('#dg_hdr').datagrid('getSelected');
	if (rh){
		var row = $('#dg_det').datagrid('getSelected');
		if (row){
			setItemUrl(rh.ref_id);
			$('#dlg2').dialog('open').dialog('setTitle','Ubah Detail');
			$('#fm2').form('load',row);
			$('#arap_id').val(rh.id);
			url = '<?=site_url($this->modul)?>/getformDet/edit';
		}
	} else {
		$.messager.show({
			title: 'Info',
			msg: 'Data header harus dipilih terlebih dahulu'
		});
	}
	
}
function simpan2(){
	$('#fm2').form('submit',{
		url: url,
		onSubmit: function(param){				
			param.arap_id = $('#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_hdr').datagrid('reload');
				$('#dg_det').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_det},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 createJournal(){
	var row = $('#dg_hdr').datagrid('getSelected');
	if (row){
		$.messager.confirm('Confirm','Apakah anda yakin akan membuat journal untuk data ini?',function(r){
			if (r){
				$.post('<?=site_url($this->modul)?>/create_journal',{id:row.id, created_journal:row.created_journal},function(result){
					var result = eval('('+result+')');
					if (result.success){
						$('#dg_hdr').datagrid('reload');
						$.messager.show({
							title: 'Info',
							msg: result.msg
						});
					} else {
						$.messager.show({	// show error message
							title: 'Error',
							msg: result.msg
						});
					}
				});
			}
		});
	}
}
function insert_det(row){
	$('#NmBarang').textbox('setValue',row.NmBarang);
	$('#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.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();?>aksi/excel?q='+q+'&page='+opts.pageNumber+'&rows='+opts.pageSize);		
	}
}
</script>	