using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FineUIPro.Web.common.SysManage
{
    public partial class UpdatePasswordEdit : PageBase
    {        
        /// <summary>
        /// 修改密码页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                string userId = Request.Params["userId"];
                if (this.CurrUser.Account == BLL.Const.Gly)
                {
                    this.txtOldPassword.Hidden = true;
                    this.txtNewPassword.Focus();
                }

                if (!string.IsNullOrEmpty(userId))
                {
                    var user = BLL.Sys_UserService.GetUsersByUserId(userId);
                    if (user != null)
                    {
                        this.txtUserName.Text = user.UserName;
                        this.txtAccount.Text = user.Account;                        
                    }
                }
            }
        }

        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string userId = Request.Params["userId"];
            var user = BLL.Sys_UserService.GetUsersByUserId(userId);
            if (user != null)
            {
                if (this.CurrUser.Account != BLL.Const.Gly)
                {
                    if (string.IsNullOrEmpty(this.txtOldPassword.Text))
                    {
                        Alert.ShowInParent("Please enter the old password!");
                        return;
                    }

                    if (user.Password != BLL.Sys_UserService.EncryptionPassword(this.txtOldPassword.Text))
                    {
                        Alert.ShowInParent("The old password was entered incorrectly!");
                        return;
                    }
                }
                if (this.txtNewPassword.Text != this.txtConfirmPassword.Text)
                {
                    Alert.ShowInParent("The password is inconsistent with the confirmation password entered!");
                    return;
                }

                BLL.Sys_UserService.UpdatePassword(user.UserId, Sys_UserService.EncryptionPassword(this.txtNewPassword.Text));
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify the password!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        /// <summary>
        ///  确认密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtConfirmPassword_Blur(object sender, EventArgs e)
        {
            if (this.txtNewPassword.Text != this.txtConfirmPassword.Text)
            {
                Alert.ShowInParent("The password is inconsistent with the confirmation password entered!");
                return;
            }
        }
    }
}