<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:'bom_id',title:'ID',width:80,hidden:true},
			{field:'bom_no',title:'BOM No.',width:50},
			{field:'Fbom_date',title:'BOM Date',width:80},
			{field:'fg_id',title:'FG Code',width:80},
			{field:'PartNo',title:'Size',width:80},
			{field:'NmBarang',title:'FG Name',width:80},
			{field:'Sat',title:'UOM',width:50},
			{field:'bom_type',title:'Type',width:50},
			
		]],
		url: '<?=site_url($this->modul)?>/dataHdr',
		onSelect:function(index,row){
			//console.log(row);
			setDgDetUrl(row.bom_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:80},
			{field:'PartNo',title:'Size',width:80},
			{field:'NmBarang',title:'Item Description',width:80},			
			{field:'Sat',title:'UOM',width:40},
			{field:'Fqty',title:'Qty.',width:100,align:'right'}
		]]
	});
}

function setDgHdrDet(){	
	$('#dg_hdrdet').datagrid({  	
		title:"Header",
		toolbar:"#toolbar_hdrdet",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:true,
		columns:[[  
			{field:'bom_id',title:'ID',width:80,hidden:true},
			{field:'bom_no',title:'BOM No.',width:50},
			{field:'Fbom_date',title:'BOM Date',width:50},
			{field:'mat_id',title:'FG Code',width:100},
			{field:'NmBarang0',title:'FG Name',width:150},			
			{field:'Sat0',title:'FG UOM',width:40},
			{field:'child_no',title:'Child No',width:80,hidden:true},
			{field:'mat_id',title:'Item Code',width:100},
			{field:'NmBarang',title:'Item Description',width:150},			
			{field:'Sat',title:'UOM',width:40},
			{field:'Fqty',title:'Qty.',width:100,align:'right'}
		]],
		url: '<?=site_url($this->modul)?>/dataHdrDet',
		onSelect:function(index,row){
			//console.log(row);
			setDgDetUrl(row.bom_id);
		}
	});
}

function setDgDetUrl(id){	
	$('#bom_id').val(id);
	$('#toolbar_det').show();
	$('#dg_det').datagrid({
		url: '<?=site_url($this->modul)?>/dataDet',
		queryParams: {
			q: id
		}
	});
}

function cari(q){
	$('#dg_hdr').datagrid({
		queryParams:{q:q}		
	});
}

function cari2(q){
	$('#dg_hdrdet').datagrid({
		queryParams:{q:q}		
	});
}

var url;
function tambah(){
	$('#dlg').dialog('open').dialog('setTitle','Tambah Header');
	$('#fm').form('clear');
	
	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
				});
			} 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.bom_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
						});
					} 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 tambah2(){
	$('#dlg2').dialog('open').dialog('setTitle','Tambah Detail');
	$('#fm2').form('clear');
	url = '<?=site_url($this->modul)?>/getformDet/add';
}

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.bom_id = $('#bom_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 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)?>/delDet',{id:row.bom_id,child_no:row.child_no},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 insert_det0(row){
	$('#NmBarang0').textbox('setValue',row.NmBarang);
	$('#Sat0').textbox('setValue',row.Sat);
}

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.bom_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>	