Ext.ns('Rebul.Order');

Rebul.Order.List = function(config){
    this.myPageSize = 25;
	this.config = config || {};
	this.config.statusID = this.config.statusID || 0; 
   
    var selectBoxModel = new Ext.grid.CheckboxSelectionModel();
    selectBoxModel.on('selectionchange', function(sm){
		if (this.config.statusID == 9) {
			if (sm.getSelections().length > 0) {
				Ext.getCmp('item-delete-button').enable();
				if (sm.getSelections().length == 1) {
					Ext.getCmp('item-edit-button').enable();
				}
				else {
					Ext.getCmp('item-edit-button').disable();
				}
			}
			else {
				Ext.getCmp('item-delete-button').disable();
				Ext.getCmp('item-edit-button').disable();
			}
		}
    }, this);
        
    this.dataStore = new Rebul.Store({
        url: '/jsonGateway.cfm?statusID=' + this.config.statusID,
        fields: [{
            name: 'INTID',
            type: 'int'
        }, {
            name: 'TXTREFERENCE',
            type: 'string'
        }, {
            name: 'DTADDED',
            type: 'date',
            dateFormat: 'Y-m-d'
        }, , {
            name: 'DTREQUESTED',
            type: 'date',
            dateFormat: 'Y-m-d'
        }, {
            name: 'TXTSTATUSNAME',
            type: 'string'
        }, {
            name: 'INTORDERQUANTITY',
            type: 'int'
        }, {
            name: 'FLTORDERPRICE',
            type: 'float'
        }]
    });
	
    this.dataStore.load({
    	params:{
    		start:0,
    		limit:Rebul.ROWLIMIT
    	}
    });
	    
    Rebul.Order.List.superclass.constructor.call(this, {
        width: 800,
		autoHeight:true,
        id: 'order-grid',
		title: '&nbsp;',
        ds: this.dataStore,
        enableColumnHide: false,
        stripeRows: true,
    	sm:selectBoxModel,
		tbar: new Ext.Toolbar({
			autoHeight:true
		}),
		/*
			bbar: new Ext.Toolbar({
            autoHeight: true,
            items: ['->', {
                xtype: 'tbtext',
                text: '<div id="bbarText" style="float:right;padding:5px;text-align:right;width:200px;color:#4379C3;font-weight:bold;">&nbsp;</div>'
            }]
        }),
        */
		bbar:new Ext.PagingToolbar({
			id:'pagingbar',
			displayInfo:true,
			pageSize: Rebul.ROWLIMIT,
			store: this.dataStore
		}),
        loadMask: {
            msg: 'loading...'
        },
        cm: new Ext.grid.ColumnModel([{
            id: 'id',
            header: 'Identify',
            dataIndex: 'INTID',
            hidden: true
        }, {
            header: 'Order #',
            dataIndex: 'INTID',
            width: 60,
            sortable: true,
            align: 'right'
        }, {
            header: 'Reference Number',
            dataIndex: 'TXTREFERENCE',
            width: 240,
            sortable: true
        }, {
            header: 'Date Created',
            dataIndex: 'DTADDED',
            width: 100,
            sortable: true,
            align: 'center',
            renderer: Ext.util.Format.dateRenderer('d/m/Y')
        }, {
            header: 'Delivery Date',
            dataIndex: 'DTREQUESTED',
            width: 100,
            sortable: true,
            align: 'center',
            renderer: Ext.util.Format.dateRenderer('d/m/Y')
        }, {
            header: 'Status',
            dataIndex: 'TXTSTATUSNAME',
            width: 100,
            sortable: true
        }, {
            header: 'Total Crates',
            dataIndex: 'INTORDERQUANTITY',
            width: 70,
            sortable: false,
            align: 'right'
        }, {
            header: 'Total Price (excl GST)',
            dataIndex: 'FLTORDERPRICE',
            width: 125,
            sortable: false,
            renderer: 'usMoney',
            align: 'right'
        }])
    });
    
    this.on('rowdblclick', function(gridPanel, rowIndex, e){
		if (this.config.statusID == 9) {
			var selectedId = this.dataStore.data.items[rowIndex].id;
			location.href = 'viewcart_saved.cfm?orderID=' + selectedId;
		}
		else{
			var selectedId = this.dataStore.data.items[rowIndex].id;
			location.href = 'orderdetail.cfm?orderID=' + selectedId;
		}
    });

	this.on('beforerender',function(){
		if(this.config.statusID==9){
			this.setTitle('Saved Quotes');
		}
		else{
			this.setTitle('Order History');
		}
	});
};

Ext.extend(Rebul.Order.List, Ext.grid.GridPanel, {
	afterRender: function(){
		Rebul.Order.List.superclass.afterRender.call(this);
		var grid = Ext.getCmp('order-grid');		
		var tb = grid.getTopToolbar();
        if (this.config.statusID==9) {
			var editButton = new Ext.Toolbar.Button({
		        id: 'item-edit-button',
		        text: 'Edit Quote',
		        tooltip: 'Edit the selected item',
		        iconCls: 'editItem',
				config: this.config,
		        disabled: true,
		        handler: function(){
			        var record = Ext.getCmp('order-grid').getSelectionModel().getSelected();
		            location.href = 'viewcart_saved.cfm?orderID=' + record.id;
			    }				
			});
			
			var deleteButton = new Ext.Toolbar.Button({
		        id: 'item-delete-button',
		        text: 'Delete Quote',
		        tooltip: 'Delete the selected item',
		        iconCls: 'remove',
				config: this.config,
		        disabled: true,
		        handler: function(){
		            var record = Ext.getCmp('order-grid').getSelectionModel().getSelected();
					location.href = 'removeItem.cfm?deleteID=' + record.id;
		        }
			});
			
			tb.addButton(editButton);
			tb.addSeparator();
			tb.addButton(deleteButton);
			tb.show();	
        }
		else{
			grid.hideTbar();
		}	
    }
});

