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

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    [CheckTable(TableType = TableTypeEnum.CustomSubTable)]
    public class SubTableDataAdminController : DefaultBaseController
    {
        public SubTableDataAdminController(): base("", "System/Table/SubTable/")
        {
            base.MvcContext.TableType = TableTypeEnum.CustomSubTable;
            base.MvcContext.IncludeCustomField = true;
            base.MvcContext.IncludeAttachments = true;
            base.ListDataExecuting = GetWhere;
            base.EditViewName = "Edit";
        }


        //构造查询条件
        public void GetWhere()
        {
            var form = Request.QueryString;
            string where = "";
            string currentState = form["state"];
            if (!string.IsNullOrEmpty(currentState))
            {
                if (StringHelper.IsInt(currentState))
                {
                    where += "[state]=" + currentState;
                }
            }
            base.MvcContext.SqlCondition = where;
        }


        /// <summary>
        /// 清理无效的内容
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult ClearData(FormCollection form)
        {
            MvcContext.Data = form;
            SubTableDataBLL Bll = new SubTableDataBLL(MvcContext.Table);
            int state = StringHelper.Format<int>(Request.QueryString["state"]);
            return Json(Bll.ClearData(MvcContext, state));
        }

        /// <summary>
        /// 统计附件表的记录
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpGet]
        public JsonResult GetCount()
        {
            SubTableDataBLL bll = new SubTableDataBLL(base.MvcContext.Table);
            return Json(bll.Count(), JsonRequestBehavior.AllowGet);

        }

    }
}