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 TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin.Template
{
    public partial class HtmlTemplate_Up : System.Web.UI.Page
    {

        //private string UpFiledPath;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RightsCheck.CheckWorkPower("T_1");
                if (Winrar.Exists() == false)
                {
                    Uploader1.Visible = false; LB_Err.Visible = true;
                    LB_Err.Text = "对不起服务器没有安装Winrar";
                }
                else
                {
                    LB_Err.Visible = false;
                }
            }
        }


        protected void Uploader1_FileUploaded(object sender, CuteEditor.UploaderEventArgs args)
        {
            string Path = Server.MapPath("~/Temp/");

            args.MoveTo(Path + args.FileGuid + args.FileName);

            Winrar rar = new Winrar();

            if (Directory.Exists(Path) == false)
            {
                Directory.CreateDirectory(Path);
            }

            rar.unCompressRAR(Server.MapPath("~/Templates/"), Server.MapPath("~/Temp/"), args.FileGuid + args.FileName);
 

            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Templates/"));

            DateTime dt = DateTime.MinValue;

            DirectoryInfo UpDir = null;
            foreach (DirectoryInfo d in di.GetDirectories())
            {
                if (d.CreationTime > dt)
                {
                    UpDir = d;
                }
            }
            if (UpDir == null)
            {
                MsgBox.Alert("程序找不到你刚刚上传的模板,请自己录入模板集的信息.", UpdatePanel1);
            }
            else
            {

                TemplatesInfo model = new TemplatesInfo();
                model.TemplateName = GetValue("TemplateName", UpDir.FullName);
                model.TemplatePath = GetValue("TemplatePath", UpDir.FullName);

                model.Style = GetValue("Style", UpDir.FullName);
                model.Remark = GetValue("Remark", UpDir.FullName);
                model.ArticleShow = GetValue("ArticleShow", UpDir.FullName);
                model.ArticleClass = GetValue("ArticleClass", UpDir.FullName);
                model.ArticleSpecial = GetValue("ArticleSpecial", UpDir.FullName);
                model.ArticleChannelIndex = GetValue("ArticleChannelIndex", UpDir.FullName);
                model.ArticleSpecialIndex = GetValue("ArticleSpecialIndex", UpDir.FullName);
                model.SoftShow = GetValue("SoftShow", UpDir.FullName);
                model.SoftClass = GetValue("SoftClass", UpDir.FullName);
                model.SoftSpecial = GetValue("SoftSpecial", UpDir.FullName);
                model.SoftChannelIndex = GetValue("SoftChannelIndex", UpDir.FullName);
                model.SoftSpecialIndex = GetValue("SoftSpecialIndex", UpDir.FullName);

                model.ID = "0";

                string strErr = null;
                new TemplatesManage().Add(model, ref strErr);

                if (string.IsNullOrEmpty(strErr) == false)
                {
                    MsgBox.Alert(strErr.ToString(), UpdatePanel1);

                }
                else
                {
                    Response.Redirect("HtmlTemplateList.aspx");

                }
            }
        }

        private string GetValue(string Key, string FullName)
        {
            FullName = FullName.Replace(Server.MapPath(@"~\"), "");

            return XML.GetConfig(Key, FullName + @"\info.config");
        }
    }
}