using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
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 TopWinCMS.BLL;
using TopWinCMS.Common;
using System.Text;

namespace TopWinCMS.WebUI.Admin.Window
{
    public partial class GetImage : System.Web.UI.Page
    {
        private int PicLong = 80;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strDir;
                strDir = Server.MapPath(Config.UP);

                string str = strDir;
                str = str.Replace(Server.MapPath("~/"), "/");
                str = str.Replace("\\", "/");
                DDL_Path.Items.Insert(0, new ListItem(str, strDir));

                BindList("");
            }
        }

        private void BindList(string strDir)
        {

            DataTable dt_Files = new DataTable();
            DataRow dr_Files;

            dt_Files.Columns.Add(new DataColumn("FileName", System.Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("FullName", System.Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("FileSize", System.Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("LastWriteTime", System.Type.GetType("System.DateTime")));
            dt_Files.Columns.Add(new DataColumn("CreationTime", System.Type.GetType("System.DateTime")));


            //针对当前目录建立目录引用对象
            if (strDir.Length == 0 && strDir != Server.MapPath(Config.UP))
            {
                strDir = Server.MapPath(Config.UP);
            }
            DirectoryInfo dirInfo = new DirectoryInfo(strDir);
            //循环判断当前目录下的文件和目录
            FileInfo fi; 
            this.DropDownList1.Items.Clear();
            this.DropDownList1.Items.Add(new ListItem("---", dirInfo.Name));
            foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
            {
                //如果是文件
                if (fsi is FileInfo)
                {
                    dr_Files = dt_Files.NewRow();
                    fi = fsi as FileInfo;

                    dr_Files["FileName"] = fi.Name;
                    dr_Files["FullName"] = fi.FullName.Replace(Server.MapPath("~/"), "/");
                    dr_Files["FileSize"] = fi.Length;
                    dr_Files["LastWriteTime"] = fi.LastWriteTime;
                    dr_Files["CreationTime"] = fi.CreationTime;
                    dt_Files.Rows.Add(dr_Files);
                }
                if (fsi is DirectoryInfo)
                {
                    ListItem li = new ListItem();
                    li.Text = fsi.Name;
                    li.Value = fsi.FullName;
                    this.DropDownList1.Items.Add(li);
                }
            }

            PageNavigation1.RecordCount = dt_Files.Rows.Count;

            DataRow[] ArrDR = dt_Files.Select("", cbSorts.SelectedValue);

            DL_List.DataSource = ArrDR;
            DL_List.DataBind();

        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {

        }
        protected string ToolTip(object FileName, object FileSize, object CreationTime, object LastWriteTime)
        {
            StringBuilder winner = new StringBuilder();
            winner.AppendLine("文 件 名：" + FileName);
            winner.AppendLine("文件大小：" + FileSize);
            winner.AppendLine("建立时间：" + CreationTime);
            winner.AppendLine("最后修改：" + LastWriteTime);

            return winner.ToString();
        }

        protected void DL_List_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item)
            {
                Image img = e.Item.FindControl("Img") as Image;

                PicLong = int.Parse(cbThumbSize.SelectedValue);
                img.Width = PicLong;
                img.Height = PicLong;
            }
        }

        protected void ReBind(object sender, EventArgs e)
        {
            BindList(DDL_Path.SelectedValue);
        }

        protected string PicURL(object InPut)
        {


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

        protected void DDL_Path_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindList(DDL_Path.SelectedValue);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Directory.Exists(DropDownList1.SelectedValue))
            {
                string str = DropDownList1.SelectedValue;
                str = str.Replace(Server.MapPath("~/"), "/");
                str = str.Replace("\\", "/");
                ListItem li = new ListItem(str, DropDownList1.SelectedValue);
                if (DDL_Path.Items.Contains(li) == false)
                {
                    DDL_Path.Items.Insert(DDL_Path.Items.Count - 1, li);
                    DDL_Path.SelectedValue = DropDownList1.SelectedValue;
                }


                BindList(DropDownList1.SelectedValue);
            }
        }
    }
}