
using PageAdmin.Common;using PageAdmin.Utils.ORM;using PageAdmin.Utils;
using PageAdmin.Web.Filter;
using PageAdmin.Web.Areas.E.Views.SubData;
using PageAdmin.Web.Areas.E.Views.SubData.Filter;
using System.Collections.Generic;
using System.Web.Mvc;
using PageAdmin.BLL;

namespace PageAdmin.Web.Areas.E.Controllers
{

    [CheckParams]
    public class SubDataController : BaseController
    {
        private MvcControllerContext MvcContext{set; get; }
        public SubDataController()
        {
            MvcContext = new MvcControllerContext();
            MvcContext.IncludeCustomField = true;
            MvcContext.IncludeAttachments = true;
            MvcContext.TableType = TableTypeEnum.CustomSubTable;
        }

        private void Init()
        {
            MvcContext.Table = ViewBag.Table;
            MvcContext.From = ViewBag.From;
            MvcContext.CurrentUser = ViewBag.CurrentUser;
        }


        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [AjaxOnly]
        [HttpGet]
        /// <summary>
        /// 获取分页数据json
        /// </summary>
        /// <param name="parentTable"></param>
        /// <param name="parentField"></param>
        /// <param name="parentGuid"></param>
        /// <param name="parentId"></param>
        /// <param name="order"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public JsonResult PageListData()
        {
            Init();
            MvcContext.Data = Request.QueryString;
            SubData subData = new SubData(MvcContext);
            IEnumerable<dynamic> list = subData.ListData();
            return base.Json(list, MvcContext.PageInfo);
        }


        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }


        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Add(FormCollection form)
        {
            Init();
            MvcContext.Data = form;
            SubData subData = new SubData(MvcContext);
            var tipsInfo=subData.Add();
            return base.Json(tipsInfo);
        }


        [HttpGet]
        public ActionResult Edit()
        {
            Init();
            MvcContext.Data = Request.QueryString;
            SubData subData = new SubData(MvcContext);
            dynamic detailData = subData.Detail();
            return View("Add",(object)JsonHelper.JsonParse(detailData));
        }

        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Edit(FormCollection form)
        {
            Init();
            MvcContext.Data = form;
            SubData subData = new SubData(MvcContext);
            var tipsInfo = subData.Edit();
            return base.Json(tipsInfo);
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        [HttpPost]
        [AjaxOnly]
        public JsonResult Delete(FormCollection form)
        {
            Init();
            MvcContext.Data = form;
            SubData subData = new SubData(MvcContext);
            var tipsInfo = subData.Delete();
            return base.Json(tipsInfo);
        }


    }

}