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.Template_Lable
{
    public partial class Template : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RightsCheck.CheckWorkPower("T_4");

                BindList();
            }
        }
        private void BindList()
        {
            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("Size", System.Type.GetType("System.String")));

            dt_Files.Columns.Add(new DataColumn("LastWriteTime", System.Type.GetType("System.DateTime")));
            dt_Files.Columns.Add(new DataColumn("Edit", System.Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("See", System.Type.GetType("System.String")));

            //针对当前目录建立目录引用对象
            string strDir;
            if (Request["dir"] == null)
            {
                strDir = Server.MapPath("~/Templates/" + TemplatesManage.GetTemplatePath());

            }
            else
            {
                strDir = Server.UrlDecode(Request["dir"]);
            }

            this.LB_Path.Text = Config.SiteName + " → <a href=\"Template.aspx\">模板管理</a>";


            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;

                    //取得文件的大小
                    dr_Files["Size"] = dFileSize(Convert.ToDouble(fi.Length));

                    //取得文件的创建时间
                    dr_Files["LastWriteTime"] = fi.LastWriteTime;

                    if (fsi.Extension.ToLower() == ".aspx")
                    {
                        dr_Files["Edit"] = "<a href=\"Template_Edit.aspx?FilePath=" + Server.UrlEncode(fi.FullName) + "\">文本编辑</a>";
                        dr_Files["See"] = "<a href=\"" + fi.FullName.Replace(Server.MapPath("~/"), "/") + "\" target=_blank>模板查看</a>";
                    }
                    else
                    {
                        dr_Files["Edit"] = "";
                        dr_Files["See"] = "";
                    }
 
                    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=\"Template.aspx?dir=" + Server.UrlEncode(di.FullName) + "\">" + di.Name + "</a>";
                    dr_Files["FullName"] = di.FullName;

                    long l = 0;
                    foreach (FileInfo f in di.GetFiles())
                    {
                        l += f.Length;
                    }
                    dr_Files["Size"] = dFileSize(Convert.ToDouble(l));

                    dr_Files["LastWriteTime"] = di.LastWriteTime;
                    dt_Files.Rows.Add(dr_Files);
                }
            }


            GV_List.DataSource = dt_Files;
            GV_List.DataBind();



        }

        private string dFileSize(double FileSize)
        {
            if ((FileSize < 1024 * 1024))
            {

                return Math.Round((FileSize / 1024), 2) + " KB";
            }
            else
            {

                return Math.Round(FileSize / (1024 * 1024), 2) + " MB";
            }
        }
    }
}