/**
 * @author mba
 */
Ext.namespace('Ext.sfweb');
Ext.sfweb.ProductSummaryPanel = function(conf){

	this.uuid = new Date().getTime();
	this.stripeRows = true;
	this.autoHeight = true;
	this.height = 'auto';
	this.viewConfig = {
        forceFit: true
    };
	this.enableHdMenu = false;
	this.enableColumnResize = false;
	this.enableColumnMove = false;
	this.enableDragDrop = false;
    this.sm = new Ext.grid.RowSelectionModel({
		singleSelect: true
	});
	
	this.data = conf.data;
	this.totalPrice = 0;

	this.addItems = function(store, records, index){
		var totalPrice = 0;
		Ext.each(store.data.items, function(el){
			totalPrice += el.data.price;
		})
		this.totalPrice = totalPrice.toFixed(2);
		var el = this.el;

		var view = this.getView();
		try{
			var thtml = '';
			var sum = Ext.select('div[@class*=x-grid3-gridsummary-row-inner]');
			sum.remove();
			thtml += '<div style="float:left;">Gesamt:</div>';
			thtml += '<div style="float:right; text-align:right;">'+formatEURCurrency(this.totalPrice)+'</div>';
			this.view.summaryWrap = Ext.DomHelper.insertAfter(this.view.scroller, {
				id: 'sum'+this.uuid,
	        	tag: 'div',
	            cls: 'x-grid3-gridsummary-row-inner',
				html: thtml
	        }, true);
		}catch(e){}
	};

	this.store = new Ext.data.GroupingStore({
        groupField:'group',
		listeners:{
			add: this.addItems.createDelegate(this)
		}
    });

	this.columns = [
        {header: this.data.columnnames.identifier, width: 60, sortable: true, dataIndex: 'identifier'},
        {header: this.data.columnnames.group, width: 20, sortable: true, dataIndex: 'group', hidden:true},
		{header: this.data.columnnames.amount, width: 20, sortable: true, dataIndex: 'amount', align: 'right'},
		{header: this.data.columnnames.unitprice, width: 20, sortable: true, renderer: formatEURCurrency, dataIndex: 'unitprice', align:'right'},
        {header: this.data.columnnames.price, width: 20, sortable: true, renderer: formatEURCurrency, dataIndex: 'price', align:'right'}
    ];

	this.view = new Ext.grid.GroupingView({
        forceFit:true,
        groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
    });

	Ext.sfweb.ProductSummaryPanel.superclass.constructor.call(this, conf);
}
Ext.extend(Ext.sfweb.ProductSummaryPanel, Ext.grid.GridPanel)