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

namespace yinLouSys
{
    public partial class frmUpdatePwd : Form
    {
        private frmMain _frmParent;

        public frmMain FrmParent
        {
            get { return _frmParent; }
            set { _frmParent = value; }
        }

        private string _oldPwd;

        public frmUpdatePwd()
        {
            InitializeComponent();
        }

        private void frmUpdatePwd_Load(object sender, EventArgs e)
        {
            _oldPwd = code.SQLHelp.ExecuteScalar("select password from tab_user where userName='"+_frmParent.UserName+"'", null).ToString();

            SetFormSize();
        }

        private void SetFormSize()
        {
            this.Top = 0;
            this.Left = 0;
            this.Width = _frmParent.ClientSize.Width - 150;
            this.Height = _frmParent.ClientSize.Height - 100;

            //this.Size=_frmParent.ClientSize.Height;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if ("" == txtOld.Text)
                {
                    MessageBox.Show("请输入旧密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtOld.Focus();
                    return;
                }

                if ("" == txtNew.Text)
                {
                    MessageBox.Show("请输入新密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtNew.Focus();
                    return;
                }

                if (!txtNew.Text.Equals(txtConfirm.Text))
                {
                    MessageBox.Show("对不起，两次输入的密码不一致", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                    
                    return;
                }

                string pwd = code.Base.StringToMD5Hash(txtNew.Text);

                code.SQLHelp.ExecuteNoResult("update tab_user set password='"+pwd+"'  where userName='" + _frmParent.UserName + "'");
                MessageBox.Show("密码修改成功，请牢记您的新密码", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                code.LogInfor bean = new yinLouSys.code.LogInfor();
                bean.UserName = _frmParent.UserName;
                bean.Ip = "";
                bean.OperType = "修改密码";
                bean.Discrible = "原密码“"+txtOld.Text+"”";

                code.LogMan.AddLog(bean);

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}