using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using wispro.sp.web.Models;

namespace wispro.sp.web.Pages.Project
{
    public partial class AssignPoint
    {
        [Inject] public MessageService MsgSvr { get; set; }

        [Parameter]
        public string id { get; set; }

        private AntDesign.Modal model;

        Task task = new()
        {
            ProjectInfo = new ProjectInfo()
            {
                CaseNo = "S2112394",
                CaseName = "美的-洗碗机专利调查",
                DoItem = "提出报告",
                DoPerson = new List<string>() { "李申", "何倚雯", "王晴", "陆跃" },
                ResponseMan = "黄瑜"
            },
            Id = 1,
            Name = "2021年9月专案点数分配任务",
            Type = "专案点数分配任务",
            ResponseMan = "黄瑜",
            State = "待处理",
            LimiteDate = DateTime.Now.AddDays(4),
            UIUrl = "/Project/AssignPoint"
        };

        List<ProjectPoint> projectPoints=new List<ProjectPoint>() { 
            new ProjectPoint(){ doItem="检索条件策略", DoItemCoefficient="B", person="李申", Point=1.0},
            new ProjectPoint(){ doItem="检索条件策略", DoItemCoefficient="B", person="何倚雯", Point=2.0},
            new ProjectPoint(){ doItem="检索条件策略", DoItemCoefficient="B", person="王晴", Point=1.5},
            new ProjectPoint(){ doItem="检索条件策略", DoItemCoefficient="B", person="陆跃", Point=1.0},
            new ProjectPoint(){ doItem="专利分类", DoItemCoefficient="B", person="李申", Point=2.0},
            new ProjectPoint(){ doItem="专利分类", DoItemCoefficient="B", person="何倚雯", Point=1.5},
            new ProjectPoint(){ doItem="专利分类", DoItemCoefficient="B", person="王晴", Point=2.5},
            new ProjectPoint(){ doItem="专利分类", DoItemCoefficient="B", person="陆跃", Point=3.0},
        };
        

        private class AddDoItem
        {
            public string Name { get; set; }

            public string nanduxishu { get; set; }

            public List<ProjectPoint> AddprojectPoints { get; set; }
        }

        private AddDoItem addDoItem;
        private bool _visible = false;
        private void AddNew()
        {
            addDoItem = new AddDoItem();
            addDoItem.AddprojectPoints = new List<ProjectPoint>();
            foreach (string name in task.ProjectInfo.DoPerson)
            {
                addDoItem.AddprojectPoints.Add(new ProjectPoint()
                {
                    //doItem = addDoItem.nanduxishu,
                    //DoItemCoefficient = addDoItem.nanduxishu,
                    person = name,
                    Point = 0
                });
            }

                _visible = true;
        }

        private void HandleOk(MouseEventArgs e)
        {

            if (!string.IsNullOrEmpty(addDoItem.Name))
            {
                List<ProjectPoint> temList = new List<ProjectPoint>();

                if(projectPoints== null)
                {
                    projectPoints = new List<ProjectPoint>();
                }

                if (addDoItem.AddprojectPoints != null)
                {
                    foreach(ProjectPoint pp in addDoItem.AddprojectPoints)
                    {
                        pp.doItem = addDoItem.Name;
                        pp.DoItemCoefficient = addDoItem.nanduxishu;
                    }
                    
                }

                projectPoints.AddRange(addDoItem.AddprojectPoints);
            }

            _visible = false;
            model.Dispose();
        }

        private void HandleCancel(MouseEventArgs e)
        {
            //Console.WriteLine(e);
            _visible = false;
            model.Dispose();
        }

        string editId = null;
        void startEdit(string id)
        {
            editId = id;
            MsgSvr.Info($"开始编辑{editId}");
        }

        void stopEdit()
        {
            //var editedData = addDoItem.AddprojectPoints.FirstOrDefault(x => x.person == editId);
            //Console.WriteLine(JsonSerializer.Serialize(editedData));
            editId = null;
        }

        void Delete(string id)
        {
            if (_visible)
            {
                var editedData = addDoItem.AddprojectPoints.FirstOrDefault(x => x.person == id);
                addDoItem.AddprojectPoints.Remove(editedData);
            }
            else
            {
                var editedData = projectPoints.FirstOrDefault(x => x.person == id);
                projectPoints.Remove(editedData);
            }
        }

        void OnSave()
        {
            //添加保存代码
        }

        void OnCancel()
        {

        }
    }
}
