using System;
using System.Web.UI;

namespace Stat.web.v
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MemberCenter : System.Web.UI.Page
    {
        Stat.Cache.IConfig config = Stat.Cache.CacheFatory.CreateConfig();
        Stat.Security.ISecurity security = Stat.Security.DataFatory.CreateSecurity();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!security.IsUserLogin() || !security.IsUser())
            {
                Response.Write("您还没有登录，请先登录！");
                Response.End();
            }
            if (!Page.IsPostBack) { FillUserInfo(); }
        }
        /// <summary>
        /// 初始化用户信息
        /// </summary>
        protected void FillUserInfo()
        {
            Model.Users model = Stat.BLL.Users.GetUserInfo(security.GetCurrentUserID());
            if (model != null && model.UserName.Length > 0)
            {
                this.txtUserName.Text = model.UserName;
                this.txtUserName.Enabled = false;
                this.hdfOldPWD.Value = model.UserPwd;
                this.hdfUserId.Value = model.UserId.ToString();
                this.txtUEmail.Text = model.UserEmail;
                this.txtUQQ.Text = model.UserQQ;
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveWebsite2_Click(object sender, EventArgs e)
        {
            Stat.Model.Users model = new Stat.Model.Users();
            model.UserId = Convert.ToInt32(this.hdfUserId.Value);
            model.UserPwd = this.txtUserPWD1.Text.Trim();
            model.UserEmail = this.txtUEmail.Text.Trim();
            model.UserQQ = this.txtUQQ.Text.Trim();
            model.UserState = 1;
            model.IsPwdMD5 = config.IsUserPwdMd5();
            if (CheckOldPassWord(model.UserId))
            {
                if (Stat.BLL.Users.Edit(model))
                {
                    Plugin.Common.Alert("修改成功！", "?");
                }
                else
                {
                    Plugin.Common.Alert("操作失败！");
                }
            }
            else
            {
                Plugin.Common.Alert("旧密码输入错误！");

            }
        }
        /// <summary>
        /// 检查旧密码
        /// </summary>
        /// <returns></returns>
        protected bool CheckOldPassWord(int UserId)
        {
            if (security.GetCurrentUserID() != UserId) { return false; }
            string strPassword = this.txtOldPWD.Text.Trim();
            if (config.IsUserPwdMd5()) { strPassword = Plugin.Common.MD5(strPassword); }
            if (strPassword.ToUpper() == this.hdfOldPWD.Value.Trim().ToUpper()) { return true; }
            return false;
        }
    }
}
