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

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


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

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

		firstDay : null,
		lastDay : null,
		warehouseId : null,
		productId : null,


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


		list : function() {
			var module = CC.f.mod.product_history;
			var moduleBack = CC.b.mod.product_history;
			var config = moduleBack.config;

			this.firstDay = config.firstDay;
			this.lastDay = config.lastDay;
			this.warehouseId = config.defaultWarehouse.ID;

			Ext.apply(
				this.filterBar,
				{
					xtype : 'panel',
					labelAlign : 'top',
					border : false,
					tbar : [
						{
							xtype : 'label',
							text : config.textFirstDay + ':',
							style : 'padding:0 2px;'
						},
						{
							xtype : 'datefield',
							id : 'FIRST_DAY',
							dataIndex : 'FIRST_DAY',
							displayField : 'TRADE_DATE',
							valueField : 'TRADE_DATE',
							//format : CC.b.enter.config.dateformat,
							value : config.firstDay,
							allowBlank : false
						},
						{
							xtype : 'label',
							text : config.textLastDay + ':',
							style : 'padding:0 2px 0 6px;'
						},
						{
							xtype : 'datefield',
							id : 'LAST_DAY',
							dataIndex : 'LAST_DAY',
							displayField : 'TRADE_DATE',
							valueField : 'TRADE_DATE',
							//format : CC.b.enter.config.dateformat,
							value : config.lastDay,
							allowBlank : false
						},
						{
							xtype : 'tbseparator'
						},
						{
							xtype : 'label',
							text : 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 : false,
					   		forceSelection : true,
							allowBlank : false
						},
						{
							xtype : 'tbseparator'
						},
						{
							xtype : 'label',
							text : config.textProducts + ':',
							style : 'padding:0 2px;'
						},
						{
							xtype : 'combo',
							id : 'PACKING_ID',
							dataIndex : 'PACKING_ID',
							displayField : 'text',
							width : 500,
							valueField : 'value',
							mode : 'remote',
							triggerAction : 'all',
							store : new Ext.data.JsonStore(
								{
									autoLoad : true,
									url : this.ajaxUrl + 'extComboListFilterProduct',
						  			fields : ['value', 'text'],
						  			root: 'result'
								}
							),
					   		autoLoad : true,
					   		typeAhead : false,
					   		forceSelection : true,
							allowBlank : false
						}
					],
					html : '<div id="productHistoryReportBox" class="x-panel-mc" style="overflow:auto;padding:8px;background:white;height:100%;"></div>'
				}
			);
			var center = CC.main.layout.center;
			center.add(this.filterBar);
			center.on('resize', this.centerResize);
			center.doLayout();

			var o;
			o = CC.help.form.setComboDefaultValue('WAREHAUSE_ID', config.defaultWarehouse.ID, config.defaultWarehouse.NAME);
			if(o) o.on('select', module.warehouseChange);

			o = Ext.getCmp('PACKING_ID');
			if(o) o.on('select', module.productChange);
			
			o = Ext.getCmp('FIRST_DAY');
			if(o) o.on('valid', module.firstDayChange);

			o = Ext.getCmp('LAST_DAY');
			if(o) o.on('valid', module.lastDayChange);

			this.centerResize();
		},


		centerResize : function() {
			var view = Ext.getCmp('CCWiew');
			if(view) document.getElementById('productHistoryReportBox').style.height = (view.getSize().height - 123) + 'px';
		},


		firstDayChange : function(obj) {
			var module = CC.f.mod.product_history;
			module.firstDay = obj.getValue().format('Y-m-d');
			module.loadContent();
		},


		lastDayChange : function(obj) {
			var module = CC.f.mod.product_history;
			module.lastDay = obj.getValue().format('Y-m-d');
			module.loadContent();
		},


		warehouseChange : function(obj) {
			var module = CC.f.mod.product_history;
			module.warehouseId = obj.getValue();
			module.loadContent();
		},


		productChange : function(obj) {
			var module = CC.f.mod.product_history;
			module.productId = obj.getValue();
			module.loadContent();
		},


		loadContent : function() {
			var module = CC.f.mod.product_history;
			if(module.productId == null) return;

			var param = {
					url : module.ajaxUrl + 'extGetHistory',	
					params : {
						firstDay : module.firstDay,	
						lastDay : module.lastDay,	
						warehouseId : module.warehouseId,
						productId : module.productId
					},
					singleInstance : true,
					success : function(result) {
						if(result.success) {
							var o = document.getElementById('productHistoryReportBox');
							o.scrollTop = 0;
							o.innerHTML = result.result;
						}
					}
				};	
				CC.help.ajax.doAjax(param);
		}

	}

} ();
