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

namespace PageAdmin.Web.Areas.E.Controllers
{

    /// <summary>
    /// 区域A控制器的基类
    /// </summary>
    public class BaseController : Controller
    {
        private string _ClassifyViewDirectory;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="viewDirectory">视图的上级目录结构，相对于控制器</param>
        public BaseController()
        {
            _ClassifyViewDirectory = null;
        }
        public BaseController(string classifyviewDirectory)
        {
            _ClassifyViewDirectory = classifyviewDirectory;
        }

        protected override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!string.IsNullOrEmpty(_ClassifyViewDirectory))
            {
                RouteData.Values["classifyViewDirectory"] = _ClassifyViewDirectory;
            }
        }

        /// <summary>
        /// 重写json方法
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected new JsonResult Json(object data)
        {
            var jsonResult = new JsonResult()
            {
                Data = JsonHelper.JsonParse(data),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                ContentType = "application/json;charset=utf-8",
            };
            return jsonResult;
        }

        /// <summary>
        /// 添加分页json
        /// </summary>
        /// <param name="data"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        protected JsonResult Json(object data, PageInfo pageInfo)
        {
            string strData = JsonHelper.JsonParse(data);
            if (strData == "")
            {
                strData = "\"\"";
            }
            string strJson = "{\"pageInfo\":{\"pageCount\":" + pageInfo.PageCount + ",\"recordCount\":" + pageInfo.RecordCount + ",\"pageSize\":" + pageInfo.PageSize + ",\"page\":" + pageInfo.CurrentPage + "},\"data\":";
            strJson += strData;
            strJson += "}";
            var jsonResult = new JsonResult()
            {
                Data = strJson,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                ContentType = "application/json;charset=utf-8",
            };
            return jsonResult;
        }

    }
}