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

namespace wispro.sp.web.Pages.Project
{
    public partial class ProjectReviewer
    {
        private List<ProjectContents> projectContents;
        private ProjectInfo projectInfo;
        private List<ProjectInfo> waitingReviewProjects;
        private ViewProjectWorkContent editObj;

        [Parameter]
        public string ProjectNo { get; set; }
        
        [Inject]
        IProjectService projectService { get; set; }

        [Inject]
        CalMonthServices calMonthServices { get; set; }

        [Inject] public MessageService _message { get; set; }

        [Inject] NavigationManager _NavigationManager { get; set; }

        protected async override  Task OnInitializedAsync()
        {
            CalMonth cal =await calMonthServices.GetHandlingMonth();
            if (!string.IsNullOrEmpty(ProjectNo))
            {
                projectContents = await projectService.GetProjectCanReviewWorkContent(ProjectNo, cal);
                HistroyContents = await projectService.GetProjectHistroy(ProjectNo);
            }
            else
            {
                waitingReviewProjects = await projectService.GetWaitingReviewProjects();
            }

            await base.OnInitializedAsync();
        }

        void startEdit(ViewProjectWorkContent pp)
        {
            editObj = pp;
        }

        void stopEdit()
        {
            editObj = null;
        }

        void ContentChanged(ViewProjectWorkContent pp)
        {
            pp.modifyState = ModifyState.Modified;
        }

        void OnInput(ChangeEventArgs e)
        {
            if (editObj != null)
            {
                editObj.modifyState = ModifyState.Modified;
            }
        }

        async void Save()
        {
            var ret = await projectService.ReviewProjectWorkContent(projectContents);

            if (ret.Success)
            {
                _NavigationManager.NavigateTo("/Home");
                await _message.Success("保存成功！");
                
            }
            else
            {
               await _message.Error(ret.ErrorMessage);
            }

        }

        List<ProjectContents> HistroyContents;
        bool _ShowHistory = false;
        async void ReviewHistory()
        {
            if (HistroyContents == null)
            {
                HistroyContents = await projectService.GetProjectHistroy(ProjectNo);
            }

            _ShowHistory = true;
            StateHasChanged();

        }

        void CloseDetail()
        {
            _ShowHistory = false;
        }

        private List<CalMonth> GetMonth(List<ProjectContents> projectContents)
        {
            List<CalMonth> monthList = new List<CalMonth>();
            foreach (var projectContentsItem in projectContents)
            {
                CalMonth calMonth = projectContentsItem.ProjectContentRecord.CalMonth;
                var temMonth =monthList.FirstOrDefault(c => c.Month == calMonth.Month && c.Year == calMonth.Year);

                if (temMonth == null)
                {
                    monthList.Add(calMonth);
                }
            }

            return monthList;
            
        }

        private List<ProjectContents> GetByMonth(CalMonth calMonth, List<ProjectContents> projectContents)
        {
            return projectContents.Where(p => p.ProjectContentRecord.CalMonthId == calMonth.Id).ToList(); 
        }

        private string GetPointString(ProjectContentRecord record,string CaseXS)
        {
            if(record != null)
            {
                switch (CaseXS)
                {
                    case "S":
                        return record.PointS.HasValue ? record.PointS.Value.ToString() : "__";
                        break;
                    case "A":
                        return record.PointA.HasValue ? record.PointA.Value.ToString() : "__";
                        break;
                    case "B":
                        return record.PointB.HasValue ? record.PointB.Value.ToString() : "__";
                        break;
                    case "C":
                        return record.PointC.HasValue ? record.PointC.Value.ToString() : "__";
                        break;
                    case "D":
                        return record.PointD.HasValue ? record.PointD.Value.ToString() : "__";
                        break;
                }
            }

            return "";
        }

    }
}
