/**
 * @author mba
 */
Ext.namespace('Ext.sfweb');
Ext.sfweb.ProductPanel = function(conf){
	
	this.layout = 'column';
	this.autoHeight= true;
	this.uuid = new Date().getTime();
	
	this.bodyBorder = false;
	this.data = conf.data;
	this.height = 500;

	this.totalinfo = {};

	this.buttonAlign = 'left';

	this.tabpanel = new Ext.TabPanel({
		activeTab: 0,
		border: false
	});
	
	this.productdetailpanel = new Ext.sfweb.ProductDetailPanel({
		data: this.data.product
	});

	this.productsummarypanel = new Ext.sfweb.ProductSummaryPanel({
		data: this.data.summary
	});

	this.items = [{
			columnWidth: .4,
			items:[
				this.productdetailpanel,
				this.productsummarypanel
			]
		}, {
			columnWidth: .6,
			items:[
				this.tabpanel
			]
		}
	];

	this.updateTotalInfo = function(){
		var tmpRecord = Ext.data.Record.create([
		    {name: 'identifier'}, {name: 'price'}, {name: 'group'}
		]);
		this.productsummarypanel.store.removeAll();

		var tnRecord = new tmpRecord({
			pid: this.data.product.pid,
		    identifier: this.data.product.title,
		    price: this.data.product.price,
			group: 'main',
			unitprice: this.data.product.price,
			amount: 1
		});
		this.productsummarypanel.store.add(tnRecord);

		this.totalinfo = {
			main: {
				id: this.data.product.pid,
				identifier: this.data.product.title,
				unitprice: this.data.product.price,
				amount: 1
			},
			props: [],
			price: 0
		}

		Ext.each(this.tabpanel.items.items, function(el){
			var sp = el.getSelectedProducts();
			Ext.each(sp, function(iel){
				var tnRecord = new tmpRecord({
					pid: iel.pid,
				    identifier: iel.identifier,
				    price: iel.price*iel.amount,
					group: el.title,
					unitprice: iel.price,
					amount: iel.amount
				});
				this.productsummarypanel.store.add(tnRecord);
				this.totalinfo.props.push({
					id: iel.pid,
					identifier: iel.identifier,
					unitprice: iel.price,
					amount: 1
				})
			}, this);

		}, this);
		
		this.totalinfo.price = this.productsummarypanel.totalPrice;
	};

	this.getTotalInfo = function(){
//		return(this.productsummarypanel.store);
		return(this.totalinfo);
	};

	this.onPanelRendered = function(){
		var w = Ext.getCmp('pp_west_'+this.uuid);
		var c = Ext.getCmp('pp_center_'+this.uuid);
		var s = Ext.getCmp('pp_south_'+this.uuid);
		
		/* set tabpanel */
		Ext.each(this.data.productproperties.productcategories, function(el){
			var rpp = new Ext.sfweb.RelatedProductPanel({
				title: el.title,
				data: el.data,
				columnnames: el.columnnames
			})
			rpp.on('itemchanged', this.updateTotalInfo.createDelegate(this), this);
			this.tabpanel.add(rpp)
		}, this);		

		this.updateTotalInfo();
	};

	this.addToCart = function(){
		console.log('addToCart');
		this.updateTotalInfo();
		var pc = this.getTotalInfo();
		this.callback(pc);
	};

	this.buttons = [{ 
		text: this.btnText,
		iconCls: 'sfui-ico-app-cart_put',
		handler: this.addToCart.createDelegate(this)
	}];

	this.listeners = {
		'render': this.onPanelRendered.createDelegate(this)
	};


	Ext.sfweb.ProductPanel.superclass.constructor.call(this, conf);
}
Ext.extend(Ext.sfweb.ProductPanel, Ext.Panel);