using DocumentFormat.OpenXml.Drawing.Spreadsheet;
using DocumentFormat.OpenXml.Spreadsheet;
using Learun.Application.Base.SystemModule;
using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Application.TwoDevelopment.ZZDT_EC.ec_lookup;
using Learun.Util;
using Learun.Util.SqlSugar;
using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Asn1.Ocsp;
using Pipelines.Sockets.Unofficial.Arenas;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Windows.Interop;

namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers
{

    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-02-24 11:10
    /// 描 述：lookup
    /// </summary>
    public class ec_LookupTableController : MvcControllerBase
    {
        ec_REFLOOKUPTABLEBLL bll = new ec_REFLOOKUPTABLEBLL();
        #region 获取数据

        /// <summary>
        /// 主页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LookupIndex()
        {
            return View();
        }

        /// <summary>
        /// 表单页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }


        /// <summary>
        /// 表单页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Structure()
        {
            return View();
        }

        /// <summary>
        /// 表单页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult DataDefinition()
        {
            return View();
        }
        /// <summary>
        /// 表单页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult FormStructure()
        {
            return View();
        }
        /// <summary>
        /// 某个类型下的lookup
        /// <param name="objectTypeId">对象类型id</param>
        /// <param name="ProjectId">项目id</param>
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetList(string objectTypeId, string ProjectId, string filter = "")
        {
            var data = bll.GetList(ProjectId);

            data = data.FindAll(x => x.ObjectTypeId == objectTypeId);

            if (!string.IsNullOrEmpty(filter))
            {
                data = data.FindAll(x => x.Name.Contains(filter));
            }
            return Success(data);
        }

        /// <summary>
        /// 某个类型下的lookup
        /// <param name="objectTypeId">对象类型id</param>
        /// <param name="ProjectId">项目id</param>
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetStructure(string spid, string ProjectId)
        {
            var data = bll.GetLookupTableStructure(ProjectId, spid);

            return Success(data);
        }


        /// <summary>
        /// 某个类型下的lookup
        /// <param name="objectTypeId">对象类型id</param>
        /// <param name="ProjectId">项目id</param>
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetAttrData(string PropertyID, string ProjectId, string lookuptableid)
        {
            var data = bll.GetLookupTableStructure(ProjectId, lookuptableid);
            var res = data.FirstOrDefault(x => x.PropertyID == PropertyID);

            return Success(res);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="lookupTableId"></param>
        /// <returns></returns>
        public ActionResult GetDataRecord(string ProjectId, string lookupTableId)
        {
            var res = bll.GetLookupDataRecord(ProjectId, lookupTableId).OrderBy(x => x.recordNumber).ToList();
            var structure = bll.GetLookupTableStructure(ProjectId, lookupTableId);
            var res2 = bll.TransformData(res, structure);
            return Success(res2);
        }
        #endregion
        #region 修改
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveAttr(string ProjectId, string strEntity)
        {
            try
            {
                var entity = strEntity.ToObject<ec_REFLOOKUPTABLEAttr>();
                if (string.IsNullOrEmpty(entity.SP_ID))
                {
                    bll.AddAttr(ProjectId, entity);
                }
                else
                {
                    bll.ModifyAttr(ProjectId, entity);
                }

                return Success("修改成功");
            }
            catch (Exception e)
            {
                return Fail(e.Message);
            }

        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="lookupTableId"></param>
        /// <param name="strEntity"></param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult SaveData(string ProjectId, string lookupTableId, string strEntity)
        {
            List<Dictionary<string, string>> tableData = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(strEntity);
            bll.ModifyData(ProjectId, lookupTableId, tableData);
            return Success("");
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string objectTypeId, string ProjectId, string strEntity)
        {

            var entity = strEntity.ToObject<EC_REFLOOKUPTABLE>();
            entity.ObjectTypeId = objectTypeId;
            var exist = bll.GetList(ProjectId).FindAll(x => x.ObjectTypeId == objectTypeId);
            if (exist.Any(x => x.Name.ToUpper() == entity.Name.ToUpper() && x.SP_ID != entity.SP_ID))
            {
                return Success("不能有重名的！");
            }
            if (string.IsNullOrEmpty(entity.SP_ID))
            {
                bll.AddLookupTable(ProjectId, entity);
                return Success("插入成功");
            }
            else
            {
                bll.ModifyLookupTable(ProjectId, entity);
                return Success("更新成功");
            }

        }

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteForm(string keyValue, string ProjectId)
        {
            bll.DeleteEntity(keyValue, ProjectId);
            return Success("删除成功！");
        }

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteAttr(string keyValue, string ProjectId)
        {
            bll.DeleteEntity(keyValue, ProjectId);
            return Success("删除成功！");
        }

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteData(string ProjectId, string lookupTableId, string strEntity)
        {
            Dictionary<string, string> tableData = JsonConvert.DeserializeObject<Dictionary<string, string>>(strEntity);
            bll.DeleteData(lookupTableId, tableData, ProjectId);
            return Success("删除成功！");
        }
        #endregion
    }
}
