var reportRemainsWindow = new Windows(SFA.ReportRemains,900,620);
FormConstructor = new Forms();
/*var CarsStop_result;
var data=new Array();
var car_name=new Array();
var driver=new Array();
var employer=new Array();
var route=new Array();
var time_start_route=new Array();
var time_route=new Array();
var mileage=new Array();
var time_not_work=new Array();
var time_holiday=new Array();
var time_start_stop=new Array();
var time_stop=new Array(); 
check = 0;*/

Ext.Loader.setPath('Ext.ux', '../js/ext-4.1-pr1/docs/output');
Ext.require([
	'Ext.selection.CheckboxModel'
]);
/*Ext.define('CarsStop_panel', {
    extend: 'Ext.data.Model',
    fields: [ {name: 'Name'},
	          {name: 'imei'}//,
			  //{name: 'check', type: 'bool'}
			]
});
      var Data_CarsStop_panel = new Ext.data.Store({
        model: 'CarsStop_panel',
           proxy: {
              type: 'ajax',
              url: 'scripts/Cars.php',
              reader: {
                  type: 'json',
                  root: 'rows'
              }
          }
   });
   Data_CarsStop_panel.load();*/
   
   
//-----------------window-----------------------------------------------------   
function show_SFA_ReportRemains_Window()
{
    Year = new Date().getFullYear();
    Month = new Date().getMonth();
    Day = new Date().getDate();
    Hours=new Date().getHours();
    Minutes=new Date().getMinutes();
    Seconds=new Date().getSeconds();
    Month=Month+1; 
    
    if (Month<10) Month='0'+Month;
    if (Day<10) Day='0'+Day;
    if (Hours<10) Hours='0'+Hours;
    if (Minutes<10) Minutes='0'+Minutes;
    if (Seconds<10) Seconds='0'+Seconds; 

    FromDate=Year+'-'+Month+'-'+Day;
        
    PrevDay=FromDate.replace(/-/g,"/");
    var converted = Date.parse(PrevDay);
    PrevDay=converted-24*60*60*1000;
    Start = new Date(PrevDay);
    
    dateFromReportRemains = FormConstructor.CreateDateFieldLabelTop('Дата с:','',Start);
    hourseFromReportRemains = FormConstructor.CreateNumberFieldLabelTop('Часы:',50,Hours,0,23);      
    minutsFromReportRemains = FormConstructor.CreateNumberFieldLabelTop('Мин.:',50,Minutes,0,60);      
    secondsFromReportRemains = FormConstructor.CreateNumberFieldLabelTop('Сек.:',50,Seconds,0,60);
    
    dateToReportRemains=FormConstructor.CreateDateFieldLabelTop('Дата по:');
    hourseToReportRemains=FormConstructor.CreateNumberFieldLabelTop('Часы:',50,Hours,0,23);      
    minutsToReportRemains=FormConstructor.CreateNumberFieldLabelTop('Мин.:',50,Minutes,0,60);      
    secondsToReportRemains=FormConstructor.CreateNumberFieldLabelTop('Сек.:',50,Seconds,0,60);
    
    CreateReportRemains = FormConstructor.CreateButtonTooltipText('Построить отчет','Кликните что бы построить отчет');
    CreateReportRemains.handler=function()
                        { 
                            BuildReportRemains();         
                        }

    dateTimeRemainsForm = Ext.create('Ext.form.Panel', 
    {                
        layout: 'fit',
        layout:{type: 'vbox',align: 'left'},
        items:[{
                    xtype: 'fieldcontainer',
                    layout: {type: 'hbox',align: 'bottom'},
                    defaults:{margin: "5 5"},
                    items: [dateFromReportRemains,hourseFromReportRemains,minutsFromReportRemains,secondsFromReportRemains]
               },
               {
                    xtype: 'fieldcontainer',
                    layout: {type: 'hbox',align: 'bottom'},
                    defaults:{margin: "5 5"},
                    items: [dateToReportRemains,hourseToReportRemains,minutsToReportRemains,secondsToReportRemains]
               },
               {
                    xtype: 'fieldcontainer',
                    layout: {type: 'hbox',align: 'bottom'},
                    defaults:{margin: "5 5"},
                    items: [CreateReportRemains]
               }]
    })   
    
    PanelInfoReportRemains=FormConstructor.CreatePanelRegionCollaps('Параметры','north',190);
    PanelInfoReportRemains.add(dateTimeRemainsForm);
    resultRemainsForm = Ext.create('Ext.form.Panel', 
    {                
        //layout: 'fit',
        autoScroll:true,
        //width:'100%',
        title:'Результат отчета',
        
    })   
    reportRemainsForm = Ext.create('Ext.form.Panel', 
    {                
        //layout: 'fit',
        //autoScroll:true,
        items:[PanelInfoReportRemains,resultRemainsForm]
    })   
    reportRemainsWindow.CreateWindowSympleDestroy(reportRemainsWindow,reportRemainsForm); 
    reportRemainsWindow.Window.show(); 
}

