Ext.define('Ext.ux.call2action.SitePanel', {

	extend: 'Ext.panel.Panel',

	alias: 'widget.ip2Call2ActionSitePanel',

	layout: 'accordion',

	split: true,

	border: false,

	initComponent: function() {
		var me = this;

		//me.height = 344; // TODO

		if (!me.data.website) {
			me.data.website = {
				type: '',
				token: 0
			};
		}

		me.childExpanded = false;

		me.stackGrid = Ext.create('Ext.ux.call2action.EntityGrid', {
			wrapper: me,
			itemId: 'stackgrid',
			dmd: {
				dmd: 'webshop.dmd',
				entity: 'WSHPAGET00',
				ui: 'portfolio',
				columns: ['PLGPROP_TITLE', 'PLGPROP__CID__'],
				filter: '[{"property":"PLGPROP__CID__", "value":"13"}]',
				union: (me.data.website.type == 'stack') ? '[{"property":"PAGPAGTKN", "value":"' + me.data.website.token + '"}]' : ''
			},
			title: ip2.i18n({
				da: 'Stakke',
				en: 'Stacks'
			}),
			listeners: {
				selectionchange: function(sm, records) {
					if (records.length > 0) {
						me.data.website.type = 'stack';
						me.data.website.token = records[0].data.PAGPAGTKN;

						me.productGrid.fireEvent('deselectAll');
						me.pagesGrid.fireEvent('deselectAll');

						me.fireEvent('dataactive');
					}
					else {
						me.fireEvent('datadeactive');
					}
				}
			}
		});

		me.productGrid = Ext.create('Ext.ux.call2action.EntityGrid', {
			wrapper: me,
			itemId: 'productgrid',
			dmd: {
				dmd: 'webshop.dmd',
				entity: 'WSHITMET00',
				ui: 'portfolio',
				columns: ['ILGPROP_ITEMNUMBER', 'ILGPROP_TITLE'],
				union: (me.data.website.type == 'product') ? '[{"property":"ITMITMTKN", "value":"' + me.data.website.token + '"}]' : ''
			},
			title: ip2.i18n({
				da: 'Produkter',
				en: 'Products'
			}),
			listeners: {
				selectionchange: function(sm, records) {
					if (records.length > 0) {
						me.data.website.type = 'product';
						me.data.website.token = records[0].data.ITMITMTKN;

						me.stackGrid.fireEvent('deselectAll');
						me.pagesGrid.fireEvent('deselectAll');

						me.fireEvent('dataactive');
					}
					else {
						me.fireEvent('datadeactive');
					}
				}
			}
		});

		me.pagesGrid = Ext.create('Ext.ux.call2action.EntityGrid', {
			wrapper: me,
			itemId: 'pagesgrid',
			dmd: {
				dmd: 'webshop.dmd',
				entity: 'WSHPAGET00',
				ui: 'portfolio',
				columns: ['PLGPROP_TITLE', 'PLGPROP__CID__'],
				filter: '[{"property":"PLGPROP__CID__", "value":"11"}]',
				union: (me.data.website.type == 'page') ? '[{"property":"PAGPAGTKN", "value":"' + me.data.website.token + '"}]' : ''
			},
			title: ip2.i18n({
				da: 'Sider',
				en: 'Pages'
			}),
			listeners: {
				selectionchange: function(sm, records) {
					if (records.length > 0) {
						me.data.website.type = 'page';
						me.data.website.token = records[0].data.PAGPAGTKN;

						me.stackGrid.fireEvent('deselectAll');
						me.productGrid.fireEvent('deselectAll');

						me.fireEvent('dataactive');
					}
					else {
						me.fireEvent('datadeactive');
					}
				}
			}
		});

		me.items = [
		me.stackGrid,
		me.productGrid,
		me.pagesGrid
		];

		me.listeners = me.listeners || {};
		Ext.apply(me.listeners, {
			childviewready: function(panel) {
				if (!me.childExpanded) {
					var item;
					switch (me.data.website.type) {
						case 'stack':
							item = me.getComponent('stackgrid');
							break;
						case 'product':
							item = me.getComponent('productgrid');
							break;
						case 'page':
							item = me.getComponent('pagesgrid');
							break;
					}
					if (item) {
						var expand = new Ext.util.DelayedTask(function() {
							item.expand();
							me.childExpanded = true;
						});
						expand.delay(200);
					}
				}
			}
		});

		me.callParent();
	},

	/* ----------------------------------------------------------------------------------------------------
	    Validate and save form
	   ---------------------------------------------------------------------------------------------------- */
	doSave: function(args) {
		var me = this;

		if (Ext.isFunction(args.success)) {
			args.success.call()
		}
	}

});
