LoginPanel = function() {
	// 登录表单
	this.loginForm = new Ext.FormPanel({
		title : '您好, 欢迎您 !',
		bodyStyle : 'background: #F1F4F9; padding:3px; padding-top: 25px',
		labelWidth : 50,
		labelAlign : 'right',
		buttonAlign : 'center',
		width : 200,
		defaults : {
			width : 80
		},
		defaultType : 'textfield', // 默认字段类型
		scope : this,
		// 定义表单元素
		items : [{
					fieldLabel : '用户名',
					name : 'cstId', // 元素名称
					allowBlank : false, // 不允许为空
					blankText : '用户名必须填写', // 错误提示内容
					anchor : '90%',
					enableKeyEvents : true,
					scope : this,
					listeners : {
						scope : this,
						'keypress' : function(thisFiled, e) {
							if (e.getKey() == Ext.EventObject.ENTER) {
								Ext.get('pwd-filed').focus();
							}
						}
					}
				}, {
					id : 'pwd-filed',
					fieldLabel : '密&nbsp;&nbsp;&nbsp;码',
					name : 'pwd',
					allowBlank : false,
					inputType : 'password',
					blankText : '密码必须填写',
					anchor : '90%',
					enableKeyEvents : true,
					scope : this,
					listeners : {
						scope : this,
						'keypress' : function(thisFiled, e) {
							if (e.getKey() == Ext.EventObject.ENTER) {
								this.login();
							}
						}
					}
				}],
		tools : [{
					id : 'help',
					qtip : '忘记密码?',
					handler : function(event, toolEl, panel) {
						Ext.Msg.hide();
						Ext.Msg.show({
									title : '提示',
									msg : '尊敬的客户, 如果您忘记了密码, 请致电 0871-6155181 与我们联系...',
									buttons : Ext.Msg.OK,
									icon : Ext.MessageBox.INFO
								});
					}
				}],
		bbar : [{
					xtype : 'tbfill'
				}, {
					xtype : 'tbseparator'
				}, {
					text : '登录',
					type : 'submit',
					iconCls : 'cx-login-1',
					scope : this,
					// 定义表单提交事件
					handler : this.login
				}, {
					xtype : 'tbseparator'
				}, {
					text : '注册',
					iconCls : 'cx-regist-1',
					handler : function() {
						this.registWindow = new RegistWindow().show();
					}
				}]
	});

	// 客户信息Panel
	this.userInfoPanel = new Ext.Panel({
				title : '您好, 欢迎回来...',
				bodyStyle : 'background: #F1F4F9; padding:3px; padding-top:10px',
				layout : 'table',
				scope : this,
				defaults : {
					bodyStyle : 'background: #F1F4F9; padding:5px; ',
					style : 'color:#3d5376;',
					border : false
				},
				layoutConfig : {
					columns : 2
				},
				items : [{
							html : '客户名称:'
						}, {
							id : 'cstName'
						}, {
							html : '用户类别:'
						}, {
							id : 'cstLevel'
						}, {
							html : '上次登录:'
						}, {
							id : 'cstLastLogTime'
						}],
				bbar : [{
							xtype : 'tbfill'
						}, {
							id : 'btn-backstage',
							xtype : 'button',
							text : '进入后台',
							scope : this,
							handler : function() {
								window.open('backstage/granted/index.jsp');
							}
						},'-', {
							xtype : 'button',
							text : '安全退出',
							scope : this,
							handler : function() {
								this.doCstLogout();
							}
						}]
			});

	LoginPanel.superclass.constructor.call(this, {
				id : 'login-panel',
				border : false,
				region : 'center',
				layout : 'card',
				activeItem : 0,
				height : 145,
				items : [this.loginForm, this.userInfoPanel]
			});

	this.loadUserInfo();

};

Ext.extend(LoginPanel, Ext.Panel, {
			login : function() {
				if (this.loginForm.form.isValid()) {
					// 提交到服务器操作
					this.loginForm.form.doAction('submit', {
								url : 'cstLogin.do',
								waitMsg : '正在登录, 请稍候...',
								waitTitle : '提示',
								scope : this,
								// 登录操作成功
								success : this.loadUserInfo,
								// 提交失败
								failure : function(form, action) {
									Ext.MessageBox.hide();
									Ext.MessageBox.show({
												title : '错误',
												msg : (Ext
														.isEmpty(action.result))
														? '与服务器连接失败, 请稍候再试...'
														: action.result.message,
												buttons : Ext.Msg.OK,
												icon : Ext.MessageBox.ERROR
											});
								}
							});
				}
			},

			loadUserInfo : function() {
				Ext.Ajax.request({
							scope : this,
							url : 'getUserInfo.do',
							success : function(response, options) {
								if (response.responseText != '') {
									var rst = Ext.util.JSON
											.decode(response.responseText);
									if (rst) {
										this.userInfoPanel.doLayout();
										this.userInfoPanel
												.getComponent('cstName')
												.getEl().update(rst.userName);
										this.userInfoPanel
												.getComponent('cstLevel')
												.getEl()
												.update(rst.customerLevel);
										this.userInfoPanel
												.getComponent('cstLastLogTime')
												.getEl()
												.update(rst.lastLoginTime);
										this.getLayout().setActiveItem(1);
										if (rst.customerLevel == '管理员'){
											Ext.get('btn-backstage').setVisible(true);
										} else {
											Ext.get('btn-backstage').setVisible(false);
										}
									} else {
										this.getLayout().setActiveItem(0);
									}
								}
							},
							failure : function(response, options) {
								Ext.Msg.hide();
								Ext.Msg.show({
											title : '错误',
											msg : '加载数据失败, 请稍候再试...',
											buttons : Ext.Msg.OK,
											icon : Ext.MessageBox.ERROR
										});
							}
						})
			},

			doCstLogout : function() {
				logoutSystem();
			}
		});