using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class etwuser_editpassword : System.Web.UI.Page
{
    public string username = string.Empty;
    public string msg = string.Empty;
    public string msg2 = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie ChatCookie = Request.Cookies["cookChatuser"];
        HttpCookie GetCookie = Request.Cookies["cookEtwuser"];
        if (Request.Cookies["cookChatuser"] == null)
        {
            if (Request.Cookies["cookEtwuser"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
                username = GetCookie["username"];
            }
        }
        else
        {
            username = ChatCookie["username"];
        }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        etw.chat_user chat = new etw.chat_user();
        etw.etw_user user = new etw.etw_user();
        DataSet ds = DbHelperSQL.Query("select id,userid,password from chat_user where username='" + username + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            int result = DbHelperSQL.GetchatUser(username, pwdtxt.Text.Trim());
            if (result > 0)  // 子帐户登入判断
            {
                if (re1pwdtxt.Text.Trim() != re2pwdtxt.Text.Trim())
                {
                    msg2 = "两次输入的密码不一致";
                }
                else if (re1pwdtxt.Text.Trim() == ds.Tables[0].Rows[0]["password"].ToString())
                {
                    msg2 = "新密码与旧密码不能相同";
                }
                else
                {
                    chat.id = Convert.ToInt32(ds.Tables[0].Rows[0]["id"]);
                    chat.password = re1pwdtxt.Text.Trim();
                    chat.Updatepassword();
                    chat.Updatechangepwd();
                    Fun.JsAlerT("密码修改成功！", "index.aspx");

                }
            }
            else { msg = "您的旧密码输入不正确"; }
        }
        else
        {
            DataSet ds1 = DbHelperSQL.Query("select userid,password from etw_user where username='" + username + "'");
            if (ds1.Tables[0].Rows.Count > 0)
            {
                int result = DbHelperSQL.GetEtwUser(username, pwdtxt.Text.Trim());
                if (result > 0)  // 总帐户登入判断
                {
                    if (re1pwdtxt.Text.Trim() != re2pwdtxt.Text.Trim())
                    {
                        msg2 = "两次输入的密码不一致";
                    }
                    else if (re1pwdtxt.Text.Trim() == ds1.Tables[0].Rows[0]["password"].ToString())
                    {
                        msg2 = "新密码与旧密码不能相同";
                    }
                    else
                    {
                        user.userid = Convert.ToInt32(ds1.Tables[0].Rows[0]["userid"]);
                        user.password = re1pwdtxt.Text.Trim();
                        user.Updatepassword();
                        user.Updatechangepwd();
                        Fun.JsAlerT("密码修改成功！", "index.aspx");
                    }
                }
                else { msg = "您的旧密码输入不正确"; }
            }
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        pwdtxt.Text = "";
        re1pwdtxt.Text = "";
        re2pwdtxt.Text = "";
    }
}
