var windowReportReportTripPostLite = null;



   
//-----------------window-----------------------------------------------------   
function show_LogistMobile_ReportTripPostLite_Window()
{
	
	
	if(!windowReportReportTripPostLite){
	
	//storeReportReportTripPostLite.load();
	
	Ext.define('modelReportReportTripPostLite', {
    extend: 'Ext.data.Model',
	fields: ['result_1', 'result_2', 'result_3', 'result_4', 'result_5', 'result_6', 'result_7', 'result_8', 'result_9', 'result_10', 'result_11', 'result_12', 'result_13', 'result_14', 'result_15', 'result_17', 'result_18', 'result_19', 'result_20']
	});


	var storeReportReportTripPostLite =  new Ext.data.Store({		        
			    pageSize: 30,
				groupField: 'result_20',
		        model: 'modelReportReportTripPostLite',
		        proxy: 
		        {
		          type: 'ajax',
		          url: 'app/LogistMobile_ReportTripPostLite.php?load=1',
		          reader: 
		          {
		            type: 'json',
		            root: 'rows',
					totalProperty: 'total'
		          }
		        }
		});
	
	var pagingToolbarReportReportTripPostLite = Ext.create('Ext.PagingToolbar', {
        border: false,
        frame: false,
        store: storeReportReportTripPostLite,
        displayInfo: true
    });
	
	//var columns = [];
	
	var gridReportReportTripPostLite = new Ext.create('Ext.grid.Panel',{
		    selType: 'rowmodel',
		    columnLines: true,
		    store: storeReportReportTripPostLite,
		    loadMask: true,
			id: 'ReportTripPostLite_GridId',
		    border: false,
		    autoScroll: true,
			bbar:[pagingToolbarReportReportTripPostLite],
			features: [{
				ftype: 'groupingsummary',
				//groupHeaderTpl: '{[values.rows.trade_point]} {name}',//{[values.rows[0].trade_point]}
				groupHeaderTpl: 'Маршрут: {[values.rows[0].data["result_2"]]}, №: {[values.rows[0].data["result_20"]]}',
				hideGroupedHeader: false,
				//startCollapsed: true,
				enableGroupingMenu: false
			}],
			tbar:[
				/*{
							tooltip: 'Обновить данные',
							iconCls: 'refresh',
	    		    		handler: function(){
								
							}
				},'-',*/{
                            xtype: 'datefield',
                            format: 'Y-m-d',
                            altFormats: 'Y-m-d',
                            startDay: 1,
                            //fieldLabel: 'Период с',
                            fieldLabel: report_plan_fact.perid_from,
							labelAlign: 'left',
                            labelWidth: 60,
                            style:' margin-left:3px;',
                            id: 'ReportTripPostLite_startdt',
                            width: 160,
                            value: new Date(),
                            maxValue: new Date(),
                            listeners:{
                                change: function(newValue, oldValue, eOpts){

                                }
                            }
                },
                {
                             xtype: 'datefield',
                             format: 'Y-m-d',
                             altFormats: 'Y-m-d',
                             startDay: 1,
                             //fieldLabel: 'Период по',
							 fieldLabel: report_plan_fact.period_for,
                             labelAlign: 'left',
                             labelWidth: 60,
                             id: 'ReportTripPostLite_enddt',
                             style:'margin-left:3px;',
                             width: 160,
                             value: new Date()
                },
                {
					xtype: 'textfield',
					width: 50,
					id: 'ReportTripPostLite_disttime',
					value: 0,
					hidden: true,
					disabled: true,
					enableKeyEvents: true,
		        	   listeners:  {
		        		   keypress: function(field, e, eOpts) {
		        			   if (e.getKey() == e.ENTER) {
		        				   
		        			   }

		        		   }
		        	   }
				},
				{
                            //text: "Сформировать",
                            text: report_plan_fact.create_report,
							//iconCls: 'left-right',
							style:'margin-left:3px;',
                            //id:'filter_data',
                            handler:function(){
                               
							   
								
								storeReportReportTripPostLite.setProxy({
									type: 'ajax',
									url: 'app/LogistMobile_ReportTripPostLite.php?load=1&reportQues_startdt='+Ext.getCmp('ReportTripPostLite_startdt').getRawValue()+'&reportQues_enddt='+Ext.getCmp('ReportTripPostLite_enddt').getRawValue(),
									reader: 
									{
										type: 'json',
										root: 'rows',
										totalProperty: 'total'
									}
								});
								
								
								storeReportReportTripPostLite.load();
							
								
                            }
                },'-',
				{
	  						  text: all.export_xls,
	  						  tooltip: all.export_xls,
	  						  iconCls:'xls',
							  disabled: true,
	  						  handler:function() 
	  						  {
								var ArrData=new Array(); 
								var columnCount = modelReportReportTripPostLite.fields.length;
								//if(Ext.getCmp('ReportTripPostLite_GridId').store.getCount() > 0){
								if(Ext.getCmp('ReportTripPostLite_GridId').store.getTotalCount() > 0){
									ArrData = save_table_data(columnCount); 
									export_xls(ArrData,Ext.getCmp('ReportTripPostLite_startdt').getRawValue(),Ext.getCmp('ReportTripPostLite_enddt').getRawValue());
								}
	  						  }
	  			}
			],
			columns:[
			{
	        	xtype: 'rownumberer',
				width: 30
	        },
			{
	        	text: 'Дата',
				dataIndex: 'result_1',
				hideable: false,
				//hidden: true,			
	            width: 80,
	            sortable: false
	        },
			{
	        	text: 'День тижня',
				dataIndex: 'result_2',
				hideable: false,
				//hidden: true,
	            width: 80,
	            sortable: false,
	        },
			{
	        	text: 'Державний номер',
				dataIndex: 'result_3',
				hideable: false,
				//hidden: true,
	            width: 120,
	            sortable: false,
	        },
			{
	        	text: 'Марка авто',
				dataIndex: 'result_4',
				hideable: false,
				//hidden: true,
	            width: 120,
	            sortable: false,
	        },
			{
	        	text: 'Марка палива',
				dataIndex: 'result_5',
				hideable: false,
				//hidden: true,
	            width: 90,
	            sortable: false,
	        },
			{
	        	text: 'л/100 км',
				dataIndex: 'result_6',
				hideable: false,
				//hidden: true,
	            width: 70,
	            sortable: false,
	        },
			{
	        	text: 'Дирекція',
				dataIndex: 'result_7',
				hideable: false,
				//hidden: true,
	            width: 80,
	            sortable: false,
	        },
			{
	        	text: 'АТП',
				dataIndex: 'result_8',
				hideable: false,
				//hidden: true,
	            width: 70,
	            sortable: false,
	        },
			{
	        	text: '№ маршрута',
				dataIndex: 'result_9',
				hideable: false,
				//hidden: true,
	            width: 80,
	            sortable: false,
	        },
			{
	        	text: 'Назва маршрута',
				dataIndex: 'result_10',
				hideable: false,
				//hidden: true,
	            width: 100,
	            sortable: false,
	        },
			{
	        	text: 'Планова дата маршрута',
				dataIndex: 'result_11',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Плановий пробіг',
				dataIndex: 'result_12',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Фактичний пробіг',
				dataIndex: 'result_13',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Плановий пробіг по карті',
				dataIndex: 'result_14',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	            text: 'К-сть точок',
				dataIndex: 'result_15',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Фактична к-сть точок',
				dataIndex: 'result_16',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	            text: 'К-сть точок з відхиленням по часу',
				dataIndex: 'result_17',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Відхилення від маршруту',
				dataIndex: 'result_18',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Відхилення від маршруту, км',
				dataIndex: 'result_19',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Середня швидкість',
				dataIndex: 'result_20',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Фактическое время уезда',
				dataIndex: 'result_21',
				hideable: false,
				//hidden: true,
	            width: 140,
	            sortable: false,
	        },
			{
	        	text: 'Фактичні витрати палива, л',
				dataIndex: 'result_22',
				hideable: false,
				//hidden: true,
	            width: 150,
	            sortable: false,
	        },
			{
	        	text: 'Фактичні витрати палива, л/100км',
				dataIndex: 'result_23',
				hideable: false,
				//hidden: true,
	            width: 150,
	            sortable: false,
	        },
			{
	        	text: 'Плановий час на маршруті, год-хв',
				dataIndex: 'result_24',
				hideable: false,
				//hidden: true,
	            width: 150,
	            sortable: false,
	        },
			{
	        	text: 'Фактичнй час на маршруті, год-хв',
				dataIndex: 'result_25',
				hideable: false,
				//hidden: true,
	            width: 150,
	            sortable: false,
	        }
			],
			listeners: {
				beforeedit: function(editor, ctx, e, eOpts, record)
				{
           
				},
				edit: function(editor, ctx, e, eOpts, record) {
           
				},
				itemdblclick: function(cmp, records) {    		
    		
				}
			}
	});

	
	
        windowReportReportTripPostLite = new Ext.Window({
                        width: 850,
                        height: 400,
                        title: LogistMobile.ReportTripPostLite,
                        closeAction : 'destroy',
                        layout: 'fit',
                        resizable:true,
                        modal:false,
						constrainHeader: true,
						listeners: {
							destroy: function(){
							windowReportReportTripPostLite = null;
							}
						},
                        //bodyStyle:{'background-color': '#FFFFFF'},
                 items:[
					gridReportReportTripPostLite
				 ]
                     

        });    
         
    }
    windowReportReportTripPostLite.show();
	
	
function save_table_data(ColumsCount)
{
	var ColumnsArray = new Array(); 
	var i=0;
	console.log(modelReportReportTripPostLite.fields);
	
	ColumsCount = ColumsCount - 1;
	
	Ext.getCmp('ReportTripPostLite_GridId').store.each(function(rec){
				//var j=1;
				var k=0;
				Str='';
				while (k<ColumsCount)
				{
					console.log(modelReportReportTripPostLite.fields[k].name);
					//Str += rec.get('result_'+j)+'/n';
					Str += rec.get(''+modelReportReportTripPostLite.fields[k].name)+'/n';
					//j++;
					k++;
				}
				ColumnsArray[i]=Str;
				i++;
	});
	return ColumnsArray;
}

function export_xls(ArrayData,reportQues_startdt,reportQues_enddt)
{

	$.post("app/LogistMobile_ReportTripPostLite.php",{func:'export_xls','ColumnsArray[]':ArrayData,reportQues_startdt:reportQues_startdt,reportQues_enddt:reportQues_enddt}, function(data){
		window.open('app/LogistMobile_ReportTripPostLite.php?func=save_export_xls', '_blank');
	});                                   

}
	
}