using System.Web.Http;
using Model;
using System.Collections.Generic;
using System;
using BLL;
using System.Linq;

namespace Mvc.Controllers
{
    public class CheckListController : ApiController
    {
        //
        // 质量巡检
        // GET: /Draw/
        [HttpGet]
        public ResponseData<List<Check_CheckControl>> Index(string projectId, int index, int page, string state, string name)
        {
            ResponseData<List<Check_CheckControl>> res = new ResponseData<List<Check_CheckControl>>();
            try
            {res.successful = true;
            res.resultValue = BLL.CheckControlService.GetListDataForApi(state, name, projectId, index, page);

            }
            catch (Exception e)
            {
                res.successful = false;
                res.message = e.Message;
            }
            
            return res;
        }

        [HttpGet]
        public ResponseData<string> Conut(string projectId, string searchWord, string unitId = null, string unitWork = null, string problemType = null, string professional = null, string dateA = null, string dateZ = null)
        {
            ResponseData<string> res = new ResponseData<string>();
            res.successful = true;
            res.resultValue = BLL.CheckControlService.GetListCountStr(projectId, searchWord, unitId, unitWork, problemType, professional, dateA, dateZ);
            return res;
        }
        [HttpGet]
        public ResponseData<List<Check_CheckControl>> Search(string state, string projectId, int index, int page, string unitId = null, string unitWork = null, string problemType = null, string professional = null, string dateA = null, string dateZ = null)
        {
            ResponseData<List<Check_CheckControl>> res = new ResponseData<List<Check_CheckControl>>();

            res.successful = true;
            res.resultValue = BLL.CheckControlService.GetListDataForApi(state, unitId, unitWork, problemType, professional,dateA, dateZ, projectId, index, page);
            return res;
        }

        /// <summary>
        /// 根据code获取详情
        /// </summary>
        /// <param name="CheckControlCode"></param>
        /// <returns></returns>
        public ResponseData<Check_CheckControl> GetCheckControl(string code)
        {
            ResponseData<Check_CheckControl> res = new ResponseData<Check_CheckControl>();
            Check_CheckControl checkControl = BLL.CheckControlService.GetCheckControlForApi(code);
            res.successful = true;
            res.resultValue = BeanUtil.CopyOjbect<Check_CheckControl>(checkControl, true);
            return res;
        }
        /// <summary>
        /// 根据code获取 审核记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ResponseData<List<Check_CheckControlApprove>> GetApproveByCode(string code)
        {
            ResponseData<List<Check_CheckControlApprove>> res = new ResponseData<List<Check_CheckControlApprove>>();

            res.successful = true;
            res.resultValue = BLL.CheckControlApproveService.GetListDataByCodeForApi(code);
            return res;
        }
        public ResponseData<Check_CheckControlApprove> GetCurrApproveByCode(string code)
        {
            ResponseData<Check_CheckControlApprove> res = new ResponseData<Check_CheckControlApprove>();

            res.successful = true;
            res.resultValue = BeanUtil.CopyOjbect<Check_CheckControlApprove>(BLL.CheckControlApproveService.getCurrApproveForApi(code), true);
            return res;
        }
        [HttpPost]
        public ResponseData<string> AddCheckControl([FromBody]Model.Check_CheckControl CheckControl)
        {
            ResponseData<string> res = new ResponseData<string>();
            try
            {
                if (string.IsNullOrEmpty(CheckControl.CheckControlCode))
                {
                    if (string.IsNullOrEmpty(CheckControl.DocCode))
                    {
                        string prefix = BLL.ProjectService.GetProjectByProjectId(CheckControl.ProjectId).ProjectCode + "-QC-XJYJ-XJ-";
                        CheckControl.DocCode = BLL.SQLHelper.RunProcNewId("SpGetNewCode5ByProjectId", "dbo.Check_CheckControl", "DocCode",CheckControl.ProjectId, prefix);
                    }
                    CheckControl.CheckControlCode = Guid.NewGuid().ToString();
                    BLL.CheckControlService.AddCheckControlForApi(CheckControl);
                    //BLL.AttachFileService.updateAttachFile(CheckControl.ReAttachUrl, CheckControl.CheckControlCode+"r", Const.CheckListMenuId);
                    //BLL.AttachFileService.updateAttachFile(CheckControl.AttachUrl, CheckControl.CheckControlCode, Const.CheckListMenuId);
                    SaveAttachFile(CheckControl.CheckControlCode + "r", BLL.Const.CheckListMenuId, CheckControl.ReAttachUrl);
                    SaveAttachFile(CheckControl.CheckControlCode, BLL.Const.CheckListMenuId, CheckControl.AttachUrl);
                    res.resultValue = CheckControl.CheckControlCode;
                }
                else
                {
                    BLL.CheckControlService.UpdateCheckControlForApi(CheckControl);
                    //BLL.AttachFileService.updateAttachFile(CheckControl.ReAttachUrl, CheckControl.CheckControlCode + "r", Const.CheckListMenuId);
                    //BLL.AttachFileService.updateAttachFile(CheckControl.AttachUrl, CheckControl.CheckControlCode, Const.CheckListMenuId);
                    SaveAttachFile(CheckControl.CheckControlCode + "r", BLL.Const.CheckListMenuId, CheckControl.ReAttachUrl);
                    SaveAttachFile(CheckControl.CheckControlCode, BLL.Const.CheckListMenuId, CheckControl.AttachUrl);
                    res.resultValue = CheckControl.CheckControlCode;
                }

                res.successful = true;

            }
            catch (Exception e)
            {
                res.resultHint = e.StackTrace;
                res.successful = false;
            }
            return res;

        }

