<div style="height:25px;">
	No.BL<span style="padding-left:10px" /><input type="text" id="id_nobl_coo_bc23" />
	Tg.BL<span style="padding-left:10px" /><input type="text" id="tgl_nobl_coo_bc23" readonly="readonly" placeholder="yyyy-mm-dd" />
	<button id="btnsearchbl_coo_bc23">search</button>
</div>	
<table>
	<td><table id="tblbc23bl_draft_coo"></div></table> <div id="ptblbc23bl_draft_coo"></div></td>
	<td align="center"><br /><br /><button id="btnselectinvbl" >Select Invoice -></button><br /><button id="btndeleteselectinvbl_coo" >Delete Selected Invoice -></button>
	<br /><br /><button id="btnprosesinvbl_coo_bc23" > Proses Add BL </button>
	<br /><br /><button id="btncancelinvbl_coo_bc23" >Cancel Group BL</button>
	</td>
	<td><table id="listinvbl_bc23_coo" class="scroll"></table>
    <div id="plistinvbl_bc23_coo" class="scroll" style="text-align:center;"></div></td>
</table>
<script type="text/javascript">
    
    $(document).ready(function(){
    	$("#tgl_nobl_coo_bc23").datepicker({
			dateFormat : 'yy-mm-dd',
			onSelect : function(selectedDate) {
				$('#tgl_nobl_coo_bc23').datepicker('option', '', selectedDate);
			}
    	});
		
		$("#btnsearchbl_coo_bc23").button().click(function(){
			var form_data = { 'nobl':$('#id_nobl_coo_bc23').val(), 'tgbl':$("#tgl_nobl_coo_bc23").val() };
			$("#listinvbl_bc23_coo").jqGrid('setGridParam', { postData: form_data });
			jQuery("#listinvbl_bc23_coo").trigger('reloadGrid');
			
		});
		
		jQuery("#tblbc23bl_draft_coo").jqGrid({
			url:'<?php echo base_url().'exim/ct_upload_coo/tbl_coo_data'; ?>',
			mtype : "post",
			postData:{'SUPNEG':'LN'},
			datatype: "json",
			colNames:['ID','Vendor','No Invoice', 'Tgl Invoice','No BL/Srt Jln','Tgl BL/Srt Jln','Item Count','SUPNEG','status', 'tipe','No Aju','No BC','Tgl BC', 'create date', 'create by'],
			colModel:[
				{name:'id_coo_data',index:'id_coo_data', width:60, hidden:true},
				{name:'vendor',index:'vendor', width:50},
				{name:'no_invoice',index:'no_invoice', width:120},
				{name:'tgl_invoice',index:'tgl_invoice', width:90},
				{name:'nobl',index:'nobl', width:106},
				{name:'tgbl',index:'tgbl', width:106},
				{name:'item_count',index:'item_count', width:99},
				{name:'supplier_country',index:'supplier_country', width:50},
				{name:'status_inv',index:'status_inv', width:106},
				{name:'type_bc',index:'type_bc', width:106},
				{name:'car',index:'car', width:106},
				{name:'nobc',index:'nobc', width:106},
				{name:'tglbc',index:'tglbc', width:106},
				{name:'upload_date',index:'upload_date', width:166, hidden:false},
				{name:'upload_by',index:'upload_by', width:106, hidden:false},
			
				],
			rowNum:20,
			rowList:[20,50,100, 500, 1000],
			pager: '#ptblbc23bl_draft_coo',
			sortname: 'tgl_invoice',
			viewrecords: false,
			sortorder: "desc",
			width: 500,
			height:320,
			jsonReader: { repeatitems : false },
			caption:"INVOICE COO DATA",
			rownumbers: true,
			shrinkToFit: false,
			multiselect: false,
			afterInsertRow: function(){
				
				
			},
			gridComplete: function(){
				var rowIDs = jQuery("#tblbc23bl_draft_coo").getDataIDs(); 
				var rowData = $("#tblbc23bl_draft_coo").getRowData();
			
				for (var i = 0; i < rowData.length; i++) 
				{
				   var trElement = jQuery("#"+ rowIDs[i],jQuery('#tblbc23bl_draft_coo'));
					
					if($.trim(rowData[i].nobl) != ''){  trElement.css({"color":"blue"}); }
					
				};
				
			},
			
		}); 
		jQuery("#tblbc23bl_draft_coo").jqGrid('navGrid','#ptblbc23bl_draft_coo',{del:false,add:false,edit:false},{},{},{},{multipleSearch:false});
		jQuery("#tblbc23bl_draft_coo").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : true, defaultSearch: 'cn', ignoreCase: true});
		
		jQuery("#listinvbl_bc23_coo").jqGrid({
			url:'<?php echo base_url().'exim/ct_inv_draft/loaddatanonobl_coo'; ?>',
			postData:{'nobl':$('#id_nobl').val(), 'tgbl':$("#tgl_nobl").val()},
			mtype : "post",
			datatype: "json",
			width: 330,
			height:320,
			colNames:['id_coo_data', 'Inv No', 'BL/AWB', 'Tgl BL/AWB', 'vendor', 'Type BC'],
			colModel:[
				{name:'id_coo_data',index:'id_coo_data', width:60, sorttype:"int", hidden:true},
				{name:'invno',index:'invno', width:90},
				{name:'blawbno',index:'blawbno', width:90},
				{name:'tglblawb',index:'tglblawb', width:90},
				{name:'vendor',index:'vendor', width:90},
				{name:'type_bc',index:'type_bc', width:90}	
			
			],
			pager: '#plistinvbl_bc23_coo',
			rowNum:10,
			rowList:[10,20,30],
			sortname: 'id_coo_data',
			sortorder: 'desc',
			jsonReader: { repeatitems : false },
			viewrecords: true,
			multiselect: false,
			caption: "Selected Invoice"
		});
		
		$("#btnselectinvbl").button().click(function(){
			
			var id = jQuery("#tblbc23bl_draft_coo").jqGrid('getGridParam','selrow');
			if (id) { 
				var myrow = jQuery("#tblbc23bl_draft_coo").jqGrid('getRowData',id);
				
			} else {
				alert("Please Select Invoice Data");
				return false;
			};
			
			if ($.trim($('#id_nobl_coo_bc23').val()) == ''  || $.trim($('#tgl_nobl_coo_bc23').val()) == '' ){
				alert("ISIKAN DAHULU NO BL DAN TANGGAL BL");
				return false;
			}
			
			var idbladd =  $.jgrid.randId();
			var id_noblx, tgl_noblx
			if ($.trim(myrow.nobl) != ''){
				//id_noblx = myrow.nobl;
				var form_data = { 'nobl':myrow.nobl, 'tgbl':myrow.tgbl };
				$("#listinvbl_bc23_coo").jqGrid('setGridParam', { postData: form_data });
				jQuery("#listinvbl_bc23_coo").trigger('reloadGrid');
				
			} else { id_noblx = $('#id_nobl_coo_bc23').val();  }
			
			if (myrow.tgbl != ''){
				tgl_noblx = myrow.tgbl;
				
			} else { tgl_noblx = $('#tgl_nobl_coo_bc23').val();  }
			
			
			
			jQuery("#listinvbl_bc23_coo").jqGrid('addRowData', idbladd, {
				id_coo_data:myrow.id_coo_data,
				vendor:myrow.vendor,
				invno: myrow.no_invoice,
				blawbno: id_noblx,
				tglblawb: tgl_noblx,
				type_bc:'BC23' 
			});
			
			var rowku = myrow.id_coo_data;
			
			var ids = $('#listinvbl_bc23_coo').jqGrid('getDataIDs');
			if(ids.length){
				var ii = 0;
				for (var j = 0; j < ids.length; j++) 
				{
					var rowId = ids[j];
					var rowData = jQuery('#listinvbl_bc23_coo').jqGrid ('getRowData', rowId);
					
					if( rowData.id_coo_data==myrow.id_coo_data){
												
						//$('#listinvbl').jqGrid('delRowData', rowId);
						ii = ii + 1;
						
						if (ii == 2){
							alert('Data Sudah Ada');
							$('#listinvbl_bc23_coo').jqGrid('delRowData', rowId);
						}
						
					}  else {
						
						//alert( myrow.inv_hdr_id  + ' != ' + rowData.inv_hdr_id );
					
					}
					
				}	
				
			} else {
				
			
			};
			
					
		});
		
		$("#btndeleteselectinvbl_coo").button().click(function(){
			var id = jQuery("#listinvbl_bc23_coo").jqGrid('getGridParam','selrow');
			if (id) { 
				var myrow = jQuery("#listinvbl_bc23_coo").jqGrid('getRowData',id);
				$('#listinvbl_bc23_coo').jqGrid('delRowData', id);
			} else {
				alert("Please Select Invoice Data yang telah dipilih, table sebelah kanan (SELECTED INVOICE)");
				return false;
			};
			
		});
		
		$("#btnprosesinvbl_coo_bc23").button().click(function(){
			
			if($.trim($("#id_nobl_coo_bc23").val()) == '' && $.trim($("#tgl_nobl_coo_bc23").val()) == ''){
				alert("No dan Tanggal BL/AWB tidak boleh kosong");
				return false;
			};
			
			var rowdatagrid = jQuery("#listinvbl_bc23_coo").jqGrid('getGridParam', 'records');
			if(rowdatagrid == 0){
				alert('pilih data terlebih dahulu');
				return false;
			}
			var gridData = jQuery("#listinvbl_bc23_coo").getRowData();
			var postData = JSON.stringify(gridData);
			var form_data={
				nobl:$("#id_nobl_coo_bc23").val(),
				tgbl:$("#tgl_nobl_coo_bc23").val(),
				datadiff:postData
			};
			$.ajax({
				url : "<?php echo base_url().'exim/ct_inv_draft/prosesdatabl_coo'; ?>",
				type : 'POST',
				dataType: 'json',
				data : form_data,
				success: function(msg){
					if (msg.sts == '00'){
						alert(msg.desc);
						$("#tblbc23bl_draft_coo").trigger("reloadGrid");

						//var searchFiler = $("#id_nobl_coo_bc23").val(), f;
						//var searchtgbl=$("#tgl_nobl_coo_bc23").val();
						//f = {groupOp:"OR",rules:[]};
						//f.rules.push({field:"nobl",op:"cn",data:searchFiler});
						//f.rules.push({field:"tgbl",op:"cn",data:searchtgbl});
						//$("#tbl_data_coo_inv_bc23")[0].p.search = true;
						//$.extend($("#tbl_data_coo_inv_bc23")[0].p.postData,{filters:JSON.stringify(f)});
               			
               			$("#tbl_data_coo_inv_bc23").trigger("reloadGrid");
						
					   
					} else if (msg.sts == '02'){
						//alert(msg.desc);
						//$("#btnrecalculateinvbl").button({ disabled: false });
					}else { alert(msg.sts+msg.desc);  }
				}
			});
			
		});
		
		$("#btncancelinvbl_coo_bc23").button().click(function(){
			var rowdatagrid = jQuery("#listinvbl_bc23_coo").jqGrid('getGridParam', 'records');
			if(rowdatagrid == 0){
				alert('Pilih Data Dahulu Datanya, kemudian SELECT INVOICE (Pindahin Ke Kanan)');
				return false;
			}
			
			var gridData = jQuery("#listinvbl_bc23_coo").getRowData();
			var postData = JSON.stringify(gridData);
			var form_data={
				nobl:$("#id_nobl_coo_bc23").val(),
				tgbl:$("#tgl_nobl_coo_bc23").val(),
				datadiff:postData
			};
			$("#dlgbc23cancelbl_coo_draft").dialog( "destroy" );
			$(".dlgbc23cancelbl_coo_draftcl").remove();
			$(".dlgbc23cancelbl_coo_draftcl").html('');
			$("body").append('<div id="dlgbc23cancelbl_coo_draft" title="Warning" class="dlgbc23cancelbl_coo_draftcl"><p>Apakah anda akan Melakukan <b>CANCEL</b> SEMUA DATA BL DI TABLE SEBELAH KANAN INI???</div>');
			$("#dlgbc23cancelbl_coo_draft").dialog({
				height: 200,
				width: 250,
				modal: true,
				hide: 'Slide',
				autoOpen: 'false',
				buttons: {
					"CONFIRM": function() {
						
					$.ajax({
						url : "<?php echo base_url().'exim/ct_inv_draft/canceldatabl_coo'; ?>",
						type : 'POST',
						dataType: 'json',
						data : form_data,
						success: function(msg){
							if (msg.sts == '00'){
								alert(msg.desc);
								$("#dlgbc23cancelbl_coo_draft").dialog("close");
								$('#dlgbc23cancelbl_coo_draft').dialog("destroy"); 
								$(".dlgbc23cancelbl_coo_draftcl").remove(); 
								
								$("#tbl_data_coo_inv_bc23").trigger("reloadGrid");
								$("#tblbc23bl_draft_coo").trigger("reloadGrid");
								$("#listinvbl_bc23_coo").jqGrid("clearGridData", true);
							} else { 
								alert(msg.sts+msg.desc); 
								$("#dlgbc23cancelbl_coo_draft").dialog("close");
								$('#dlgbc23cancelbl_coo_draft').dialog("destroy"); 
								$(".dlgbc23cancelbl_coo_draftcl").remove();  
							}
						}
					});
						
						
					
				},
					"CANCEL": function() {
						$( "#dlgbc23cancelbl_coo_draft" ).dialog( "close" );
								
					}
				},
				close : function(){
					$('#dlgbc23cancelbl_coo_draft').dialog("destroy"); 
					$(".dlgbc23cancelbl_coo_draftcl").remove(); 
					$("body").append('<div id="dlgbc23cancelbl_coo_draft" title="Warning" class="dlgbc23cancelbl_coo_draftcl"><p>Apakah anda akan Memproses Data ini ke Modul BC23?!?</p></div>');     
					
				},
				open: function(){
					$("body").unblock();
					
				}
			});
			
			
				
			
		});
		

	});
		  	
</script>



	
	