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

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class EntitiesController : DefaultBaseController
    {
        EntitiesBLL Bll;
        public EntitiesController() : base("", "System/Table/")
        {
            Bll = new EntitiesBLL();
            base.MvcContext.ListOrder = "thedate desc";
            base.IPageListBll = Bll;
        }

        /// <summary>
        /// 执行完毕后清理缓存
        /// </summary>
        /// <param name="context"></param>
        protected override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.RequestContext.HttpContext.Request.HttpMethod == "POST")
            {
                string table = Request.QueryString["table"];
                CacheHelper.Remove("/Entities/"+ table+ ".config");
            }
            base.OnActionExecuted(context);
        }


        private void CheckTable(string table)
        {
            if (!StringHelper.IsStr(table))
            {
                throw new Exception("无效的table参数!");
            }
        }

        [HttpGet]
        public ActionResult Set()
        {
            string table = Request.QueryString["table"];
            CheckTable(table);
            return View();
        }


        [HttpPost]
        public override JsonResult Add(FormCollection form)
        {
            string table = form["name"];
            CheckTable(table);
            var tipsInfo = Bll.Add(table, form);
            return Json(tipsInfo,JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public override JsonResult Delete(FormCollection form)
        {
            string table = form["table"];
            CheckTable(table);
            var tipsInfo = Bll.Delete(table);
            return Json(tipsInfo);
        }

        [HttpGet]
        public override ActionResult Edit()
        {
            string table = Request.QueryString["table"];
            CheckTable(table);
            var detailData = JsonHelper.JsonParse(Bll.Detail(table));
            return View("Add",(object)detailData);
        }

        [AjaxOnly]
        [HttpPost]
        public override JsonResult Edit(FormCollection form)
        {
            string table = form["name"];
            CheckTable(table);
            var tipsInfo = Bll.Edit(table, form);
            return Json(tipsInfo);
        }

        [HttpGet]
        public JsonResult FieldList()
        {
            string table = Request.QueryString["table"];
            CheckTable(table);
            string jsonData = JsonHelper.JsonParse(Bll.GetFields(table));
            return Json(jsonData,JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult FieldUpdate(FormCollection form)
        {
            string table = Request.QueryString["table"];
            CheckTable(table);
            var tipsInfo = Bll.FieldUpdate(table, form);
            return Json(tipsInfo);
        }


    }
}