using AntDesign;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.web.Services;

namespace wispro.sp.web.Pages.Project
{
    public partial class MyProject
    {
        List<ProjectContentRecord> ProjectInfos;
        int _pageIndex = 1;
        int _pageSize = 10;
        int _total = 0;
        bool _loading = false;

        private ModalRef _modalRef;

        [Inject] protected IProjectService _itemService { get; set; }
        [Inject] public MessageService MsgSvr { get; set; }
        [Inject] NavigationManager _NavigationManager { get; set; }

        [Inject] ModalService _ModalService { get; set; }

        [Inject] MessageService _msgService { get; set; }


        private string getStatus(int state)
        {
            switch (state)
            {
                case 0:
                    return "未提交";
                    break;
                case 1:
                    return "待审核";
                    break;
                case 2:
                    return "已审核";
                    break;
                default:
                    return "";
            }
        }
        protected async override Task OnInitializedAsync()
        {
            ProjectInfos = await _itemService.GetMyProjects();
            _total = ProjectInfos.Count;
            await base.OnInitializedAsync();
        }

        public int serialNumber(int pageIndex, int pageSize, ProjectContentRecord pr)
        {
            int iIndex = ProjectInfos.IndexOf(pr);

            return iIndex + 1;
        }

        public async Task SetFinished(string CaseNo)
        {
            var result = await _itemService.SetProjectFinish(CaseNo);

            if (!result)
            {
               await MsgSvr.Error($"设定专案[{CaseNo}]为已完成时出错，请稍后再试！");
            }
        }

        private void HandlePageChange(PaginationEventArgs args)
        {
            if (_pageIndex != args.Page)
            {
                _pageIndex = args.Page;
            }

            if (_pageSize != args.PageSize)
            {
                _pageSize = args.PageSize;
            }
        }

        public void AddNew()
        {
            _NavigationManager.NavigateTo("/Project/WorkContent");
        }

        public void Edit(ProjectContentRecord projectContent)
        {
            _NavigationManager.NavigateTo($"/Project/WorkContent/{projectContent.Id}");
        }

        private string FininalJX(ProjectContentRecord projectContent)
        {
            string strRet = "";

            if (projectContent.PointS.HasValue)
            {
                strRet = $"{strRet}{projectContent.PointS}S,";
            }

            if (projectContent.PointA.HasValue)
            {
                strRet = $"{strRet}{projectContent.PointA}A,";
            }

            if (projectContent.PointB.HasValue)
            {
                strRet = $"{strRet}{projectContent.PointB}B,";
            }

            if (projectContent.PointC.HasValue)
            {
                strRet = $"{strRet}{projectContent.PointC}C,";
            }

            if (projectContent.PointD.HasValue)
            {
                strRet = $"{strRet}{projectContent.PointS}D,";
            }

            if(strRet.Length > 0)
            {
                strRet = strRet.Substring(0, strRet.Length - 1);
            }
            return strRet;
        }

        async void Submit(ProjectContentRecord projectContent)
        {
            var modalConfig = new ModalOptions();
            modalConfig.Title = $"{projectContent.Project.CaseNo}工作内容提交审核";
            modalConfig.Width = 850;
            //modalConfig.Footer = null;
            modalConfig.DestroyOnClose = true;
            modalConfig.MaskClosable = false;
            modalConfig.OkText = "提交";
            modalConfig.CancelText = "取消";

            _modalRef = await _ModalService
                .CreateModalAsync<Components.SubmitWorkContent, ProjectContentRecord>(modalConfig, projectContent);

            _modalRef.OnOpen = () =>
            {
                return Task.CompletedTask;
            };

            _modalRef.OnOk = async () =>
            {
                try
                {
                    if (projectContent.Project.ReviewerId.HasValue)
                    {
                        await _itemService.SubmitToReview(projectContent.Id, projectContent.Project.ReviewerId.Value);

                        var SuccessConfig = new ConfirmOptions()
                        {
                            Content = @"提交成功！"
                        };
                       
                        modalConfig.DestroyOnClose = true;
                        _ModalService.Success(SuccessConfig);

                        ProjectInfos = await _itemService.GetMyProjects();
                        StateHasChanged();
                    }
                    else
                    {
                        _ModalService.Info(new ConfirmOptions()
                        {
                            Content = "请选择审核人员！",
                        });
                    }
                }
                catch (Exception ex)
                {
                    _ModalService.Error(new ConfirmOptions()
                    {
                        Title = "错误",
                        Content = ex.Message,
                    });

                }

            };

            _modalRef.OnCancel = () =>
            {
                return Task.CompletedTask;
            };

            _modalRef.OnClose = () =>
            {
                return Task.CompletedTask;
            };
            StateHasChanged();
        }
    }
}
