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

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

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

        [Inject] protected NavigationManager navigation { get; set; }

        [Inject] protected IAuthService _authService { get; set; }

        [Inject] protected IJSRuntime JSRuntime { get; set; }

        protected async override Task OnInitializedAsync()
        {
            string strResourceId = navigation.Uri.Replace(navigation.BaseUri,"/");
            //Console.WriteLine(navigation.ToAbsoluteUri(navigation.Uri));
            //Console.WriteLine(navigation.ToBaseRelativePath(navigation.Uri));

            await _authService.CanVisitResource();
            

            ProjectInfos = await _itemService.GetAllProjects();
            _total = ProjectInfos.Count;
            await base.OnInitializedAsync();
        }

        public int serialNumber(int pageIndex, int pageSize, string name)
        {
            int iIndex = 0;
            
            foreach (ProjectInfo sf in ProjectInfos)
            {
                iIndex++;

                if (sf.CaseNo == name)
                {
                    break;
                }
            }

            return iIndex;
        }

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

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

        public async Task OnChange(QueryModel<ProjectInfo> queryModel)
        {
            //Console.WriteLine(JsonSerializer.Serialize(queryModel));
        }

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

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

