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
{
    [Produces("application/json;charset=UTF-8")]
    [Route("api/SafeQualityCheck")]
    public class SafeQualityCheckController : BaseController
    {
        /// <summary>
        /// 质量安全检查列表获取
        /// </summary>
        /// <param name="checkType">检查类型</param>
        /// <param name="searchValue">搜索条件</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSafeQualityCheckDTOs")]
        public ActionResult GetSafeQualityCheckDTOs(int type, string searchValue)
        {
            var manager = new SafeQualityCheckManager();
            string checkType = "QualityCheck";
            if (type == 1)
            {
                checkType = "SafeCheck";
            }
            var list = manager.GetSafeQualityCheckDTOs(checkType, searchValue);
            return Json(list);
        }

        /// <summary>
        /// 获取质量安全检查实例及明细
        /// </summary>
        /// <param name="checkCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSafeQualityCheckDTO")]
        public ActionResult GetSafeQualityCheckDTO(string checkCode)
        {
            var manager = new SafeQualityCheckManager();

            var entity = manager.GetSafeQualityCheckDTO(checkCode);
            var list = manager.GetSafeQualityCheckItems(checkCode);
            var attachments = new RequisitionManager().GetAttachMents(checkCode, entity.CheckType);
            var data = new
            {
                entity = entity,
                list = list,
                attachments = attachments
            };
            return Json(data);
        }
        /// <summary>
        /// 插入质量安全检查
        /// </summary>
        /// <param name="sc"></param>
        /// <param name="si"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertSafeQualityCheck")]
        public ActionResult InsertSafeQualityCheck([FromBody] PostData data)
        {
            logger.Info(JsonConvert.SerializeObject(data));
            //return Json("");
            var manager = new SafeQualityCheckManager();
            var list = manager.InsertSafeQualityCheck(data.SafeQualityCheck, data.ItemList);
            return Json(list);
        }
        /// <summary>
        /// 更新质量安全检查
        /// </summary>
        /// <param name="sc"></param>
        /// <param name="si"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateSafeQualityCheck")]
        public ActionResult UpdateSafeQualityCheck([FromBody] PostData data)
        {
            logger.Info(JsonConvert.SerializeObject(data));
            //return Json("");
            var manager = new SafeQualityCheckManager();
            var checkcode = manager.UpdateSafeQualityCheck(data.SafeQualityCheck, data.ItemList);
            return Json(checkcode);
        }
    }

    public class PostData
    {
        public SafeQualityCheck SafeQualityCheck { get; set; }
        public List<SafeQualityCheckItem> ItemList { get; set; }
    }
}
