using PageAdmin.BLL;
using PageAdmin.Common;using PageAdmin.Utils.ORM;using PageAdmin.Utils;
using PageAdmin.Web.Filter;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
   [CurrentSite]
    public class TemplateManagerController: FileManageBaseController
    {
        public TemplateManagerController():base("/Templates/", "Website")
        {
            base.FuncFilter = List => List.OrderBy(c => c.Type);
        }

        protected override void OnActionExecuting(ActionExecutingContext context)
        {
            GetCurrentSiteTemplateDir(context);
            base.OnActionExecuting(context);
        }

        [NonAction]
        private void GetCurrentSiteTemplateDir(ActionExecutingContext context)
        {
            int siteId = (int)ViewBag.CurrentSiteId;
            SiteBLL siteBll = new SiteBLL();
            string siteTemplateDir = ViewBag.CurrentTemplateDirectory;
            if (!string.IsNullOrEmpty(siteTemplateDir))
            {
                siteTemplateDir += "/";
            }
            base.RootPath += siteTemplateDir; //重写跟目录，根据站点设置的模板
            ViewBag.RootPath = RootPath;
        }


        /// <summary>
        /// 获取模板目录
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpGet]
        [NotCheckMenuItemPermissions]
        public JsonResult TemplatesDirectoryList()
        {
            IEnumerable<dynamic> data =IOHelper.DirectoryList("/Templates/").Where(c=>c.Type==0).Select(c=>new { Name = c.Name });
            return Json(data);
        }


        /// <summary>
        /// 选择模板目录界面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NotCheckMenuItemPermissions]
        public ActionResult SelectTemplate()
        {
            return View();
        }

        /// <summary>
        /// 选择模板界面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NotCheckMenuItemPermissions]
        public ActionResult select()
        {
            ViewBag.CurrentPath = StringHelper.Format<string>(Request.QueryString["path"]);
            return View();
        }
    }
}