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 IProjectService _itemService { get; set; }
        [Inject] NavigationManager _NavigationManager { get; set; }

        ProjectContents task = new() {
            ProjectContentRecord = new ProjectContentRecord(),
            ProjectWorkContents = new List<ViewProjectWorkContent>()
        };

        List<ViewProjectWorkContent> _ShowWorkContent;

        List<ProjectInfo> ProjectInfos;
        List<ProjectInfo> ShowProjectInfos;

        ViewProjectWorkContent addDoItem;

        bool isLoading = false;

        [Parameter]
        public int? Id { get; set; }

        

        private void AddNew()
        {
            addDoItem = new ViewProjectWorkContent();
            addDoItem.WorkDate = DateTime.Now;
            addDoItem.modifyState = ModifyState.New;

            task.ProjectWorkContents.Add(addDoItem);
            _ShowWorkContent = GetShowWorkContents();
        }

        protected async override Task OnInitializedAsync()
        {
            isLoading = true;
            ProjectInfos =await _itemService.GetProjectInfos(0);
            ShowProjectInfos = ProjectInfos;
            
            if (Id.HasValue)
            {
                task = await _itemService.getProjectWorkContent(Id.Value);
                _ShowWorkContent = GetShowWorkContents();
            }
            await base.OnInitializedAsync();

            isLoading = false;
        }

        private void OnSelectedItemChangedHandler(ProjectInfo value)
        {

            if (value != null)
            {
                //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(value));

                
                task.ProjectContentRecord.ProjectNo = value.CaseNo;
                task.ProjectContentRecord.Project = value;
            }
        }

        private void OnSearch(string value)
        {
            if (ProjectInfos != null)
            {
                ShowProjectInfos = ProjectInfos.Where<ProjectInfo>(p => p.CaseNo.Contains(value)).ToList();
            }
            StateHasChanged();
        }

        void startEdit(ViewProjectWorkContent pw)
        {
            addDoItem = pw;
            
        }

        void deletePersonPoint(ViewProjectWorkContent pw)
        {
            if (pw.modifyState != ModifyState.New)
            {
                pw.modifyState = ModifyState.Deleted;
            }
            else
            {
                task.ProjectWorkContents.Remove(pw);
            }

            _ShowWorkContent = GetShowWorkContents();
        }

        void stopEdit()
        {
            if(addDoItem.modifyState != ModifyState.New)
            {
                addDoItem.modifyState = ModifyState.Modified;
            }
            
            addDoItem = null;
        }

        void DeleteDoItem(ViewProjectWorkContent pp)
        {
            pp.modifyState = ModifyState.Deleted;
            //task.ProjectWorkContents.Remove(pp);
            _ShowWorkContent = GetShowWorkContents();
        }

        void EditDoItem(ViewProjectWorkContent pp)
        {
            addDoItem = pp;
        }


        private List<ViewProjectWorkContent> GetShowWorkContents()
        {
            return task.ProjectWorkContents.Where(p => p.modifyState != ModifyState.Deleted).ToList();
        }
       
        async Task OnSave()
        {
            //添加保存代码

            var response = await _itemService.SaveContent(task);

            if (response.Success)
            {
                await MsgSvr.Info("保存成功！");
            }
            else
            {
                await MsgSvr.Error(response.ErrorMessage);
            }

            _NavigationManager.NavigateTo("/Project/MyProjects");

        }

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

        
    }

    
}
