Ext.namespace('Ext.parafiada');
/**
 * @author    Tomasz Śmietanka
 * @copyright (c) 2009, OTN IT Advisory Services
 * @date      9. June 2009
 * @version   $Id$
 */
Ext.parafiada.ExtendedGrid = Ext.extend(Ext.grid.EditorGridPanel, {
    paging: false,
    initComponent: function(config){
        Ext.apply(this, config);
        
        if (this.paging) {
            Ext.apply(this, {
                bbar: new Ext.PagingToolbar({
                    pageSize: this.pageSize ? this.pageSize : 100,
                    search: true,
                    store: this.store,
                    displayInfo: true,
                    displayMsg: 'Wyniki od {0} do {1} z {2}',
                    emptyMsg: "Brak danych do wyświetlenia",
                    items: [{
                        iconCls: 'eg-print-icon',
                        handler: this.printCurrent,
                        scope: this,
                        tooltip: 'Drukuj bieżące dane'
                    }, {
                        iconCls: 'eg-printall-icon',
                        handler: this.printAll,
                        scope: this,
                        tooltip: 'Drukuj wszystkie dane'
                    } , "-", {
                        iconCls: 'eg-csv-icon',
                        handler: this.exportCSV,
                        scope: this,
                        tooltip: 'Eksptortuj bieżące dane'
                    }, {
                        iconCls: 'eg-csvall-icon',
                        handler: this.exportCSVAll,
                        scope: this,
                        tooltip: 'Eksportuj wszystkie dane'
                    }]
                })
            });
        }
        else {
            Ext.apply(this, {
                bbar: new Ext.Toolbar({
                    items: [{
                        iconCls: 'eg-print-icon',
                        handler: this.printCurrent,
                        scope: this,
                        tooltip: 'Drukuj bieżące dane'
                    }, {
                        iconCls: 'eg-printall-icon',
                        handler: this.printAll,
                        scope: this,
                        tooltip: 'Drukuj wszystkie dane'
                    }, "-", {
                        iconCls: 'eg-csv-icon',
                        handler: this.exportCSV,
                        scope: this,
                        tooltip: 'Eksptortuj bieżące dane'
                    }, {
                        iconCls: 'eg-csvall-icon',
                        handler: this.exportCSVAll,
                        scope: this,
                        tooltip: 'Eksportuj wszystkie dane'
                    }]
                })
            });
        }
        
        if (this.search) {
            if (!this.tbar) {
                Ext.apply(this, {
                    tbar: []
                });
            }
        }
        Ext.parafiada.ExtendedGrid.superclass.initComponent.call(this);
    },
    afterRender: function(){
    /*
        if (this.task) {
            this.getBottomToolbar().add('-');
            this.getBottomToolbar().add({
                iconCls: 'eg-createtask-icon',
                handler: this.createTask,
                scope: this,
                tooltip: 'Zleć zadanie z bieżącymi danymi'
            });
            this.getBottomToolbar().add({
                iconCls: 'eg-createtask-icon-all',
                handler: this.createTask,
                scope: this,
                tooltip: 'Zleć zadanie z wszystkimi danymi'
            });
            
        }*/
        if (this.search) {
            if (this.getTopToolbar() && this.getTopToolbar().getSize() > 0) {
                this.getTopToolbar().add('');
            }
            this.getTopToolbar().add({
                xtype: 'tbtext',
                text: 'Wyszukaj:',
                style: 'padding-left: 4px; padding-right: 4px; font-weight: bold;'
            });
            this.getTopToolbar().add({
                xtype: 'searchfield',
                store: this.store,
                params: this.params,
                width: 300
            });
        }
        Ext.parafiada.ExtendedGrid.superclass.afterRender.call(this);
    },
    printCurrent: function(){
    
        var pp = new Ext.parafiada.PrintPreview({
            width: 300,
            heigth: 300,
            url: this.store.url,
            params: this.store.lastOptions.params
        });
        pp.show();
    },
    printAll: function(){
        var newParams = Ext.ux.clone(this.store.lastOptions.params);
        newParams.start = 0;
        newParams.limit = 10000;
        var pp = new Ext.parafiada.PrintPreview({
            width: 300,
            heigth: 300,
            url: this.store.url,
            params: newParams
        });
        pp.show();
    },
    exportCSV: function(){
    	var newparms;
    	var newparms2;
		var params = Ext.urlEncode(this.store.lastOptions.params);
		newparms2 = params.replace(/=/gi,"/");
		newparms = newparms2.replace(/&/gi,"/");
		window.open(this.store.url + 'csv/'+ newparms,"_blank");                
    },
    exportCSVAll: function(){


		window.open(this.store.url + 'csv/limit/0/',"_blank");
    },
    
    createTask: function(){
    
    }
});


Ext.reg('extendedgrid', Ext.parafiada.ExtendedGrid);

