using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration;
using ServiceStack;
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.AppCase
{
    public partial class CaseManager
    {
        [Inject]
        protected  IconService iconService { get; set; }

        public TableFilter<string>[] CaseCoeFilters = new TableFilter<string>[] {
            new() { Text = "S", Value = "S" },
            new() { Text = "A", Value = "A" },
            new() { Text = "B", Value = "B" },
            new() { Text = "C", Value = "C" },
            new() { Text = "D", Value = "D" }
        };

        [Inject]
        protected AppealTypeService apTypeService { get; set; }

        private List<PerformanceItem> _Datas;
        private List<StaffStatistics> MyStatistics;
        IEnumerable<PerformanceItem> selectedItems = new List<PerformanceItem>();
        private CalMonth HandlingCalMonth;
        private List<CalMonth> calMonths;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total;
        bool _loading = false;
        private Table<PerformanceItem> table;

        [Inject] public PerformanceItemServices _ItemService { get; set; }
        [Inject] public MessageService _message { get; set; }
        [Inject] protected NavigationManager NavigationManager { get; set; }
        [Inject] protected IUserService _userService { get; set; }

        [Inject] protected CalMonthServices _CalMonthService { get; set; }

        [Inject] IConfiguration _configuration { get; set; }

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

        protected override async Task OnInitializedAsync()
        {
            await _authService.CanVisitResource();
            await iconService.CreateFromIconfontCN("//at.alicdn.com/t/font_8d5l8fzk5b87iudi.js");
            calMonths  = await _CalMonthService.GetAll();

            if (calMonths != null && calMonths.Count > 0)
            {
                await calMonthClick(calMonths[0]);
            }

        }

        private async Task HandleTableChange(QueryModel<PerformanceItem> queryModel)
        {
            var _user = await _userService.GetUser();
            
            _loading = true;
            
            var data = await _ItemService.Query(HandlingCalMonth, queryModel);

            _Datas = data.Results;
            _total = data.TotalCount;
            _loading = false;
            StateHasChanged();
        }

        private async Task calMonthClick(CalMonth calMonth)
        {
            HandlingCalMonth = calMonth;

            var data = await _ItemService.Query(HandlingCalMonth,_pageIndex ,_pageSize,null);

            _Datas = data.Results;
            _total = data.TotalCount;
            _loading = false;
            StateHasChanged();
        }

        bool isGuiDang = false;
        private async Task GuidangCalMonth(CalMonth calMonth)
        {
            isGuiDang = true;
            //还未实现，此处需要添加归档代码
            var fileData = await _ItemService.FinishedCalMonth(calMonth);

            while (!fileData.Finished)
            {
                fileData = await _ItemService.getExportDataProcessing(fileData.Id);
                await Task.Delay(1000);
            }

            if (!string.IsNullOrEmpty(fileData.ErrorMessage))
            {
                //calMonths = await _CalMonthService.GetAll();
                calMonth.Status = 4;
                //calMonthClick(calMonth);
            }

            isGuiDang = false;
            StateHasChanged();
        }

        private int serialNumber(int pageIndex, int pageSize, int id)
        {
            int iIndex = 0;
            foreach (PerformanceItem sf in _Datas)
            {
                iIndex++;

                if (sf.Id == id)
                {
                    break;
                }
            }
            return (pageIndex - 1) * pageSize + iIndex;
        }

        bool isDownloading = false;
        private async Task ExportDataAsync(CalMonth calMonth)
        {
            isDownloading = true;
            var fileData = await _ItemService.ExportData(calMonth.Id);

            while (!fileData.Finished)
            {
                try
                {
                    fileData = await _ItemService.getExportDataProcessing(fileData.Id);
                    await Task.Delay(100);
                }
                catch {
                    await Task.Delay(1000);
                }
                
            }

            NavigationManager.NavigateTo($"{_configuration.GetValue<string>("APIUrl")}FileProcesTask/Download?Id={fileData.Id}");

            isDownloading = false;

        }

        private async Task ExportJXDataAsync(CalMonth calMonth)
        {
            isDownloading = true;
            var fileData = await _ItemService.ExportJXReport(calMonth.Year,calMonth.Month);

            while (!fileData.Finished)
            {
                fileData = await _ItemService.getExportDataProcessing(fileData.Id);
                await Task.Delay(20);
            }

            NavigationManager.NavigateTo($"{_configuration.GetValue<string>("APIUrl")}FileProcesTask/Download?Id={fileData.Id}");

            isDownloading = false;

            

        }

        private async Task ExportCurrentMonthJXList()
        {
            isDownloading = true;
            var fileData = await _ItemService.CurrentData2Excel(HandlingCalMonth);

            while (!fileData.Finished)
            {
                fileData = await _ItemService.getExportDataProcessing(fileData.Id);
                await Task.Delay(20);
            }

            NavigationManager.NavigateTo($"{_configuration.GetValue<string>("APIUrl")}FileProcesTask/Download?Id={fileData.Id}");

            isDownloading = false;
        }

        async Task OnRefresh(PerformanceItem item)
        {
            item = await _ItemService.RefreshItem(item.Id);
            table.ReloadData();
            StateHasChanged();
        }

        async Task<string> CalItemJX(PerformanceItem item)
        {
            return null;
        }
    }
}
