using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProductionControl
{
    public partial class FrmModifyPW : Form
    {
        public FrmModifyPW()
        {
            InitializeComponent();
        }

        private void FrmModifyPW_Load(object sender, EventArgs e)
        {
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Service.UserService service=new Service.UserService();
            try
            {
                string pw = this.txtPW.Text.Trim();
                pw = Utils.Util.GetMD5(pw);

                string newpw=this.txtNewPW.Text.Trim();
                string newpw2 = this.txtNewPW2.Text.Trim();
                if (newpw == "")
                    throw new Exception("请输入新密码！");
                if (newpw.Length <6)
                    throw new Exception("新密码长度至少6位！");
                if (newpw2 !=newpw)
                    throw new Exception("确认新密码输入不一致！");

                if (string.IsNullOrWhiteSpace(Config.loginUser.Password))
                    Config.loginUser.Password = "";
                if (Config.loginUser.Password!=pw)
                    throw new Exception("旧密码错误！");

                if(!service.ModifyPw(Config.loginUser.Id, Utils.Util.GetMD5(newpw)))
                    throw new Exception("密码修改失败！");

                MessageBox.Show("修改成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
    }
}
