using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using TopWinCMS.BLL;
using TopWinCMS.Common;
namespace TopWinCMS.WebUI.Admin.Window
{
    public partial class GetTemplate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strDir;
                strDir = Server.MapPath("~/Templates/" + TemplatesManage.GetTemplatePath());

                string str = strDir;
                str = str.Replace(Server.MapPath("~/"), "/");
                str = str.Replace("\\", "/");
                DropDownList1.Items.Insert(0, new ListItem(str, strDir));
                BindList("");
            }
        }
        private void BindList(string strDir)
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);

            DataTable dt_Files = new DataTable();

            DataRow dr_Files;


            dt_Files.Columns.Add(new DataColumn("ICO", System.Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("Name", System.Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("FullName", System.Type.GetType("System.String")));



            dt_Files.Columns.Add(new DataColumn("Get", System.Type.GetType("System.String")));


            //针对当前目录建立目录引用对象

            if (string.IsNullOrEmpty(strDir))
            {
                strDir = Server.MapPath("~/Templates/" + TemplatesManage.GetTemplatePath());
            }


            DirectoryInfo dirInfo = new DirectoryInfo(strDir);
            //循环判断当前目录下的文件和目录
            FileInfo fi;
            DirectoryInfo di;

            foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
            {
                //如果是文件
                if (fsi is FileInfo)
                {
                    dr_Files = dt_Files.NewRow();
                    fi = fsi as FileInfo;

                    dr_Files["ICO"] = fi.Extension.Replace(".", "") + ".gif";

                    dr_Files["Name"] = fi.Name;
                    dr_Files["FullName"] = fi.FullName;

                   string str = fi.FullName.Replace(Server.MapPath("~/"), "/");
                    str = str.Replace("\\", "/");

                    dr_Files["Get"] = "<input id=\"Button1\" type=\"button\" class=\"btn\" value=\"选择\" onclick='ClickRetrun(\"" + str + "\");' />";

                    dt_Files.Rows.Add(dr_Files);
                }
                else
                {
                    dr_Files = dt_Files.NewRow();

                    di = fsi as DirectoryInfo;
                    dr_Files["ICO"] = "folder.gif";
                    //dr_Files["Name"] = "<a href=\"GetTemplate.aspx?dir=" + di.FullName + "\">" + di.Name + "</a>";
                    dr_Files["Name"] = di.Name;
                    dr_Files["FullName"] = di.FullName;


                    dt_Files.Rows.Add(dr_Files);
                }
            }
            GV_List.DataSource = dt_Files;
            GV_List.DataBind();

            MyJavascript.RegJS(UpdatePanel1, "attachMouseEvent('" + GV_List.ClientID + "');");
       }



        protected string GetTempalteURL(object InPut)
        {
            string OutPut = "ClickRetrun(\"" + InPut.ToString().Replace("\\", "/") + "\")";
            return OutPut;
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "GOTO")
            {

                string strDir = e.CommandArgument.ToString();

                if (Directory.Exists(strDir))
                {
                    string str = strDir;
                    str = str.Replace(Server.MapPath("~/"), "/");
                    str = str.Replace("\\", "/");
                    ListItem li = new ListItem(str, strDir);
                    if (DropDownList1.Items.Contains(li) == false)
                    {
                        DropDownList1.Items.Insert(DropDownList1.Items.Count - 1, li);
                        DropDownList1.SelectedValue = strDir;
                    }

                    BindList(strDir);
                }
                else
                {
                    strDir = strDir.Replace(Server.MapPath("~/"), "/");
                    strDir = strDir.Replace("\\", "/");
                    MyJavascript.RegJS(UpdatePanel1, "ClickRetrun(\"" + strDir + "\");");
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindList(DropDownList1.SelectedValue);
        }
    }
}