using System;
using System.Xml;
using System.IO;
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;

namespace MYAPP.Manage.MY
{
    public partial class Change_Password : MyApp.Web.UI.UserPage
    {
        int _User_ID = 0;
        public static string _filename = "/Data/User.config";
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                action = "edit";
                _User_ID = MyApp.Components.Authorization.User.Get_UserID();
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
        }

        override protected void DataInit()//数据初始化
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        override protected void pl_edit()
        {
            this.UI_Password_OR.Focus();
        }
        protected void ButPost_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Button bn = (Button)sender;
                if (action == "edit")
                {
                    string node_path = "/KRPano/User/Item[@username='{0}']";
                    XmlDocument xmldoc;
                    xmldoc = MyApp.Components.XML.XmlDocument.Load(String.Format(_filename));

                    XmlNodeList xmlnodelist = xmldoc.SelectNodes(String.Format(node_path, MyApp.Components.Authorization.User.Get_LogName()));
                    XmlElement elem_item = (XmlElement)xmlnodelist[0];

                    if (xmlnodelist.Count > 0)
                    {
                        if ((elem_item.GetAttribute("password") == this.UI_Password_OR.Text.Trim()))
                        {
                            SetElemValue(elem_item, "password", this.UI_Password);

                            try
                            {
                                 //xmldoc.Save(Server.MapPath(String.Format(_filename)));
                                 MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format(_filename)), xmldoc.OuterXml);
                            }
                            catch(Exception ex)
                            {
                                Response.Write(ex.ToString());
                            }
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Set OK!!!');top.close_dialog();</script>");
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('OPassword Error!!!');</script>");
                            this.UI_Password_OR.Focus();
                            return;
                        }
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');</script>");
                        return;
                    }
                }
              
            }
        }
    }
}
