<div id="container_ReportStock" style="height:100%; margin:0px; padding:0px;overflow:'hidden'; border: 0px solid #cd0a0a">
	<div class="pane-header ui-state-active" id="ReportStock_bar" >   
 
			<label style="margin-right:10px;">SPAREPART</label>
			<input id="KODE_SPAREPART" name="KODE_SPAREPART" type="text" style="width:75px;margin-bottom:10px; text-align: left"  value="" />	
		    <input id="NAMA_SPAREPART" name="NAMA_SPAREPART" type="text" style="width:150px;margin-bottom:10px; text-align: left"  value="" disabled /> 

 	
		 <button id="btnSearchReportSparepart" >SEARCH</button>
 
			 <button id="btnPrintReportSparepartXLXS" >PRINT XLSX</button>  
	</div>
	 <table id="tblReportStock"  /><div id="ptblReportStock"/> 
</div> 

<script> 
  $(document).ready(function(){ 
	$(window).bind('resize', function() {   
		 setTimeout(function(){
		 setGridHight($("#tblReportStock"),$("#container_ReportStock"),$("#ReportStock_bar")); 
  		},10); 
	}) ;
	
	$("#btnSearchReportSparepart").button();
 
	$("#btnPrintReportSparepartXLXS").button(); 

  
	 
	$("#KODE_SPAREPART").mcautocomplete({
			showHeader: true,
			 classes: {
						"ui-autocomplete": "highlight"
						},
			columns: [	{name: 'Id', width: '60px', valueField: 'rID'},
						{name: 'Code', width: '250px', valueField: 'r1'},
						{name: 'Name', width: '250px', valueField: 'r2'}],
			select: function(event, ui) {
				this.value = (ui.item ? ui.item.rID : ''); 
				$("#KODE_SPAREPART").val((ui.item ? ui.item.r1: '')); 
				$("#NAMA_SPAREPART").val((ui.item ? ui.item.r2: '')); 
				return false;
			},
			 change: function( event, ui ) { 
			 }, 
			minLength: 1,
			delay: 500,
			position: { my : "left top"
					  , at: "left bottom" },
			source: function(request, response) {
				$.ajax({
					url: '<?php echo base_url().'pasi_datapart/ct_datapart_load/find_sparepart'; ?>',
					dataType: 'json',
					data: {
						limit: 15,
						findtext: request.term
					},
					success: function(data) {
						var result;
						if (!data || data.length === 0 || !data.rows || data.rows.length === 0) {
							result = [{label: 'No match found.'}];
							this.value = '';
							$("#KODE_SPAREPART").val(''); 
						}
						else {
							result = data.rows;
						}
						response(result);
					}
				});
			}
		});		
      

	$("#btnSearchReportSparepart").click(function(){ 
		var	start_date 	=	$.datepicker.formatDate('yymmdd', new Date($("#txtStartReportStock").val())) ;
		var end_date	=	$.datepicker.formatDate('yymmdd', new Date($("#txtEndReportStock").val())) ; 
		var kode_sparepart = $("#KODE_SPAREPART").val();
		jQuery("#tblReportStock").jqGrid('setGridParam', { postData: {kode_sparepart : kode_sparepart,start_date : start_date,end_date : end_date  ,bc_type:'spartpart',source:'aw'} });
		$('#tblReportStock').trigger( 'reloadGrid' );
	});
	
	function reportPrint(format){
		var	start_date 	=	$.datepicker.formatDate('yymmdd', new Date($("#txtStartReportStock").val())) ;
		var end_date	=	$.datepicker.formatDate('yymmdd', new Date($("#txtEndReportStock").val())) ;
 		var mypostdata = $('#tblReportStock').getGridParam("postData").filters;
 		var tipe_bc='';
		if(mypostdata){ 
			pabean_tipe_bc = JSON.parse(mypostdata);  
			 if (pabean_tipe_bc['rules'].length>0){
					pabean_tipe_bc['rules'].forEach(function(entry) { 
						 if (entry.field=='r1');
							tipe_bc	=entry.data 
					}); 
			 }
		}
		
		var dialog = jQuery('<div />')
					 .html('<div class="ui-layout-content ui-widget-content-panel">\
									<p >Are u want to print report?</p>\
								   </div>\
									<div class="pane-footer ui-state-default"></div>\
									<form hidden id="frmPrintPabeanOut" method="POST" action="<?php echo base_url().'reports_sparepart/CT_report_sparepart_load/exp_excel_stock'; ?>"/>');  
		dialog.dialog({
				modal:true,
				width : 300,
				height : 150,
				title :'Laporan Stock Sparepart',
				buttons: { 
					"CLOSE": function(){
						dialog.dialog("close");  
					},
					"PRINT": function() { 
					
					$("#frmPrintPabeanOut").append($("<input>").attr({"value":start_date , "name":'start_date'})); 
					$("#frmPrintPabeanOut").append($("<input>").attr({"value":end_date , "name":'end_date'})); 
					$("#frmPrintPabeanOut").append($("<input>").attr({"value":format , "name":'format'})); 
					$("#frmPrintPabeanOut").append($("<input>").attr({"value":tipe_bc , "name":'tipe_bc'})); 
 					$("#frmPrintPabeanOut").append($("<input>").attr({"value":'sparepart' , "name":'bc_type'})); 
					$("#frmPrintPabeanOut").append($("<input>").attr({"value":'aw' , "name":'source'})); 
 					$("#frmPrintPabeanOut").submit();  
					
					dialog.dialog("close");  
					}
				},
				close : function(){
					dialog.dialog('destroy').remove(); 
				},
				open: function(){
					$("body").unblock(); 
				}
			}); 
	
	} 

	$("#btnPrintReportSparepartPDF").button().click(function(){	
		reportPrint('pdf');
 	})	
 	$("#btnPrintReportSparepartXLX").button().click(function(){	
		reportPrint('xls');
 	})	
 	$("#btnPrintReportSparepartXLXS").button().click(function(){	
		reportPrint('xlsx');
 	})	
	var arr = <?php echo json_encode($rID); ?>;
	var pabean_out_invoice = ((arr.indexOf("3111")==-1) ? '0' : '1');
	jQuery("#tblReportStock").jqGrid({
	   url:'<?php echo base_url().'reports_sparepart/ct_report_sparepart_data/loaddata_stock'; ?>',
		mtype : "post",
		postData:{'q':'1'},
		datatype: "json",
		colNames:['ID','KODE', 'NAMA', 'SATUAN','STOCK'],
		colModel:[
			{name:'rID',index:'rID', width:100, hidden:true},
			// {name:'r1',index:'r1', width:50},
			{name:'r6',index:'r6', width:100},  
			{name:'r2',index:'r2', width:120}, 
			{name:'r3',index:'r3', width:70, align :'center'},
			{name:'r4',index:'r4', width:100, align :'right'},
		   
		],
		rowNum:10,
		rowList:[10,20,30],
		pager: '#ptblReportStock',
		sortname: 'r5',
	 	sortorder: "asc",
		shrinkToFit:false,
		autowidth: false,           
		width: '',
		height: 100,
 		rownumbers:true, 
		jsonReader: { repeatitems : false }, 
		viewrecords : true, 
		gridview:true
	}); 
	$("#tblReportStock").jqGrid("setColProp", "rn", {hidedlg: false});
	
	jQuery("#tblReportStock").jqGrid('setGroupHeaders', 
		{ useColSpanStyle: true, groupHeaders:[
			{startColumnName: 'r14', numberOfColumns: 2, titleText: '<em><center>DOC</center></em>'}
		, {startColumnName: 'r8', numberOfColumns: 2, titleText: '<em><center>SPAREPART</center></em>'}
		, {startColumnName: 'r17', numberOfColumns: 2, titleText: '<em><center>MESIN</center></em>'}
		, {startColumnName: 'r15', numberOfColumns: 2, titleText: '<em><center>QTY</center></em>'}

		 ] });
 
	jQuery("#tblReportStock").jqGrid('navGrid','#ptblReportStock',{edit:false,add:false,del:false,view:false, search: false});  
	jQuery("#tblReportStock").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false, defaultSearch: 'cn', ignoreCase: false});  
	
 		 setTimeout(function(){
		 setGridHight($("#tblReportStock"),$("#container_ReportStock"),$("#ReportStock_bar")); 
  		},10); 
	 
	
	
});

</script> 

  