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

using DotNet.FrameWork.Utils;
using DotNet.FrameWork.Common.Web;
using DotNet.FrameWork.Page;
using DotNet.FrameWork.Data;
using DotNet.FrameWork.Common.Encrypt;
using System.Security.Cryptography;
using System.Text;
using GetCommand;

namespace HteamDd.Web.Admin
{
   
    public partial class Admin_add:BaseAdminUI
    {
        HaTeam.Dd.Model.Dd_Admin model = new HaTeam.Dd.Model.Dd_Admin();
        HaTeam.Dd.BLL.Dd_Admin bll = new HaTeam.Dd.BLL.Dd_Admin();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Fetch.Get("action") != null && Fetch.Get("cq_ask_id") != null)
                {
                    if (Fetch.Get("action").ToString() == "edit")
                    {
                        txt.Visible = true;
                        //txt.Style["Display"] = "Block";
                        model = bll.GetModel(int.Parse(Fetch.Get("Admin_id").ToString()));
                        txtName.Text = model.Admin_Name;
                    }
                }
               
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            model.Admin_Name = txtName.Text;
            model.Admin_Pwd = Encrypt.EncryptPassword(this.txtPassword.Text, "MD5");
            model.Admin_Datetime = DateTime.Now;
            model.Admin_UpDatetime = DateTime.Now;
            model.Admin_Delete = 0;
            if (Fetch.Get("action").ToString() == "edit")
            {
                string sql = "select admin_pwd from Dd_Admin where admin_id =" + Fetch.Get("Admin_id").ToString();
                string pass = Convert.ToString(DbHelperSQL.GetSingle(sql)).ToString();
                string oldpass = this.txtOldpass.Text.Trim();
                //Response.Write(MD5(oldpass).ToUpper());
                if (MD5(oldpass).ToUpper() == pass)
                {
                    model.Admin_ID = int.Parse(Fetch.Get("Admin_id"));
                    bll.Update(model);
                    Jscript.AlertAndRedirect("系统提示:密码修改成功!", "Admin_manage.aspx");

                }
                else
                {
                    Jscript.Alert("密码输入错误");
                }
            }
            else
            {
                bll.Add(model);
                Jscript.AlertAndRedirect("系统提示:操作成功!", "Admin_manage.aspx");
            }

        }
        /// <summary>
        /// MD5加密算法
        /// </summary>
        /// <param name="strPsw">用户名密码</param>
        /// <returns>加密后的结果</returns>
        public string MD5(string strPsw)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = Encoding.Default.GetBytes(strPsw);//将字符编码为一个字节序列
            byte[] md5data = md5.ComputeHash(data);//计算data字节的哈希值
            md5.Clear();
            string str = "";
            for (int i = 0; i < md5data.Length; i++)
            {
                str += md5data[i].ToString("x").PadLeft(2, '0');
            }
            return str;
        }
    }
}