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.Collections.Generic;
using Xiaoxiao.Upload;
using System.IO;
public partial class Manage_PlugInfo : ManagePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PartsView.RowCreated += new GridViewRowEventHandler(PartsView_RowCreated);
        PartsView.RowCommand += new GridViewCommandEventHandler(PartsView_RowCommand);
       // if (!IsPostBack)
            BindList();
          
    }

    void PartsView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "add")
        {
          List<Module> list=  UserPlug.GetPlugModules();
          foreach (Module m in list)
          {
              if (m.Name == e.CommandArgument.ToString())
              {
                  Globals.GetModules().AddModule(m);
                  break;
              }

          }
          
        }
        if (e.CommandName == "remove")
        {
            string name = e.CommandArgument.ToString();
            Globals.GetModules().DelelteModule(name);
           
 
        }
        BindList();
    }

    void PartsView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 
            Module m=e.Row.DataItem as Module;
            if (m != null)
            {
                LinkButton lbtnOpt = e.Row.FindControl("lbtnOpt") as LinkButton;
                if (lbtnOpt != null)
                {
                    lbtnOpt.CommandArgument = m.Name;
                    if (Globals.GetModules().GetModule(m.Name) != null)
                    {
                        lbtnOpt.Text = "移除插件";
                        lbtnOpt.CommandName = "remove";
                        lbtnOpt.OnClientClick = "javascript:return confirm('是否要移除该模块？');";
                    }
                    else
                    {
                        lbtnOpt.CommandName = "add";
                        lbtnOpt.Text = "添加到模块";
                    }
                }
            
            }
        }
    }
    void BindList()
    {
        this.PartsView.DataSource = UserPlug.GetPlugModules();
        this.PartsView.DataBind();
    
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string path = Globals.PlugsPath;
        UploadFile uploadFile = UploadHelper.GetUploadFile("file1");
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        string fileName = Path.GetFileName(uploadFile.FileName);
        string newFile=Utils.GetNewFileName(Path.Combine(path, fileName));
        uploadFile.SaveAs(Path.Combine(path, newFile));
        UserPlug.RemovePlugCache();
        BindList();
    }
}
