Ext.define('App.view.DatetimeField',{
	extend:'Ext.form.FieldContainer',
	alias:'widget.datetimeField',

	layout:'hbox',

	config:{
		name: '',
		allowBlank: true,
		labelName: 'Datetime',
		increment: 30,
		onChange: null,
		readOnly: false,
		gridSubmit: false,
		minValue: '00:00',
		maxValue: '23:59',
	},
	initComponent: function() {
		var me = this;

		Ext.applyIf(me, {
			resetOriginalValue: function() {
				this.down('datefield').resetOriginalValue();
				this.down('timefield').resetOriginalValue();
			},
			isValid: function() {
				return this.down('datefield').isValid() && this.down('timefield').isValid();
			},
			setValueStr: function(){
				return this.down('hidden').getValue();
			},
			setDisplayValue: function(date){
				this.down('hidden').setValue(me.gridSubmit? date.getTime(): Ext.Date.format(date, App.dateTimeSubmitFormat));

				this.down('datefield').setValue(date);
				this.down('timefield').setValue(date);

				if(this.onChange) this.onChange(this);
			},
			setMaxDay: function(date){
				this.down('datefield').setMaxValue(date);
				this.down('datefield').isValid();
			},
			setMinDay: function(date){
				this.down('datefield').setMinValue(date);
				this.down('datefield').isValid();
			},
			getValue: function(){
				var date = this.down('datefield').getValue(),
				time = this.down('timefield').getValue();

				if(!date || !time) return null;

				return new Date(
					date.getFullYear(),
					date.getMonth(),
					date.getDate(),
					time.getHours(),
					time.getMinutes(),
					time.getSeconds()
				);
			},
			getField: function(){
				var rs = new Array();
				rs['datefield'] = this.down('datefield');
				rs['timefield'] = this.down('timefield');
				return rs;
			},
			setReadOnly: function(readOnly){
				me.getField().forEach(function(item){
					item.setReadOnly(readOnly);
				});
			},
			fieldLabel: me.labelName,
			items:[
				{
					xtype: 'hidden',
					name: me.name,
					allowBlank: me.allowBlank,
					gridSubmit: me.gridSubmit,
					setDisplayValue: function(value) {
						if (value) me.setDisplayValue(new Date(value));
					}
				},
				{
					xtype:'datefield',
					readOnly: me.readOnly,
					submitValue: false,
					selectOnFocus: true,
					allowBlank: me.allowBlank,
					flex:6,
					listeners:{
						change: function(field, newValue, oldValue){
							var date = newValue,
							time = me.down('timefield').getValue();
							if(!time) time = new Date();

							if(newValue){
								me.setDisplayValue(new Date(
									date.getFullYear(),
									date.getMonth(),
									date.getDate(),
									time.getHours(),
									time.getMinutes(),
									time.getSeconds()
								));
							}else
								me.setDisplayValue(null);
						}
					}
				},
				{
					xtype: 'timefield',
					readOnly: me.readOnly,
					format: 'H:i',
					submitValue: false,
					selectOnFocus:true,
					increment: me.increment,
					allowBlank: me.allowBlank,
					autoSelect: true,
					checkChangeBuffer: 1800,
					minValue: me.minValue,
					maxValue: me.maxValue,
					flex:4,
					listeners:{
						change: function(field, newValue, oldValue){
							var date = me.down('datefield').getValue(),
							time = newValue;
							if(!date) date = new Date();

							if(newValue){
								me.setDisplayValue(new Date(
									date.getFullYear(),
									date.getMonth(),
									date.getDate(),
									time.getHours(),
									time.getMinutes(),
									time.getSeconds()
								));
							}else
								me.setDisplayValue(null);
						}
					}
				}
			]
		});

		me.callParent(arguments);
	}
});
