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

namespace PageAdmin.Web.Areas.Member.Controllers
{

    /// <summary>
    /// 自定义数据的基类
    /// </summary>
    public class CustomDataBaseController : DefaultBaseController
    {
        protected string Table;
        protected int TableId;
        public CustomDataBaseController(string table, string directory = "") : base(table, directory)
        {
            if (table == "")
            {
                table = System.Web.HttpContext.Current.Request.QueryString["table"];
                base.MvcContext.Table = table;
                if (!StringHelper.IsStr(table))
                {
                    throw new Exception("无效的table参数!");
                }
            }
        }

        [NonAction]
        public void CheckTable()
        {
            string table = base.MvcContext.Table;
            TableTypeEnum tableType = base.MvcContext.TableType;
            string saveTable = "";
            if (tableType == TableTypeEnum.EntityTable)
            {
                ViewBag.Table = table;
                return;
            }
            if (tableType == TableTypeEnum.CustomInfoTable)
            {
                saveTable = "pa_info_table";
            }
            else if (tableType == TableTypeEnum.CustomCategoryTable)
            {
                saveTable = "pa_category_table";
            }
            else if (tableType == TableTypeEnum.CustomSubTable)
            {
                saveTable = "pa_sub_table";
            }
            else
            {
                return;
            }
            DataBaseContext dbContext = DbHelper.DbContext();
            dbContext.SetDbParamter(new { name = table });
            dynamic data = dbContext.QueryFirst(saveTable, "remark,id", "name=@name");
            if (data != null)
            {
                ViewBag.TableRemark = data.remark;
                ViewBag.Table = table;
                TableId = data.id;
                Table = table;
                ViewBag.TableId = TableId;
            }
            else
            {
                throw new Exception("表" + table + "不存在!");
            }
        }

        private void LoadField(string pageType)
        {
            ViewBag.FieldData = new FieldBLL().GetFieldList(MvcContext.Table, pageType, MvcContext.CurrentUser);
        }

        [HttpGet]
        public override ActionResult Index()
        {
            LoadField("list");
            return base.Index();
        }

        [HttpGet]
        public override ActionResult Add()
        {
            LoadField("add");
            //ViewBag.PostType = "add";
            ViewBag.DetailId = 0;
            ViewBag.Guid = SysHelper.NewGuid();
            return base.Add();
        }

        [HttpGet]
        public override ActionResult Edit()
        {
            LoadField("edit");
            //ViewBag.PostType = "edit";
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            string guid = StringHelper.Format<string>(Request.QueryString["guid"]);
            ViewBag.DetailId = id;
            ViewBag.Guid = guid;
            return base.Edit();
        }

    }
}