using NLog;
using Org.BouncyCastle.Bcpg;
using System;
using WebAPIBase.API.Requests;
using WebAPIBase.Service.Interface;
using Newtonsoft.Json;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net;
using Sugar.Enties;
using System.Text;
using WebAPIBase.Utils;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Collections.Specialized;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Authorization;
using System.Drawing;
using Microsoft.AspNetCore.Http;
using System.Linq.Expressions;

namespace WebAPIBase.API.Controllers
{
    /// <summary>
    /// 待审签证
    /// </summary>
    [Produces("application/json;charset=UTF-8")]
    [Route("api/Vise")]
    public class ViseController : BaseController
    {


        /// <summary>
        /// 获取待审签证列表
        /// </summary>
        /// <param name="projectCode"></param>
        /// <param name="searchValue"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetVises")]
        public ActionResult GetVises(string projectCode, string searchValue = "")
        {
            var manager = new ViseManager();
            var list = manager.GetVises(projectCode, searchValue);
            return Json(list);
        }

        /// <summary>
        /// 获取待审签证详情
        /// </summary>
        /// <param name="projectCode"></param>
        /// <param name="searchValue"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetVise")]
        public ActionResult GetVise(string viseCode)
        {
            var manager = new ViseManager();
            var entity = manager.GetVise(viseCode);
            var contract = new ContractManager().GetById(entity.ContractCode);
            if (contract != null)
            {
                entity.SupplierName = new MaterialInManager().GetSupplierNameAndType(contract.SupplierCode, contract.SupplierTypeCode);
            }
            entity.PersonName = new SystemUserManager().GetById(entity.Person)?.UserName;
            entity.RequisitionName = new RequisitionManager().GetById(entity.RequisitionCode)?.RequisitionName;

            var manager1 = new UnitManager();
            var list = new ViseManager().GetViseCostDTOs(viseCode);
            foreach (var item in list)
            {
                item.CostCodeName = manager1.GetCostCodeName(item.CostCode, item.CostBudgetSetCode);
            }
            var attachments = new RequisitionManager().GetAttachMents(viseCode, "Vise");
            var data = new
            {
                mainEntity = entity,
                subList = list,
                attachments = attachments
            };
            logger.Info($"【GetVise】data:{JsonConvert.SerializeObject(data)}");
            return Json(data);
        }

        /// <summary>
        /// 待审核签证费用明细
        /// </summary>
        /// <param name="viseCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetViseCostDTOs")]
        public ActionResult GetViseCostDTOs(string viseCode)
        {
            var manager = new UnitManager();
            var list = new ViseManager().GetViseCostDTOs(viseCode);
            foreach (var item in list)
            {
                item.CostCodeName = manager.GetCostCodeName(item.CostCode, item.CostBudgetSetCode);
            }
            return Json(list);
        }

        /// <summary>
        /// 获取合同明细
        /// </summary>
        /// <param name="contractCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetContractCostDTOs")]
        public ActionResult GetContractCostDTOs(string contractCode)
        {
            var unitManager = new UnitManager();
            var manager = new ViseManager();
            var list = manager.GetContractCostDTOs(contractCode);
            foreach (var item in list)
            {
                item.CostCodeName = unitManager.GetCostCodeName(item.CostCode, item.CostBudgetSetCode);
            }
            return Json(list);
        }
        /// <summary>
        /// 插入待审核签证主表和明细表
        /// </summary>
        /// <param name="vise"></param>
        /// <param name="viseCosts"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertVise")]
        public ActionResult InsertVise([FromBody] ViseUnion viseUnion)
        {

            logger.Info($"【InsertVise】viseUnion:{JsonConvert.SerializeObject(viseUnion)}");
            var manager = new ViseManager();
            var viseCode = manager.InsertVise(viseUnion.vise, viseUnion.viseCosts);
            return Json(viseCode);
        }

        /// <summary>
        /// 更新待审核签证主表和明细表
        /// </summary>
        /// <param name="viseUnion"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateVise")]
        public ActionResult UpdateVise([FromBody] ViseUnion viseUnion)
        {
            logger.Info($"【UpdateVise】viseUnion:{JsonConvert.SerializeObject(viseUnion)}");
            var manager = new ViseManager();
            var viseCode = manager.UpdateVise(viseUnion.vise, viseUnion.viseCosts);
            return Json(viseCode);
        }

        [HttpGet]
        [Route("GetViseEdit")]
        public ActionResult GetViseEdit(string viseCode)
        {
            var manager = new ViseManager();
            var entity = manager.GetVise(viseCode);
            var contract = new ContractManager().GetById(entity.ContractCode);
            if (contract != null)
            {
                entity.SupplierName = new MaterialInManager().GetSupplierNameAndType(contract.SupplierCode, contract.SupplierTypeCode);
            }
            entity.PersonName = new SystemUserManager().GetById(entity.Person)?.UserName;
            entity.RequisitionName = new RequisitionManager().GetById(entity.RequisitionCode)?.RequisitionName;

            var manager1 = new UnitManager();
            var list = new ViseManager().GetViseCostDTOs(viseCode);
            foreach (var item in list)
            {
                item.CostCodeName = manager1.GetCostCodeName(item.CostCode, item.CostBudgetSetCode);
            }
            var attachments = new RequisitionManager().GetAttachMents(viseCode, "Vise");
            var data = new
            {
                mainEntity = entity,
                subList = list,
                attachments = attachments
            };
            logger.Info($"【GetViseEdit】data:{JsonConvert.SerializeObject(data)}");
            return Json(data);
        }

    }

    public class ViseUnion
    {
        public Vise vise { get; set; }
        public List<ViseCost> viseCosts { get; set; }
    }
}