        /// <summary>
        /// 
        /// </summary>
        public static void SaveAttachFile(string dataId, string menuId, string url)
        {
            Model.ToDoItem toDoItem = new Model.ToDoItem
            {
                MenuId = menuId,
                DataId = dataId,
                UrlStr = url,
            };
            APIUpLoadFileService.SaveAttachUrl(toDoItem);
        }

        [HttpPost]
        public ResponseData<string> AddApprove([FromBody]Model.Check_CheckControlApprove approve)
        {
            ResponseData<string> res = new ResponseData<string>();
            try
            {

                Model.Check_CheckControl CheckControl = new Model.Check_CheckControl();
                CheckControl.CheckControlCode = approve.CheckControlCode;
                CheckControl.State=approve.ApproveType;
                BLL.CheckControlService.UpdateCheckControlForApi(CheckControl);

                if (approve.ApproveType != "1" && approve.ApproveType != "7")
                {

                    var userid = Request.Headers.TryGetValues("token", out IEnumerable<string> token);
                    var user =  BLL.APIUserService.getUserByUserId(token.FirstOrDefault());
                    APICommonService.SendSubscribeMessage(approve.ApproveMan, "质量巡检问题待办理", user.UserName, string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));

                }
                res.resultValue = BLL.CheckControlApproveService.AddCheckControlApproveForApi(approve);
                res.successful = true;

            }
            catch (Exception e)
            {
                res.resultHint = e.StackTrace;
                res.successful = false;
            }
            return res;

        }
        [HttpPost]
        public ResponseData<string> UpdateApprove([FromBody]Model.Check_CheckControlApprove approve)
        {
            ResponseData<string> res = new ResponseData<string>();
            try
            {
                // Model.Check_CheckControlApprove approve1 = BLL.CheckControlApproveService.GetCheckControlApproveByCheckControlId(approve.CheckControlCode);
                
                approve.ApproveDate = DateTime.Now;
                BLL.CheckControlApproveService.UpdateCheckControlApproveForApi(approve);
                res.successful = true;

            }
            catch (Exception e)
            {
                res.resultHint = e.StackTrace;
                res.successful = false;
            }
            return res;

        }
        // GET: /Draw/
        [HttpGet]
        public ResponseData<string> see(string dataId, string userId)
        {
            ResponseData<string> res = new ResponseData<string>();
            res.successful = true;
            BLL.CheckControlApproveService.See(dataId, userId);
            return res;
        }
    }
}