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

namespace WebAPI.Controllers.TestRun
{
    public class DriverRunController : ApiController
    {
        //
        // 质量巡检
        // GET: /Draw/
        [HttpGet]
        public ResponseData<List<DriverRun_DriverRun>> Index(string projectId, int index, int page, string state)
        {
            ResponseData<List<DriverRun_DriverRun>> res = new ResponseData<List<DriverRun_DriverRun>>();

            res.successful = true;
            res.resultValue = BLL.DriverRunService.GetListDataForApi(state, projectId, index, page);
            return res;
        }

        [HttpGet]
        public ResponseData<List<DriverRun_DriverRun>> Search(string state, string projectId, int index, int page)
        {
            ResponseData<List<DriverRun_DriverRun>> res = new ResponseData<List<DriverRun_DriverRun>>();

            res.successful = true;
            res.resultValue = BLL.DriverRunService.GetListDataForApi(state, projectId, index, page);
            return res;
        }

        /// <summary>
        /// 根据code获取详情
        /// </summary>
        /// <param name="DriverRunId"></param>
        /// <returns></returns>
        public ResponseData<DriverRun_DriverRun> GetDriverRun(string code)
        {
            ResponseData<DriverRun_DriverRun> res = new ResponseData<DriverRun_DriverRun>();
            DriverRun_DriverRun DriverRun = BLL.DriverRunService.GetDriverRunForApi(code);
            res.successful = true;
            res.resultValue = BeanUtil.CopyOjbect<DriverRun_DriverRun>(DriverRun, true);
            return res;
        }
        /// <summary>
        /// 根据code获取 审核记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ResponseData<List<DriverRun_DriverRunApprove>> GetApproveByCode(string code)
        {
            ResponseData<List<DriverRun_DriverRunApprove>> res = new ResponseData<List<DriverRun_DriverRunApprove>>();

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

            res.successful = true;
            res.resultValue = BeanUtil.CopyOjbect<DriverRun_DriverRunApprove>(BLL.DriverRunApproveService.getCurrApproveForApi(code), true);
            return res;
        }
        [HttpPost]
        public ResponseData<string> AddDriverRun([FromBody]Model.DriverRun_DriverRun DriverRun)
        {
            ResponseData<string> res = new ResponseData<string>();
            try
            {
                if (string.IsNullOrEmpty(DriverRun.DriverRunId))
                {
                    DriverRun.DriverRunId = Guid.NewGuid().ToString();
                    BLL.DriverRunService.AddDriverRunForApi(DriverRun);
                    SaveAttachFile(DriverRun.DriverRunId, BLL.Const.CheckListMenuId, DriverRun.AttachUrl);
                    res.resultValue = DriverRun.DriverRunId;
                }
                else
                {
                    BLL.DriverRunService.UpdateDriverRunForApi(DriverRun);
                    SaveAttachFile(DriverRun.DriverRunId, BLL.Const.CheckListMenuId, DriverRun.AttachUrl);
                    res.resultValue = DriverRun.DriverRunId;
                }

                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.DriverRun_DriverRunApprove approve)
        {
            ResponseData<string> res = new ResponseData<string>();
            try
            {

                Model.DriverRun_DriverRun DriverRun = new Model.DriverRun_DriverRun();
                DriverRun.DriverRunId = approve.DriverRunId;
                DriverRun.State = approve.ApproveType;
                BLL.DriverRunService.UpdateDriverRunForApi(DriverRun);
                res.resultValue = BLL.DriverRunApproveService.AddDriverRunApproveForApi(approve);
                res.successful = true;

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

        }
        [HttpPost]
        public ResponseData<string> UpdateApprove([FromBody]Model.DriverRun_DriverRunApprove approve)
        {
            ResponseData<string> res = new ResponseData<string>();
            try
            {
                approve.ApproveDate = DateTime.Now;
                BLL.DriverRunApproveService.UpdateDriverRunApproveForApi(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.DriverRunApproveService.See(dataId, userId);
            return res;
        }
    }
}