namespace Kesion.NET.WebSite
{
    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 System.IO;

    using Kesion.Publics;
    using Kesion.BLL;
    using Kesion.Cache;
    using Kesion.Weixin;
    public partial class Admin_SelectTemplate : KSCMS
    {
        public string ParentPath;
        public string _CurrPath, _PreFolder, _MainTemplatePath;
        public string _Action;
        public static int from3g = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            KSSecurity.PowerID = "Label/KS.Template.aspx";
            KSSecurity.CheckPermissions(1, true);

            _Action = S("action");
            _CurrPath = S("CurrPath");

            from3g = Utils.StrToInt(KSCMS.S("from3g"));
           
            if (from3g==1)
            {
                _MainTemplatePath = string.Concat(KSCMS.GetInstallDir(), M.GetConfig("TemplateDir"), "/" + M.GetConfig("TemplateStyleDir") + "/");
            }
            else
            {
                _MainTemplatePath = string.Concat(KSCMS.GetInstallDir(), MyCache.GetCacheConfig(13));
            }


            if (_CurrPath == "") _CurrPath = _MainTemplatePath;
            if (!IsPostBack)
            {   
                switch (_Action)
                {
                    case "filelist":
                        ParentPath += _CurrPath;
                        InitialList(ParentPath);
                        this.ParentPannel.Visible = false;
                        this.listpannel.Visible = true;
                        break;
                    default:
                        this.ParentPannel.Visible = true;
                        this.listpannel.Visible = false;
                        ListItem li=new ListItem();
                        li.Value=_MainTemplatePath;
                        li.Text=_MainTemplatePath;
                        this.FolderSelectList.Items.Add(li);
                        break;
                }
            }
        }

        void InitialList(string _path)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Size", typeof(string)));
            dt.Columns.Add(new DataColumn("Time", typeof(string)));
            dt.Columns.Add(new DataColumn("TypeName", typeof(string)));

            string[] files = Directory.GetFiles(Server.MapPath(_path));
            int id = 0;//自动编号
            long size = 0;//文件大小
            string TypeName;//类型

            DirectoryInfo d = new DirectoryInfo(Server.MapPath(_path));
            foreach (DirectoryInfo _d in d.GetDirectories())
            {
                id = id + 1;
                TypeName = "目录";
                size = DirSize(_d) / 1024;
                dt.Rows.Add(new string[] { id.ToString(), _d.Name.ToString(), size.ToString() + "K", _d.CreationTime.ToString(), TypeName });
            }

            foreach (string file in files)
            {
                System.IO.FileInfo fileInfo = new FileInfo(file);
                //判断文件扩展名
                if (fileInfo.Extension == ".html" || fileInfo.Extension == ".htm" || fileInfo.Extension == ".shtm" || fileInfo.Extension == ".shtml")
                {

                    id = id + 1;
                    size = fileInfo.Length / 1024;
                    TypeName = "文件";
                    dt.Rows.Add(new string[] { id.ToString(), fileInfo.Name.ToString(), size.ToString() + "K", fileInfo.CreationTime.ToString(), TypeName });
                }
            }
            dlPubInfo.DataSource = dt;
            dlPubInfo.DataBind();

        }
        public static long DirSize(DirectoryInfo d)
        {
            long Size = 0;
            //   Add   file   sizes.   
            FileInfo[] fis = d.GetFiles();
            foreach (FileInfo fi in fis)
            {
                Size += fi.Length;
            }
            //   Add   subdirectory   sizes.   
            DirectoryInfo[] dis = d.GetDirectories();
            foreach (DirectoryInfo di in dis)
            {
                Size += DirSize(di);
            }
            return (Size);
        }


        public string GetName(int id,string TypeName, string Title)
        { 
         if (TypeName=="目录")
             return @"<a class='FolderItem' href=""javascript:;"" onclick=""OpenFolder('" + Title + @"');"">" + Title + "</a>";
         else
             return @"<a class='FolderItem' href='javascript:;'  onclick=""parent.SelectFile('"+Title+@"');"">" + Title + "</a>";


        }


}
}