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

namespace PageAdmin.Web.Areas.E.Controllers
{
    public class AttachmentsDataController:BaseController
    {

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

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

        [AjaxOnly]
        [HttpGet]
        /// <summary>
        /// 获取附件表集合,根据父级表和父级字段,父级guid和父级id
        public JsonResult GetList()
        {
            string parentTable = Request.QueryString["parentTable"];
            string parentField = Request.QueryString["parentField"];
            string parentGuid = Request.QueryString["parentGuid"];
            int parentId =StringHelper.Format<int>(Request.QueryString["parentId"]);
            string order= Request.QueryString["order"];
            if(string.IsNullOrEmpty(order))
            {
                order = "xuhao asc";
            }
            IEnumerable<dynamic> list = new List<dynamic>();
            if(!StringHelper.IsStr(parentTable) || !StringHelper.IsStr(parentField) ||  !StringHelper.IsStr(parentGuid))
            {
                return base.Json(null);
            }
            DataBaseContext dbContext = DbHelper.DbContext();
            AttachmentTableBLL atachmentTableBLL = new AttachmentTableBLL();
            string attachmentTable = atachmentTableBLL.GetName(parentTable, parentField);
            return GetList(attachmentTable, parentGuid, parentId, order);
        }

        private JsonResult GetList(string table, string parentGuid, int parentId, string order = "xuhao asc")
        {
            IEnumerable<dynamic> list = new List<dynamic>();
            if (!StringHelper.IsStr(table) || parentId < 0 || !StringHelper.IsStr(parentGuid))
            {
                return base.Json(null);
            }
            DataBaseContext dbContext = DbHelper.DbContext();
            AttachmentTableBLL atachmentTableBLL = new AttachmentTableBLL();
            if (!atachmentTableBLL.IsExists(table))
            {
                return base.Json(null);
            }
            string condition = "";
            if(parentId<=0)
            {
                dbContext.SetDbParamter(new { parent_id=0,parent_guid = parentGuid, state =0 });
            }
            else
            {
                dbContext.SetDbParamter(new { parent_id=parentId,parent_guid = parentGuid,state=1 });
            }
            condition = "parent_id=@parent_id and parent_guid=@parent_guid and state=@state";
            list = dbContext.Query(table, "*", condition, order);
            return base.Json(list);
        }

        [AjaxOnly]
        [CheckTable]
        [HttpPost]
        public JsonResult UpdateList(FormCollection form)
        {
           DbKeyValueList dic = new DbKeyValueList();
            MvcControllerContext mvcContext = new MvcControllerContext();
            mvcContext.TableType = TableTypeEnum.CustomAttachmentTable;
            mvcContext.Table = ViewBag.Table;
            mvcContext.Data = form;
            dic.Add("title", form["title"]);
            dic.Add("xuhao", form["xuhao"]);
            IUpdateBLL updateBll = new UpdateBLL();
            TipsInfo tipsInfo = updateBll.Update(mvcContext,dic);
            return base.Json(tipsInfo);
        }


        [AjaxOnly]
        [CheckTable]
        [HttpPost]
        public JsonResult Delete(FormCollection form)
        {
            MvcControllerContext mvcContext = new MvcControllerContext();
            mvcContext.TableType = TableTypeEnum.CustomAttachmentTable;
            mvcContext.Table = ViewBag.Table;
            mvcContext.Data = form;
            IDeleteBLL deleteBll = new DeleteBLL();
            TipsInfo tipsInfo= deleteBll.Delete(mvcContext);
            return base.Json(tipsInfo);
        }
    }
}