<script type="text/javascript">
	
	var url;
	
function editForm() {
	$('#po_id').combogrid('readonly');
	$('#supplier').combogrid('readonly');
	$('#currency').combogrid('readonly');
	$('#kurs').combogrid('readonly');
	$('#KdJnsDok').combogrid('readonly');
}

function addForm() {
	$('#po_id').combogrid('readonly',false);
	$('#supplier').combogrid('readonly',false);
	$('#currency').combogrid('readonly',false);
	$('#kurs').combogrid('readonly',false);
	$('#KdJnsDok').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: 'matin_id',
						title: 'ID',
						width: 80,
						hidden: true
					},
					// {field:'company',title:'Company',width:50},
					{
						field: 'matin_no',
						title: 'Incoming No.',
						width: 110
					},
					{
						field: 'Fmatin_date',
						title: 'Incoming Date',
						width: 80
					},
					{
						field: 'matin_name',
						title: 'Incoming Type',
						width: 80
					},
					{
						field: 'po_no',
						title: 'PO No.',
						width: 110
					},
					{
						field: 'partial',
						title: 'Partial',
						width: 40
					}
				]
			],
			url: '<?= site_url($this->modul) ?>/dataHdr',
			onSelect: function(index, row) {
				//console.log(row);
				setDgDetUrl(row.matin_id);
			}
		});
	}

	function setDgDet() {
		$('#dg_det').datagrid({
			title: "Detail",
			toolbar: "#toolbar_det",
			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:100},
				{field:'Fbom_date',title:'BOM Date',width:80},
				{field:'fg_id',title:'FG Code',width:80},
				{field:'NmBarang',title:'FG Name',width:150},
				{field:'Sat',title:'UOM',width:50}
			]]
		});
	}

	function setDgHdrDet() {
		$('#dg_hdrdet').datagrid({
			title: "Header",
			toolbar: "#toolbar_hdrdet",
			fit: true,
			fitColumns: true,
			singleSelect: true,
			rownumbers: true,
			pagination: true,
			columns: [
				[{
						field: 'matin_id',
						title: 'ID',
						width: 80,
						hidden: true
					},
					// {field:'company',title:'Company',width:50},		
					{
						field: 'matin_no',
						title: 'Incoming No.',
						width: 50
					},
					{
						field: 'Fmatin_date',
						title: 'Incoming Date',
						width: 50
					},
					{
						field: 'matin_name',
						title: 'Incoming 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: 'NoAju',
						title: 'No. Pengajuan',
						width: 50
					},
					{
						field: 'FTgAju',
						title: 'Tgl. Pengajuan',
						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.matin_id);
			}
		});
	}

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

	function setItemUrl(po_id = 'empty') {
		if (po_id != 'empty' && po_id != '' && po_id != '0') {
			$('#mat_id').combogrid({
				url: '<?= site_url('general/gridBarangPO') ?>/' + po_id,
			});

		}
	}

	function cari(q) {
		KdJnsDok = $('#KdJnsDok2').combobox('getValue');
		$('#dg_hdr').datagrid({
			queryParams: {
				q: q,
				KdJnsDok: KdJnsDok
			}
		});
	}

	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: {
					matin_id: row.matin_id,
					q: q
				}
			});
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data header belum dipilih'
			});
		}
	}


	function tambah() {


		$('#dlg').dialog('open').dialog('setTitle', 'Tambah Header');
		$('#fm').form('clear');
		$('#matin_no').textbox('setValue', '<?= $ri ?>');
		$('#matin_date').datebox('setValue', '<?= date('d/m/Y') ?>');
		$('#TgDaf').datebox('setValue', '<?= date('d/m/Y') ?>');
		$('#TgAju').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);
			$('#po_no').combobox('disable'); 
		}
	}

	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.matin_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');
			setItemUrl(rh.po_id);
			url = '<?= site_url($this->modul) ?>/getformDet/add';
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data header harus dipilih terlebih dahulu'
			});
		}
	}

	function excelCeisa() {
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row) {
			openurl("<?= site_url($this->modul); ?>/excelCeisa/" + row.matin_id);
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Harap pilih data yang akan di export ke excel'
			});
		}
	}



	function ubah2() {
		var rh = $('#dg_hdr').datagrid('getSelected');
		if (rh) {
			var row = $('#dg_det').datagrid('getSelected');
			if (row) {
				setItemUrl(rh.po_id);
				$('#dlg2').dialog('open').dialog('setTitle', 'Ubah Detail');
				$('#fm2').form('load', row);
				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.matin_id = $('#matin_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.matin_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 simpan3() {
		$('#fm3').form('submit', {
			url: url,
			onSubmit: function(param) {
				param.matin_id = $('#matin_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) {
		$('#NmBarang').textbox('setValue', row.NmBarang);
		$('#Sat').textbox('setValue', row.Sat);
		if ($('#aksi').val() == 't') {
			$('#price').numberbox('setValue', row.Harga);
		}
	}

	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 transferCeisa(){
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row){
			$.messager.confirm('Confirm','Apakah anda yakin akan transfer data ini ke Ceisa?',function(r){
				if (r){
					$.post('<?=site_url($this->modul)?>/transfer_ceisa',{id:row.id, transfer_ceisa:row.transfer_ceisa},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 printSlip() {
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row) {
			openurl("<?= site_url($this->modul); ?>/slip/" + row.matin_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>