<div style="height:25px;">
	No.BL<span style="padding-left:10px" /><input type="text" id="id_nobl" />
	Tg.BL<span style="padding-left:10px" /><input type="text" id="tgl_nobl" readonly="readonly" />
	<button id="btnsearchbl">search</button>
</div>
<table>
	<td><div id="testblock"><table id="tblbc23bl_draft"></div></table> <div id="ptblbc23bl_draft"></div></td>
	<td align="center"><br /><br /><button id="btnselectinvbl" >Select Invoice >></button><br /><button id="btndeleteselectinvbl" >Delete Selected Invoice >></button>
	<br /><br /><button id="btnprosesinvbl" >Proses Add BL << >></button>
	<br /><br /><button id="btncancelinvbl" >Cancel</button>
	<br /><br /><button id="btnrecalculateinvbl" >Re Calculate</button>
	</td>
	<td><table id="listinvbl" class="scroll"></table>
    <div id="plistinvbl" class="scroll" style="text-align:center;"></div></td>

</table>

<script type="text/javascript">
    
    $(document).ready(function(){
		$("#tgl_nobl").datepicker();
		 
		$("#btnsearchbl").button().click(function(){
			$("#btnrecalculateinvbl").button({ disabled: true });
			$('#testblock').unblock(); 
			var form_data = { 'nobl':$('#id_nobl').val(), 'tgbl':$("#tgl_nobl").val() };
			$("#listinvbl").jqGrid('setGridParam', { postData: form_data });
			jQuery("#listinvbl").trigger('reloadGrid');
		});
		
		$("#btnrecalculateinvbl").button({ disabled: true });
		$("#btnrecalculateinvbl").click(function(){
			
			if($("#id_nobl").val() == '' && $("#tgl_nobl").val() == ''){
				alert("No dan Tanggal BL/AWB tidak boleh kosong");
				return false;
			};
			
			var rowdatagrid = jQuery("#listinvbl").jqGrid('getGridParam', 'records');
			if(rowdatagrid == 0){
				alert('pilih data terlebih dahulu');
				return false;
			}
			var gridData = jQuery("#listinvbl").getRowData();
			var postData = JSON.stringify(gridData);
			var form_data={
				nobl:$("#id_nobl").val(),
				tgbl:$("#tgl_nobl").val(),
				datadiff:postData
			};
			
			$("#dlg_recalculate_bl").dialog( "destroy" );
			$(".dlg_recalculate_bl_cl").remove();
			$(".dlg_recalculate_bl_cl").html('');
			$("body").append('<div id="dlg_recalculate_bl" title="Warning" class="dlg_recalculate_bl_cl"><p>Apakah anda yakin akan melakukan penambahan invoice (data table sebelah kanan)???</p><br />Mohon diperiksa kembali datanya dengan baik-baik..</div>');
			$("#dlg_recalculate_bl").dialog({
				height: 200,
				width: 250,
				modal: true,
				hide: 'Slide',
				autoOpen: 'false',
				buttons: {
					"CONFIRM": function() {
					$.ajax({
						url : "<?php echo base_url().'exim/ct_inv_draft/recalculatedatabl'; ?>",
						type : 'POST',
						dataType: 'json',
						data : form_data,
						success: function(msg){
							if (msg.sts == '00'){
								alert(msg.desc);
								$("#dlg_recalculate_bl").dialog("close");
								$('#dlg_recalculate_bl').dialog("destroy"); 
								$(".dlg_recalculate_bl_cl").remove(); 
								$('#tblbc23hdr_draft').trigger('reloadGrid');
								
							} else { 
								alert(msg.sts+msg.desc); 
								$("#dlg_recalculate_bl").dialog("close");
								$('#dlg_recalculate_bl').dialog("destroy"); 
								$(".dlg_recalculate_bl_cl").remove(); 
								$('#tblbc23hdr_draft').trigger('reloadGrid');
								
							}
						}
					});
				},
					"CANCEL": function() {
						$( "#dlg_recalculate_bl" ).dialog( "close" );

					}
				},
				close : function(){
					$('#dlg_recalculate_bl').dialog("destroy"); 
					$(".dlg_recalculate_bl_cl").remove(); 
					$("body").append('<div id="dlg_recalculate_bl" title="Warning" class="dlg_recalculate_bl_cl"><p>Apakah anda yakin akan melakukan penambahan invoice (data table sebelah kanan)???</p><br />Mohon diperiksa kembali datanya dengan baik-baik..</div>');     
					
				},
				open: function(){
					$("body").unblock();
					
				}
			});
			$("#btnrecalculateinvbl").button({ disabled: true });
			
		});
		
		
		$("#btncancelinvbl").button().click(function(){
			$("#btnrecalculateinvbl").button({ disabled: true });
			/* var selr; 
			selr = jQuery("#tblbc23bl_draft").jqGrid('getGridParam','selarrrow'); 
			if (selr.length == 0){
				alert('Pilih Data Dahulu');
				return false;
			
			} */
			
			var rowdatagrid = jQuery("#listinvbl").jqGrid('getGridParam', 'records');
			if(rowdatagrid == 0){
				alert('Pilih Data Dahulu Datanya, kemudian SELECT INVOICE (Pindahin Ke Kanan)');
				return false;
			}
			
			var gridData = jQuery("#listinvbl").getRowData();
			var postData = JSON.stringify(gridData);
			var form_data={
				nobl:$("#id_nobl").val(),
				tgbl:$("#tgl_nobl").val(),
				datadiff:postData
			};
			
			$.ajax({
				url : "<?php echo base_url().'exim/ct_inv_draft/canceldatabl'; ?>",
				type : 'POST',
				dataType: 'json',
				data : form_data,
				success: function(msg){
					if (msg.sts == '00'){
						alert(msg.desc);
						<!-- jQuery("#bc23_addbldata_dlg").dialog("close"); -->
						$("#tblbc23hdr_draft").trigger("reloadGrid");
						$("#tblbc23bl_draft").trigger("reloadGrid");
						$("#listinvbl").jqGrid("clearGridData", true);
					} else { alert(msg.sts+msg.desc);  }
				}
			});
			
		
		});
		
		$("#id_nobl").focusout(function(){
			var value = $("#id_nobl").val(); 
		//	value = value.replace(/\s/g, '');
			$("#id_nobl").val(value);
		});
		
		$("#btnprosesinvbl").button().click(function(){
			<!-- var rowdatagrid = $("#listinvbl").getGridParam("reccount"); -->
			$("#btnrecalculateinvbl").button({ disabled: true });
			if($("#id_nobl").val() == '' && $("#tgl_nobl").val() == ''){
				alert("No dan Tanggal BL/AWB tidak boleh kosong");
				return false;
			};
			
			var rowdatagrid = jQuery("#listinvbl").jqGrid('getGridParam', 'records');
			if(rowdatagrid == 0){
				alert('pilih data terlebih dahulu');
				return false;
			}
			var gridData = jQuery("#listinvbl").getRowData();
			var postData = JSON.stringify(gridData);
			var form_data={
				nobl:$("#id_nobl").val(),
				tgbl:$("#tgl_nobl").val(),
				datadiff:postData
			};
			$.ajax({
				url : "<?php echo base_url().'exim/ct_inv_draft/prosesdatabl'; ?>",
				type : 'POST',
				dataType: 'json',
				data : form_data,
				success: function(msg){
					if (msg.sts == '00'){
						alert(msg.desc);
						<!-- jQuery("#bc23_addbldata_dlg").dialog("close"); -->
						$("#tblbc23hdr_draft").trigger("reloadGrid");
						$("#tblbc23bl_draft").trigger("reloadGrid");
						
					} else if (msg.sts == '02'){
						alert(msg.desc);
						$("#btnrecalculateinvbl").button({ disabled: false });
					}else { alert(msg.sts+msg.desc);  }
				}
			});
			
		});
		
		$("#btnselectinvbl").button().click(function(){
			
			
			/* var rowIds = $('#listinvbl').jqGrid('getDataIDs');
			for(var i=0,len=rowIds.length;i<len;i++){
				var currRow = rowIds[i];
				$('#listinvbl').jqGrid('delRowData', currRow);
			}	

		
			var selr; 
			selr = jQuery("#tblbc23bl_draft").jqGrid('getGridParam','selarrrow'); 
			if(selr.length) {
			for(var i=0;i<selr.length;i++)
				{
					
					
					var myrow = jQuery('#tblbc23bl_draft').jqGrid('getRowData',selr[i]);
					var idbladd =  $.jgrid.randId();
					var id_noblx, tgl_noblx
					if (myrow.nobl != '-'){
						id_noblx = myrow.nobl;
						
					} else { id_noblx = $('#id_nobl').val();  }
					
					if (myrow.tgbl != ''){
						tgl_noblx = myrow.tgbl;
						
					} else { tgl_noblx = $('#tgl_nobl').val();  }
					
					jQuery("#listinvbl").jqGrid('addRowData', idbladd, {
						inv_hdr_id:myrow.inv_hdr_id,
						invno: myrow.r3,
						blawbno: id_noblx,
						tglblawb: tgl_noblx
					
					});
					//jQuery("#listinvbl").trigger('reloadGrid');
				}
			} */
			
			
			var id = jQuery("#tblbc23bl_draft").jqGrid('getGridParam','selrow');
			if (id) { 
				var myrow = jQuery("#tblbc23bl_draft").jqGrid('getRowData',id);
				
			} else {
				alert("Please Select Invoice Data");
				return false;
			};
			
			var idbladd =  $.jgrid.randId();
			var id_noblx, tgl_noblx
			if (myrow.nobl != '-'){
				id_noblx = myrow.nobl;
				
			} else { id_noblx = $('#id_nobl').val();  }
			
			if (myrow.tgbl != ''){
				tgl_noblx = myrow.tgbl;
				
			} else { tgl_noblx = $('#tgl_nobl').val();  }
			
			jQuery("#listinvbl").jqGrid('addRowData', idbladd, {
				inv_hdr_id:myrow.inv_hdr_id,
				invno: myrow.r3,
				blawbno: id_noblx,
				tglblawb: tgl_noblx
			
			});
			
			var rowku = myrow.inv_hdr_id;
			
			var ids = $('#listinvbl').jqGrid('getDataIDs');
			if(ids.length){
				var ii = 0;
				for (var j = 0; j < ids.length; j++) 
				{
					var rowId = ids[j];
					var rowData = jQuery('#listinvbl').jqGrid ('getRowData', rowId);
					
					if( rowData.inv_hdr_id==myrow.inv_hdr_id){
												
						//$('#listinvbl').jqGrid('delRowData', rowId);
						ii = ii + 1;
						
						if (ii == 2){
							alert('Data Sudah Ada');
							$('#listinvbl').jqGrid('delRowData', rowId);
						}
						
					}  else {
						
						//alert( myrow.inv_hdr_id  + ' != ' + rowData.inv_hdr_id );
						
					
					}
				}	
				
			} else {
				
			
			};
			
					
		});
		
		$("#btndeleteselectinvbl").button().click(function(){
			var id = jQuery("#listinvbl").jqGrid('getGridParam','selrow');
			if (id) { 
				var myrow = jQuery("#listinvbl").jqGrid('getRowData',id);
				$('#listinvbl').jqGrid('delRowData', id);
			} else {
				alert("Please Select Invoice Data yang telah dipilih, table sebelah kanan (SELECTED INVOICE)");
				return false;
			};
		
		
		});
		
		jQuery("#tblbc23bl_draft").jqGrid({
			url:'<?php echo base_url().'exim/ct_inv_draft/loaddata'; ?>',
			mtype : "post",
			postData:{'q':'1'},
			datatype: "json",
			colNames:['ID', 'No. Invoice', 'Date. Inv', 'Vessel','No BL/AWB','TGL BL/AWB','Supp. Code', 'Supp. Name', 'Container'],
			colModel:[
				{name:'inv_hdr_id',index:'inv_hdr_id', width:60, hidden:true},
				{name:'r3',index:'r3', width:79, hidden:false},
				{name:'r5',index:'r5', width:67, hidden:false},
				{name:'r20',index:'r20', width:75, hidden:false},
				{name:'nobl',index:'nobl', width:85, hidden:false},
				{name:'tgbl',index:'tgbl', width:75, hidden:false},
				{name:'r2',index:'r2', width:70, hidden:false},
				{name:'supplier_name',index:'supplier_name', width:180, hidden:false},
				{name:'r4',index:'r4', width:95, hidden:false},
				
				
			],
			rowNum:10,
			rowList:[10,50,100, 500, 1000],
			pager: '#ptblbc23bl_draft',
			sortname: 'inv_hdr_id',
			viewrecords: false,
			sortorder: "asc",
			width: 500,
			height:320,
			jsonReader: { repeatitems : false },
			caption:"INVOICE DATA",
			rownumbers: true,
			shrinkToFit: false,
			multiselect: false,
			afterInsertRow: function(){
				
				
			},
			gridComplete: function(){
				var rowIDs = jQuery("#tblbc23bl_draft").getDataIDs(); 
				var rowData = $("#tblbc23bl_draft").getRowData();
			
				for (var i = 0; i < rowData.length; i++) 
				{
				   var trElement = jQuery("#"+ rowIDs[i],jQuery('#tblbc23bl_draft'));
					
					if(rowData[i].nobl != '-'){  trElement.css({"color":"blue"}); }
					
				};
				if ($("#id_nobl").val() == "" && $("#tgl_nobl").val() == ""){
					$('#testblock').block({ 
						message: '<h2>Please Insert BL, Date and Click Search</h2>', 
						css: { border: '3px solid #a00' } 
					}); 
				}
			},
			
		}); 
			
                        
		jQuery("#tblbc23bl_draft").jqGrid('navGrid','#ptblbc23bl_draft',{del:false,add:false,edit:false},{},{},{},{multipleSearch:false});
		jQuery("#tblbc23bl_draft").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : true, defaultSearch: 'cn', ignoreCase: true});
		
		
		 jQuery("#listinvbl").jqGrid({
			url:'<?php echo base_url().'exim/ct_inv_draft/loaddatanonobl'; ?>',
			postData:{'nobl':$('#id_nobl').val(), 'tgbl':$("#tgl_nobl").val()},
			mtype : "post",
			datatype: "json",
			width: 300,
			height:320,
			colNames:['inv_hdr_id', 'Inv No', 'BL/AWB', 'Tgl BL/AWB'],
			colModel:[
				{name:'inv_hdr_id',index:'inv_hdr_id', width:60, sorttype:"int", hidden:true},
				{name:'invno',index:'invno', width:90},
				{name:'blawbno',index:'blawbno', width:90},
				{name:'tglblawb',index:'tglblawb', width:90},
			
			
			],
			pager: '#plistinvbl',
			rowNum:10,
			rowList:[10,20,30],
			sortname: 'inv_hdr_id',
			sortorder: 'desc',
			jsonReader: { repeatitems : false },
			viewrecords: true,
			multiselect: false,
			caption: "Selected Invoice"
		});
		

	});
		  	
</script>



	
	