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

namespace PageAdmin.Web.Areas.E.Views.SubData.Filter
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true)]
    public class CheckParamsAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            NameValueCollection request;
            if (filterContext.HttpContext.Request.HttpMethod == "POST")
            {
                request = filterContext.HttpContext.Request.Form;
            }
            else
            {
                request = filterContext.HttpContext.Request.QueryString;
            }
            string thefrom = StringHelper.Format<string>(request["_from"]);
            int fieldId = StringHelper.Format<int>(request["_fieldId"]);
            int parentId= StringHelper.Format<int>(request["parentId"]);
            filterContext.Controller.ViewBag.ParentId = parentId;

            if (fieldId == 0)
            {
                Error(filterContext, "无效的_fieldId参数！");
                return;
            }
            FieldBLL fieldBll = new FieldBLL();
            dynamic fieldData = fieldBll.GetData(fieldId);
            if (fieldData == null)
            {
                Error(filterContext, "_fieldId参数不正确！");
                return;
            }

            CheckParentId(filterContext, fieldData.TableName, parentId);
         
            ///post时检测detailId

            filterContext.Controller.ViewBag.FieldData = fieldData;
            string fieldType = fieldData.FieldType;
            if (fieldType!="subtable")
            {
                Error(filterContext, "_fieldId对应的fieldType类型不匹配！");
                return;
            }
            SubTableBLL subtTableBLL = new SubTableBLL();
            string subTable = subtTableBLL.GetName(fieldData.TableName, fieldData.Name);



            if (string.IsNullOrEmpty(subTable))
            {
                Error(filterContext, "找不到附属表！");
                return;
            }
            filterContext.Controller.ViewBag.FieldId= fieldId;
            filterContext.Controller.ViewBag.Table = subTable;
            CurrentUser user = filterContext.Controller.ViewBag.CurrentUser;
            FromEnum from = FromEnum.Guest;
            if (thefrom == "admin" && user.RoleId > 0)
            {
                from = FromEnum.Admin;
            }
            else if (thefrom == "member" && user.Uid > 0)
            {
                from = FromEnum.Member;
            }
            filterContext.Controller.ViewBag.From = from;
        }

        //检测detailId是否正确
        private void CheckParentId(ActionExecutingContext filterContext,string parentTable,int parentId)
        {
            if(filterContext.HttpContext.Request.HttpMethod=="POST")
            {
                if (filterContext.ActionDescriptor.ActionName.Equals("Delete", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
                if(parentId==0)
                {
                    return;
                }
                DataBaseContext dbContext = DbHelper.DbContext();
                bool isExists=dbContext.IsExists(parentTable, "id", parentId);
                if(!isExists)
                {
                    Error(filterContext, "parentId参数错误，或父级数据已经被删除");
                }
            } 
        }

       private void Error(ActionExecutingContext filterContext, string content)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = new JsonResult
                {
                    ContentType = "application/json",
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = JsonHelper.JsonParse(new { State = 0, Msg = content })
                };
            }
            else
            {
                filterContext.Result = new ContentResult
                {
                    ContentType = "text/html",
                    Content = content
                };
            }

        }
    }
}