using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using EKETEAM.Data;
using EKETEAM.FrameWork;
namespace eFrameWork.Plugins
{
    public partial class IconSelect : System.Web.UI.Page
    {
        private string basePath = "";//当前目录
        public string path = eParameters.QueryString("path");
        string aspxFile = eBase.getAspxFileName();
        public string siteid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            siteid = eConfig.SiteID();
            basePath = Server.MapPath("~/") + "upload\\" + (siteid.Length > 0 ? siteid + "\\" : "") + "filemanage\\";
            if (path.Length > 0) basePath += path.Replace("/", "\\") + "\\";
            if (!Directory.Exists(basePath)) Directory.CreateDirectory(basePath);

            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"filemanage_local\">");
            if (path.Length > 0)
            {
                sb.Append("<a href=\"" + aspxFile + "\">根目录</a>");
                string[] arr = path.Split("/".ToCharArray());
                string tmp = "";
                for (int i = 0; i < arr.Length; i++)
                {
                    if (i > 0) tmp += "/";
                    tmp += arr[i];
                    if (i == arr.Length - 1)
                    {
                        sb.Append(" / " + arr[i]);
                    }
                    else
                    {
                        sb.Append(" / <a href=\"" + aspxFile + "?path=" + tmp + "\">" + arr[i] + "</a>");
                    }

                }
            }
            else
            {
                sb.Append("根目录");
            }
            sb.Append("</div>");

            sb.Append("<div class=\"filemanage_files\" style=\"margin:8px;\">");
            System.IO.DirectoryInfo dinfo = new DirectoryInfo(basePath);
            foreach (DirectoryInfo a in dinfo.GetDirectories())
            {
                sb.Append("<a href=\"" + aspxFile + "?path=" + (path.Length > 0 ? path + "/" : path) + a.Name.ToString() + "\" title=\"" + a.Name + "\">\r\n");
                sb.Append("<dl>\r\n");
                sb.Append("<dt class=\"folder\"></dt>\r\n");
                sb.Append("<dd>" + a.Name + "</dd>\r\n");
                sb.Append("</dl>\r\n");
                sb.Append("</a>\r\n");
            }

            foreach (FileInfo a in dinfo.GetFiles())
            {

                sb.Append("<a href=\"javascript:;\" onclick=\"parent.setIcon('upload/" + (siteid.Length > 0 ? siteid + "/" : "") + "filemanage" + (path.Length > 0 ? "/" + path : "") + "/" + a.Name + "');\" title=\"" + a.Name + "\">");
                sb.Append("<dl>\r\n");
                if (".jpg.jpeg.gif.bmp.png.tif".IndexOf(a.Extension.ToLower()) > -1)
                {
                    sb.Append("<dt" + (".gif.png".IndexOf(a.Extension.ToLower()) > -1 ? " style=\"background-color:#f1f1f1;\"" : "") + "><img src=\"../upload/" + (siteid.Length > 0 ? siteid + "/" : "") + "filemanage" + (path.Length > 0 ? "/" + path : "") + "/" + a.Name + "\" /></dt>\r\n");
                }
                else
                {
                    sb.Append("<dt class=\"file\"></dt>\r\n");
                }
                sb.Append("<dd>" + a.Name + "</dd>\r\n");
                sb.Append("</dl>\r\n");
                sb.Append("</a>\r\n");
            }
            sb.Append("</div>");

            litBody.Text = sb.ToString();
        }
    }
}