Ext.namespace('CC');
Ext.namespace('CC.b');
Ext.namespace('CC.b.mod');

CC.b.mod.stock = function() {
	return {
		config : {},
		init : function() {
			CC.f.mod.stock.init();
		}
	}
} ();


Ext.namespace('CC.f');
Ext.namespace('CC.f.mod');

CC.f.mod.stock = function() {
	return {
		ajaxUrl : CC_baseURL +'stock/',
		grids : {},
		formItems : [],
		windows: [],
		tree :{},

		init : function() {
			eval('this.' + CC.b.mod.stock.config.start_event + '();');
		},


		list : function() {
			var tbarConfig = [
				{ xtype : 'tbfill' },
				{
					xtype : 'label',
					text : CC.b.mod.stock.config.textLastDay + ':',
					style : 'padding:0 2px;'
				},
				{
					xtype : 'combo',
					id : 'LAST_DAY',
					dataIndex : 'LAST_DAY',
					displayField : 'text',
					valueField : 'value',
					mode : 'remote',
					triggerAction : 'all',
					store : new Ext.data.JsonStore(
						{
							autoLoad: true,
							url : this.ajaxUrl + 'extComboListFilterLastDay',
				  			fields : ['value', 'text'],
				  			root: 'result'
						}
					),
			   		autoLoad : true,
			   		typeAhead : true,
			   		forceSelection : true,
					allowBlank : false,
					width : 100
				},
				{
					xtype : 'tbseparator'
				},
				{
					xtype : 'label',
					text : CC.b.mod.stock.config.textWarehouse,
					style : 'padding:0 2px;'
				},
				{
					xtype : 'combo',
					id : 'WAREHAUSE_ID',
					dataIndex : 'WAREHAUSE_ID',
					displayField : 'text',
					valueField : 'value',
					mode : 'remote',
					triggerAction : 'all',
					store : new Ext.data.JsonStore(
						{
							autoLoad: true,
							url : this.ajaxUrl + 'extComboListFilterWarehouse',
				  			fields : ['value', 'text'],
				  			root: 'result'
						}
					),
			   		autoLoad : true,
			   		typeAhead : true,
			   		forceSelection : true,
					allowBlank : false
				}
			];

			CC.help.grid.initGrid(
				'stock', 
				{
					dataIndex : 'stock_view',
					ajaxUrl : 'extStock',
					primaryField : 'ID'
				}, 
				tbarConfig
			);
			
			
			this.tree = new Ext.TreeBar({
				moduleName: 'stock',
				gridName: 'Base',
				dataIndex: 'categoryTree',
				title: 'Kategóriák',
				showToolbar : false,
				urlGetData : CC_baseURL +'products/'+'extCategorytree',
				dblClickEvent : CC.f.mod.stock.refreshGridByTree,
				buttons : {
					create : CC.b.mod.stock.config.buttonCreateMenu,
					edit : CC.b.mod.stock.config.buttonEditMenu,
					del : CC.b.mod.stock.config.buttonDeleteMenu,
					changeParent : CC.b.mod.stock.config.buttonAddData
				},
				changeParent : function(){
					try{
						var category = this.obj.tree.selModel.getSelectedNode().attributes[this.obj.gridPkField];
						var prevId = this.obj.previousNode;
					}catch(e){
					}
					try{
						var datas = CC.f.mod.stock.grids.Base.grid.getSelectionModel().getSelections();
					}catch(e){
					}
					if (typeof category != 'undefined' && datas.length>0){
						var ids = {};
						ids['category'] = category;
						ids['stock'] = {};
						for ( i in datas){
							if (typeof datas[i] == 'object'){
								ids['stock'][i] = {ID : datas[i].id}
							}
						}
						
						var param ={
							singleInstance : true,
							url: CC.f.mod.stock.ajaxUrl+'extAssignData',
							params:{ 
								formData : Ext.encode(ids) 
							},
							success: function(result){
					
								CC.f.mod.stock.grids.Base.grid.getStore().reload({params: {categoryId: prevId}});
							}
						};	
						CC.help.ajax.doAjax(param);
					}
				},
				textConfirmDelete : CC.b.mod.stock.config.confirmCategoryDelete
			});
			var view = Ext.getCmp('CCWiew');
			var west = CC.main.layout.west;
			west.add(this.tree);
			west.setWidth(200);
			west.on('resize',
					function() {
						var h = view.getSize().height - 82;
						CC.f.mod.stock.tree.setTBHeight(h);
					}
				);
			CC.main.layout.center.add(this.grids.stock_view.grid);
			view.doLayout();
			
			CC.f.mod.stock.grids.stock_view.gridStore.baseParams = {
				lastDay : CC.b.mod.stock.config.lastSockMoveDay,
				warehouseId : CC.b.mod.stock.config.defaultWarehouse.ID
			};

			CC.main.layout.center.doLayout();
		//	this.grids.stock_view.grid.on('rowdblclick', this.displayViewWindowByDblClick);

			last_day_combo = CC.help.form.setComboDefaultValue('LAST_DAY',
					CC.b.mod.stock.config.lastSockMoveDay, CC.b.mod.stock.config.lastSockMoveDay);
			last_day_combo.on('select', CC.f.mod.stock.last_day_filter_change);

			warehouse_combo = CC.help.form.setComboDefaultValue('WAREHAUSE_ID',
					CC.b.mod.stock.config.defaultWarehouse.ID, CC.b.mod.stock.config.defaultWarehouse.NAME);
			warehouse_combo.on('select', CC.f.mod.stock.warehouse_filter_change);
		},

		refreshGridByTree : function(node){
			var id = node.attributes['ID'];
			document.getElementById('treeBarFilter').innerHTML = '<span class="ellipsis" style="float:right;font-weight:bold;width:84px;text-align:left;" title="' + node.attributes['text'] + '">' + node.attributes['text'] + '</span>Szűrés:';
			var limit = Ext.getCmp(CC.f.mod.stock.grids.stock_view.grid.bottomToolbar.items.keys[0]).getPageSize();
			CC.f.mod.stock.grids.stock_view.grid.getStore().baseParams = {categoryId : id };
			CC.f.mod.stock.grids.stock_view.grid.getStore().reload({params: { start:0,limit:limit}});
			
		},

		displayViewWindow : function(dbId) {
			if(CC.main.layout.isModuleLoaded('products')) {
				CC.b.mod.products.config.call_from = 'stock';
				CC.f.mod.products.displayEditorWindow(dbId);
			} else {
				CC.main.layout.loadModule('products', 'displayEditorWindow', dbId, 'stock');
			}
		},


		displayViewWindowByDblClick : function(gridObject, rowIndex) {
			var key = null;
			var gridObject = CC.f.mod.stock.grids.stock_view.grid;
			if(typeof gridObject.selModel.selections.keys == 'object') {
				key = gridObject.selModel.selections.keys[0];
			} else {
				key = gridObject.selModel.selections.keys;
			}
			CC.f.mod.stock.displayViewWindow(key);
		},


		last_day_filter_change : function() {
			CC.f.mod.stock.grids.stock_view.gridStore.baseParams.lastDay = this.getValue();
			CC.f.mod.stock.grids.stock_view.gridStore.reload();
		},


		warehouse_filter_change : function () {
			CC.f.mod.stock.grids.stock_view.gridStore.baseParams.warehouseId = this.getValue();
			CC.f.mod.stock.grids.stock_view.gridStore.reload();
		}

	}
} ();
