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;
using Spaces;
using Spaces.Controls;
using System.Xml;
using System.IO;
public partial class Manage_PartInfo : ManagePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        PartsView.RowCommand += new GridViewCommandEventHandler(PartsView_RowCommand);
        PartsView.RowCreated += new GridViewRowEventHandler(PartsView_RowCreated);
        //PartsView.RowDeleting += new GridViewDeleteEventHandler(PartsView_RowDeleting);
        PartsView.RowUpdating += new GridViewUpdateEventHandler(PartsView_RowUpdating);
        PartsView.RowEditing += new GridViewEditEventHandler(PartsView_RowEditing);
        PartsView.RowCancelingEdit += new GridViewCancelEditEventHandler(PartsView_RowCancelingEdit);
      if (!IsPostBack)
            BindList();
    }

    void PartsView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        PartsView.EditIndex = -1;
        BindList();
    }

    
    void PartsView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        PartsView.EditIndex = e.NewEditIndex;
        BindList();
    }

    void PartsView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow vr = PartsView.Rows[e.RowIndex];
        Label lblModuleID = vr.FindControl("lblModuleID") as Label;
        string name = lblModuleID.Text;
        TextBox txtTitle =vr.FindControl("txtTitle") as TextBox;
        string title = txtTitle.Text;
        TextBox txtDescription = vr.FindControl("txtDescription") as TextBox;
        string desc = txtDescription.Text;
        UpdateModule(name,title,desc);
        PartsView.EditIndex = -1;
        BindList();
    }
    void UpdateModule(string name, string title, string desc)
    {
        string themesXmlFile = "/Modules.config";
        string file = HttpContext.Current.Server.MapPath(Globals.ApplicationPath + themesXmlFile);
        XmlDocument doc = new XmlDocument();

        FileInfo xmlFile = new FileInfo(file);
        if (!xmlFile.Exists)
        {
            throw new ApplicationException("没有找到配置文件");
        }
        doc.Load(xmlFile.FullName);

        XmlNode ts = doc.SelectSingleNode("Modules");
        // XmlNode selectNode = null;
        foreach (XmlNode n in ts.ChildNodes)
        {
            if (n.NodeType != XmlNodeType.Comment)
            {
               // Response.Write(string.Format("{0}|{1}<br/>", name, n["Name"].InnerText));
                if (name == n["Name"].InnerText.Trim())
                {

                    n["Title"].InnerText = title;
                    n["Description"].InnerText = desc;
                    break;
                }

            }
        }
        doc.Save(xmlFile.FullName);
        foreach (Module m in Globals.GetModules().ModuleList)
        {
            if (m.Name == name)
            {
                m.Title = title;
                m.Description = desc;
            }
        }
       // Response.Write(string.Format("{0}|{1}<br/>", name, title));
    }
    
    void PartsView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //throw new Exception("The method or operation is not implemented.");
    }

    void PartsView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            string name=e.CommandArgument.ToString();
            Globals.GetModules().DelelteModule(name);
          
        }
        BindList();
    }

    private void BindList()
    {
        PartsView.DataSource = Globals.GetModules().ModuleList;
        PartsView.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string mid = txtModuleID.Text;
        string mtitle = txtModuleTitle.Text;
        string desc = txtModuleDesc.Text;
        Module m = new Module();
        m.Name = mid;
        m.Title = mtitle;
        m.Description = desc;
        m.EnableRemove = true;
        AddModule(m);
        BindList();
    }
    public void AddModule(Module m)
    {
      
        Globals.GetModules().AddModule(m);

    }
   
}
