
using PageAdmin.Common;using PageAdmin.Utils.ORM;using PageAdmin.Utils;
using PageAdmin.Web.Filter;
using System.Collections.Generic;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.E.Controllers
{
    public class SelectDataController:BaseController
    {
        private CurrentUser _CurrentUser;
        SelectData _Bll;
        public SelectDataController()
        {
            _Bll= new SelectData();
        }


        protected override void OnActionExecuting(ActionExecutingContext context)
        {
            _CurrentUser = ViewBag.CurrentUser;
        }

        /// <summary>
        /// 根据id等参数获取数据并填充到select
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public JsonResult LoadData()
        {
            IEnumerable<dynamic> data = _Bll.LoadList(Request.QueryString, _CurrentUser);
            return Json(data);
        }

        /// <summary>
        /// 表格不分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public JsonResult ListData()
        {
            IEnumerable<dynamic> data = _Bll.ListData(Request.QueryString, _CurrentUser);
            return Json(data);
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public virtual JsonResult PageListData()
        {
            var form = Request.QueryString;
            PageInfo pageInfo = new PageInfo();
            IEnumerable<dynamic> data = _Bll.PageListData(Request.QueryString,pageInfo, _CurrentUser);
            return Json(data,pageInfo);
        }

        [HttpGet]
        //选择数据页面
        public ActionResult Select()
        {
            int fieldId = StringHelper.Format<int>(Request.QueryString["fieldId"]);
            if(fieldId<=0)
            {
                return Content("无效的参数！");
            }
            dynamic fieldData=_Bll.GetField(fieldId);
            ViewBag.FieldId = fieldId;
            string datasourceShowtype = fieldData.DatasourceShowType.ToString();
            string viewPath = "";
            switch (datasourceShowtype)
            {
                case "1":
                    viewPath = "List";
                    break;

                case "2":
                    viewPath = "PageList";
                    break;
            }
            ViewBag.ValueField = StringHelper.FirstCharUpper(fieldData.DatasourceValueField);
            ViewBag.TextField = StringHelper.FirstCharUpper(fieldData.DatasourceTextField);
            ViewBag.FieldName =StringHelper.FirstCharUpper(fieldData.Name);
            ViewBag.FieldType = fieldData.FieldType;

            ViewBag.TextfieldName = fieldData.DatasourceTextFieldName;
            ViewBag.IsTree = fieldData.DatasourceMultiLevel ==1? "true" : "false";
            ViewBag.OnlySelectFinal = fieldData.DatasourceOnlySelectFinal == 1 ? "true" : "false";
            return View(viewPath);
        }

    }
}