using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using DotNet.FrameWork.Data;
using DotNet.FrameWork.Common.Encrypt;

namespace HteamDd.Web.ashx
{
    /// <summary>
    /// Dd_setting 的摘要说明
    /// </summary>
    public class Dd_setting : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request["mark"] == "setting")
            {
                string user_ID = context.Request["user_ID"].ToString();
                string email = context.Request["email"].ToString();
                string psw=Encrypt.EncryptPassword(context.Request["user_psw"].ToString(), "MD5");//对接受过来的值进行MD5加密

                string sql = "select User_Email,User_Passwd from Dd_User where User_ID=@ID";
                SqlParameter sqlpam = new SqlParameter("@ID", SqlDbType.Int);
                sqlpam.Value = user_ID;
                SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, sqlpam);
                string user_mes = string.Empty;
                while(sdr.Read())
                {
                    user_mes = sdr["User_Passwd"].ToString();
                }            
                if (psw== user_mes)//输入的密码正确才能修改
                {
                    string sqlup = "update Dd_User set User_Email=@email,User_Passwd=@psw where User_ID=@ID";
                    SqlParameter[] parameters = { 
                                                new SqlParameter("@email",SqlDbType.NVarChar,50),
                                                new SqlParameter("@psw",SqlDbType.NVarChar,50),
                                                new SqlParameter("@ID",SqlDbType.Int)
                                                };
                    parameters[0].Value = email;
                    parameters[1].Value =Encrypt.EncryptPassword(context.Request["new_psw"].ToString(),"MD5");//用户的新密码
                    parameters[2].Value = user_ID;
                    if (DbHelperSQL.ExecuteSql(sqlup, parameters) != 0)
                    {
                        context.Response.Write("1");//修改成功
                    }
                    else
                    {
                        context.Response.Write("0");//修改失败.
                    }
                }
                else
                {
                    context.Response.Write("psw_Error");//该用户输入的当前密码错误，不能执行修改操作
                }
              
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}