function BuildReportRemains()
{
    dateFrom=dateFromReportRemains.getRawValue()+' '+hourseFromReportRemains.getValue()+':'+minutsFromReportRemains.getValue()+':'+secondsFromReportRemains.getValue();
    dateTo=dateToReportRemains.getRawValue()+' '+hourseToReportRemains.getValue()+':'+minutsToReportRemains.getValue()+':'+secondsToReportRemains.getValue();
    reportRemainsWindow.Window.mask('Обработка...');
    $.post("app/SFA_ReportRemains.php",{func:'getResult',dateTo:dateTo,dateFrom:dateFrom},function(data)  
    {
          //alert(data['store'])  
          var ObjReport = eval("(" + data + ")");   
          var grid_field = eval("(" + ObjReport.tableRezult + ")");
          var model = eval("(" + ObjReport.model + ")");
          var store = eval("(" + ObjReport.store + ")"); 
          
           Ext.define('ReportRemains', 
           {
                extend: 'Ext.data.Model',
                fields: model
           });
           var dataReportRemains = Ext.create('Ext.data.Store', 
           {
                model: 'ReportRemains',
                data : store
           });
           
           resultReportRemains = new Ext.grid.GridPanel(
           {
            //    columnLines: true,
                layout: 'fit',
               /*width:700,
               heigth:300,*/
                //autoScroll:true,
                store:dataReportRemains,    
                autoScroll: true,
                dockedItems: [ 
                {
                    xtype: 'toolbar',
                    items: [{
                                    itemId: 'CarsStop_xls',
                                    text:'Экспорт в xls',
                                    tooltip:'Экспорт в xls',
                                    iconCls:'xls',
                                    handler:function() 
                                    {
                                        var ArrData = new Array();
                                        ArrData = GetDataRemains(ObjReport.countColumn); 
                                        ExportxlsRemains(ArrData);  
                                    }
                           }]
                 }],
                 columns:grid_field
    });
    
           reportRemainsForm.remove(resultRemainsForm);
           
           resultRemainsForm = Ext.create('Ext.form.Panel', 
           {                
             //layout: 'fit',
             //autoScroll:true,
             //width:'100%',
             title:'Результат отчета',
             items:[resultReportRemains]
        
           })   
           reportRemainsForm.add(resultRemainsForm); 
           reportRemainsWindow.Window.unmask('Обработка...');   
    })
}


function GetDataRemains(ColumsCount)
{
    var ColumnsArray=new Array();
    var i=0;
    resultReportRemains.store.each
    (
        function(rec)
        {
            j=1;
            Str='';
            while (j<=ColumsCount)
            {
                Str+=rec.get('result_'+j)+'/n';
                j++;  
            }
            ColumnsArray[i]=Str;
            i++;
        }
    )

    return  ColumnsArray; 
}

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

    
function alertObj(obj) 
{ 
    var str = ""; 
    for(k in obj) { 
        str += k+": "+ obj[k]+"\r\n"; 
    } 
    alert(str); 
}
