﻿Ext.ns("eKonx.Login");
Ext.ns("eKonx.Res.Login");
Ext.ns("eKonx.Urls.Login");

eKonx.Res.Login =
{
	Password: "Пароль",
	PasswordRepeat: "Повтор пароля",
	Organization: "Организация",
	Action: "Действие",
	LogOnAction: "Вход в систему",
	ForgotPasswordAction: "Напомнить пароль",
	RegistrationAction: "Регистрация",
	ChangePasswordAction: "Изменить пароль",
	PasswordReminder: "Код для восстановления пароля",
	LoginData:
	{
		Loaded: false,
		Rendered: false
	}
}
eKonx.Urls.Login =
{
	Login: "/Account/Login",
	GetOrganizations: 'Account/GetOrganizations'
}


eKonx.Login = Ext.extend(
Ext.Window,
{
	loginConfig:
	{
		modal: true,
		autoHeigth: true,
		layout: "fit",
		shadow: false,
		width: 300,
		height: 204,
		//autoHeight: true,
		closable: false,
		resizable: false,
		draggable: false,
		autoShow: false,
		border: false,
		items:
        [
			{
				xtype: "form",
				bodyStyle: "padding: 10px",
				defaults: { anchor: "100%" },
				url: eKonx.Urls.Login.Login,
				standardSubmit: false,
				hideLabels: true,
				id: "LoginForm",
				tbar: new Ext.Toolbar(
				{
					items:
					[
						{
							margins: '0 10 0 0',
							xtype: "tbfill"
						},
						{
							text: eKonx.Res.Login.Action
						},
						{
							xtype: "combo",
							name: "Action",
							id: "cbAction",
							editable: false,
							triggerAction: 'all',
							value: 1,
							mode: 'local',
							valueField: 'id',
							displayField: 'displayText',
							store: new Ext.data.ArrayStore(
							{
								id: 0,
								fields: ['id', 'displayText'],
								data:
								[
									[1, eKonx.Res.Login.LogOnAction],
									[2, eKonx.Res.Login.ForgotPasswordAction],
									[3, eKonx.Res.Login.RegistrationAction],
									[4, eKonx.Res.Login.ChangePasswordAction]
								]
							}),
							listeners:
							{
								select: function(combo, record, index)
								{
									var form = combo.ownerCt.ownerCt.getForm();
									function Hide(id)
									{
										var field = form.findField(id);
										field.hide();
										field.disable();
									}
									function Show(id)
									{
										var field = form.findField(id);
										field.show();
										field.enable();
									}
									switch (record.data.id)
									{
										case (1):
											{
												//login
												Show("cbOrganization");
												Show("tbEmail");
												Show("tbPwd");
												Show("cbRememberMe");
												Hide("tbPwdRepeat");
												Hide("tbPwdReset");
												break;
											}
										case (2):
											{
												//remember pwd
												Hide("cbOrganization");
												Show("tbEmail");
												Hide("tbPwd");
												Hide("tbPwdRepeat");
												Hide("tbPwdReset");
												Hide("cbRememberMe");
												break;
											}
										case (3):
											{
												//register
												Show("cbOrganization");
												Show("tbEmail");
												Show("tbPwd");
												Show("tbPwdRepeat");
												Hide("tbPwdReset");
												Hide("cbRememberMe");
												break;
											}
										case (4):
											{
												//password change
												Show("tbPwd");
												Show("tbPwdRepeat");
												Show("tbPwdReset");
												Hide("cbOrganization");
												Hide("cbRememberMe");
												Hide("tbEmail");
												break;
											}
									}
								}
							}
						}
					]
				}),
				items:
				[
					cb = {
						xtype: "combo",
						name: "Organization",
						id: "cbOrganization",
						hiddenName: "organizationId",
						valueField: 'ID',
						displayField: 'Name',
						emptyText: eKonx.Res.Login.Organization,
						triggerAction: 'all',
						mode: 'local',
						editable: false,
						lazyInit: false,
						lazyRender: false,
						restoreData: function()
						{
							var cbRememberMe = Ext.ComponentMgr.get('cbRememberMe');
							var cbOrganization = Ext.ComponentMgr.get('cbOrganization');
							if (cbRememberMe.getValue())
							{
								cbOrganization.setValue(Ext.state.Manager.get("cbOrganization"));
							}
						},
						store: new Ext.data.JsonStore(
						{
							storeId: 'myStore',
							root: "root",
							autoLoad: true,
							proxy: new Ext.data.HttpProxy(
							{
								url: eKonx.Urls.Login.GetOrganizations,
								method: 'POST'
							}),
							fields:
							[
								"Name",
								"ID"
							],
							listeners:
							{
								load: function()
								{
									eKonx.Res.Login.LoginData.Loaded=true;
									if (eKonx.Res.Login.LoginData.Rendered)
									{
										cb.restoreData();
									}
								}
							}
						}),
						listeners:
						{
							afterrender: function()
							{
								eKonx.Res.Login.LoginData.Rendered = true;
								if (eKonx.Res.Login.LoginData.Loaded)
								{
									cb.restoreData();
								}
							},
							select: function(combobox, record)
							{
								if (combobox.isValid())
								{
									if (this.ownerCt.findById("cbRememberMe").getValue())
									{
										Ext.state.Manager.set("cbOrganization", record.get('ID'));
									}
								}
							}
						}
					},
					{
						xtype: "textfield",
						name: "Email",
						id: "tbEmail",
						emptyText: eKonx.Res.Common.Email,
						allowBlank: false,
						vtype: "email",
						listeners:
						{
							change: function(textbox, newValue)
							{
								if (textbox.isValid())
								{
									if (this.ownerCt.findById("cbRememberMe").getValue())
									{
										Ext.state.Manager.set("tbEmail", newValue);
									}
								}
							},
							afterrender: function()
							{
								if (this.ownerCt.findById("cbRememberMe").getValue())
								{
									this.setValue(Ext.state.Manager.get("tbEmail"));
								}
							}
						}
					},
					{
						xtype: "textfield",
						name: "PwdReset",
						id: "tbPwdReset",
						emptyText: eKonx.Res.Login.PasswordReminder,
						allowBlank: false,
						disabled: true,
						hidden: true
					},
					{
						xtype: "textfield",
						name: "Pwd",
						id: "tbPwd",
						inputType: 'password',
						emptyText: eKonx.Res.Login.Password,
						allowBlank: false
					},
					{
						xtype: "textfield",
						name: "PwdRepeat",
						id: "tbPwdRepeat",
						emptyText: eKonx.Res.Login.PasswordRepeat,
						allowBlank: false,
						disabled: true,
						hidden: true
					},
					{
						xtype: "checkbox",
						boxLabel: "Запомнить меня",
						name: "RememberMe",
						id: "cbRememberMe",
						stateful: true,
						stateId: "cbRememberMe",
						stateEvents: ["check"],
						applyState: function(state)
						{
							this.setValue(state.checked);
						},
						getState: function()
						{
							return { checked: this.getValue() };
						},
						handler: function(checkbox, checked)
						{
							if (!checked)
							{
								Ext.state.Manager.clear("tbEmail");
								Ext.state.Manager.clear("cbOrganization");
							}
							else
							{
								if (this.rendered)
								{
									this.ownerCt.SaveState();
								}
							}
						}
					}
				],
				SaveState: function()
				{
					Ext.state.Manager.set("tbEmail", Ext.ComponentMgr.get("tbEmail").getValue());
					Ext.state.Manager.set("cbOrganization", Ext.ComponentMgr.get('cbOrganization').getValue());
				},
				buttons:
				[
					{
						text: 'OK',
						handler: function()
						{
							var form = this.ownerCt.ownerCt.getForm();
							if (!form.isValid())
							{
								eKonx.Error(eKonx.Res.Common.WrongCredentials);
								return;
							}
							/*
							if (Ext.ComponentMgr.get("cbRememberMe").getValue())
							{
							Ext.ComponentMgr.get("LoginForm").SaveState();
							}
							*/
							var act = form.tbar.getComponent("cbAction").getValue();
							var sendData = { action: act };
							Ext.apply(sendData, form.getValues());
							var result = eKonx.Json(eKonx.Urls.Login.Login, sendData);
							if (result && act == 1 && result.success)
							{
								eKonx.LoginWindowInstance.hide();
								eKonx.Runtime.RunApplication();
							}
							return;
						}
					}
				]
			}
		]
	},
	constructor: function(config)
	{
		config = config || {};
		Ext.apply(config, this.loginConfig);
		eKonx.Login.superclass.constructor.call(this, config);
	}
});
