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

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

         [Inject] protected PerformanceItemServices _itemService { get; set; }
        [Inject] NavigationManager _NavigationManager { get; set; }


        private AntDesign.Modal model;

        ProjectPointRecord task = new()
        {
            
            //CaseNo = "S2112394",
            //CaseName = "美的-洗碗机专利调查",
                
            // ProjectDoItemPoints = new List<ProjectDoItemPoint>()
            // {
            //    new ProjectDoItemPoint(){ DoItem="检索策略评估",  DoItemCoefficient="B", 
            //        PersonPoints=new List<PersonPoint>{
            //            new PersonPoint(){Id="1", Person ="李申", Point=1.0 },
            //            new PersonPoint(){Id="2", Person ="张三", Point=1.0 },
            //            new PersonPoint(){Id="3", Person ="李四", Point=1.0 },
            //        }},
            //    new ProjectDoItemPoint(){ DoItem="专利分类",  DoItemCoefficient="A", 
            //        PersonPoints=new List<PersonPoint>{
            //            new PersonPoint(){Id="1",  Person ="李申", Point=2.0 },
            //            new PersonPoint(){Id="2",  Person ="张三", Point=1.50 },
            //            new PersonPoint(){Id="3",  Person ="李四", Point=2.5 },
            //        }},
            // }
        };

        ProjectDoItemPoint addDoItem;
        bool isAdd = false;
        private bool _visible = false;
        private void AddNew()
        {
            addDoItem = new ProjectDoItemPoint();
            isAdd = true;
            _visible = true;
        }

        private void HandleOk(MouseEventArgs e)
        {
            if (isAdd)
            {
                if (!string.IsNullOrEmpty(addDoItem.DoItem))
                {
                    task.ProjectDoItemPoints.Add(addDoItem);
                }
            }
            
            _visible = false;
            model.Dispose();
        }

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

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

        void deletePersonPoint(string id)
        {
            var delObj = addDoItem.PersonPoints.FirstOrDefault<PersonPoint>(f => f.Id == id);
            addDoItem.PersonPoints.Remove(delObj);
        }

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

        void Delete(string doItem, string person)
        {
            if (_visible)
            {
                var editedData = addDoItem.PersonPoints.FirstOrDefault(x => x.Person  == person);
                addDoItem.PersonPoints.Remove(editedData);
            }
            else
            {
                
                var editedData = task.ProjectDoItemPoints .FirstOrDefault(x => x.DoItem  == doItem);
                var personPoint = editedData.PersonPoints.FirstOrDefault(x => x.Id  == person);
                editedData.PersonPoints.Remove(personPoint);
                
            }

            StateHasChanged();
        }

        void DeleteDoItem(string doItem)
        {
            var delData = task.ProjectDoItemPoints.FirstOrDefault(x => x.DoItem == doItem);
            task.ProjectDoItemPoints.Remove(delData);
        }

        void EditDoItem(string doItem)
        {
            isAdd = false;
            addDoItem = task.ProjectDoItemPoints.FirstOrDefault(x => x.DoItem == doItem);
            _visible = true; 
        }

        private string GetId(List<PersonPoint> personPoints)
        {
            int i = personPoints.Count;

            begin:
            foreach(var pp in personPoints)
            {
                if(pp.Id == i.ToString())
                {
                    i++;
                    goto begin;
                }
            }

            return i.ToString();

        }
        void addRow()
        {
            if(addDoItem.PersonPoints == null)
            {
                addDoItem.PersonPoints = new List<PersonPoint>();
            }

            string strId = GetId(addDoItem.PersonPoints);

            addDoItem.PersonPoints.Add(
                new PersonPoint() { Id = strId, Person ="修改处理人姓名", Point = 0}
                ) ;
            editId = strId;
            StateHasChanged();
        }

        async Task OnSave()
        {
            //添加保存代码
            var response = await _itemService.AddProjectPerformanctItem(task);

            if (response.Success)
            {
                await MsgSvr.Info("项目绩效保存成功！");
                _NavigationManager.NavigateTo("/MyCaseList");
            }
            else
            {
                await MsgSvr.Error(response.ErrorMessage);
            }

        }

        void OnCancel()
        {
            _NavigationManager.NavigateTo("/MyCaseList");
        }

        private PerformanceItem Item;
        private bool loading = false;
        async Task GetProjectInfo()
        {
            loading = true;
            Item = await _itemService.GetProjectInfo(task.CaseNo);

            if(Item != null)
            {
                task.CaseName = Item.CaseName;
                task.CaseNo = Item.CaseNo;
            }
            loading = false;
        }
    }

    
}
