Ext.define('App.view.UserChangePasswordWindow', {
	extend: 'Ext.window.Window',
	alias: 'widget.userChangePasswordWindow',
	scrollable: true,
	constrainHeader: true,
	modal: true,

	onSaveBtnClick: function(button, event) {
		var me = this,
			f = me.down('form').getForm();
		if (f.isValid()) {
			f.submit({
				waitMsg: 'Saving...',
				success: function(form, action) {
					App.showMsg('Password changed');
					me.close();
				},
				failure: function(form, action) {
					App.log(action);
					App.showError('Failed to change password, please make sure you have entered your current password correctly!');
				}
			});
		}
	},
	initComponent: function(){
		var me = this;
		me.items=[
			{
				xtype: 'form',
				url:  App.url("/user/change-password"),
				padding: 5,
				defaults: {
					labelWidth: 200,
					width: 500
				},
				items: [
					{
						xtype: 'textfield',
						name: 'password',
						fieldLabel: App.localeRes('Current Password'),
						inputType: 'password',
						allowBlank: false,
						minLength: 8,
						maxLength: 20
					},
					{
						xtype: 'textfield',
						name: 'newPassword',
						fieldLabel: App.localeRes('New Password'),
						inputType: 'password',
						allowBlank: false,
						validator: function(value){
							const min = 8;
							const max = 20;
							const number = 'True';
							const upperEng = 'True';
							const lowerEng = 'True';
							const specialChar = 'True';

							let regExp = "^";
							if(number) regExp += '(?=.*[0-9])';
							if(upperEng) regExp += '(?=.*[A-Z])';
							if(lowerEng) regExp += '(?=.*[a-z])';
							if(specialChar) regExp += "(?=.*[!\"#$%&'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])";
							regExp += '.{'+min+','+max+'}';
							
							if(new RegExp(regExp).test(value)){
								return true;
							}else{
								let wrongMsg = 'Please Following Password Rule.<br />';
								wrongMsg += 'Minimum ' + min + ' Characters<br />';
								wrongMsg += 'Maximum ' + max + ' Characters<br />';
								if(number) wrongMsg += 'Numbers<br />';
								if(lowerEng) wrongMsg += 'Lower-Case Letters<br />';
								if(upperEng) wrongMsg += 'Capital Letters<br />';
								if(specialChar) wrongMsg += 'Symbols<br />';
								return wrongMsg;
							}
						}
					},
					{
						xtype: 'textfield',
						fieldLabel: App.localeRes('Confirm New Password'),
						inputType: 'password',
						submitValue: false,
						maxLength: 20,
						validator: function(value) {
							var p = me.down('field[name=newPassword]').getValue();
							return (p == value) ? true : 'New passwords do not match!';
						}
					}
				],
				buttons: [
					{
						xtype: 'button',
						itemId: 'saveBtn',
						text: App.localeRes('Save & Close'),
						ui: "blue",
						iconCls: 'silk-disk',
						formBind: true,
						handler: me.onSaveBtnClick,
						scope: me
					},
					{
						xtype: 'component',
						flex: 1
					},
					{
						xtype: 'button',
						text: App.localeRes('Cancel'),
						handler: function(button, event) {
							me.close();
						}
					}
				]

			}
		];

		me.listeners= {
			show: function(window, opts) {

			},
			close: function(window, opts) {
				if (me.callbackOnClose) me.callbackOnClose();
			},
			afterrender: function(window, options) {
				this.keyNav = new Ext.util.KeyMap({
					target: window.el,
					binding: [{
						key: 13,
						fn: function(keyCode,event){
							me.down('#saveBtn').click();
						}
					}]
				});
			}
		};

		me.callParent(arguments);
	}

});
