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

namespace wispro.sp.web.Components
{
    public partial class SSTable
    {
        protected AntDesign.Table<AppealRecord> table;
        AntDesign.Column<AppealRecord> column;
        int _total;
        int _pageIndex=1;
        int _pageSize=10;
        bool _loading;
        
        [Parameter]
        public List<AppealRecord> Datas { get; set; }

        //[Inject] protected UserService _userService { get; set; }

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

        [Parameter]
        public bool ShowAction { get; set; } = true;

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

        AppealRecord CurrentAppealRecord;
        bool _ShowDetail;
        List<InputFieldValue> _FieldValues;
        List<InputFieldValue> _ReviewValues;
        List<AttachFile> attachFiles;
        async Task ShowDetail(AppealRecord appealRecord)
        {
            
            CurrentAppealRecord = appealRecord;
            _FieldValues = await _atService.GetInputFieldValues(appealRecord.Id, 0);
            attachFiles = await _atService.GetAppealRecordAttachFiles(CurrentAppealRecord.Id);

            if (appealRecord.State > 0)
            {
                _ReviewValues = await _atService.GetInputFieldValues(appealRecord.Id, 1);
            }

            _ShowDetail = true;
            StateHasChanged();
        }

        void CloseDetail()
        {
            _ShowDetail = false;
        }

        Dictionary<string, object> OnRow(RowData<AppealRecord> row) => new()
        {
            ["id"] = row.Data.Id,
            ["onclick"] = ((Action)delegate
            {
                _ = ShowDetail(row.Data);
            })
        };

        //CurrentUser currentUser;

        [Parameter]
        public EventCallback<AppealRecord> OnReview { get; set; }

        [Parameter]
        public EventCallback<AppealRecord> OnChangeReviewer { get; set; }

        protected override void OnParametersSet()
        {
            _total = Datas.Count;
            base.OnParametersSet();
        }

        public int serialNumber(int pageIndex, int pageSize, AppealRecord appealRecord)
        {
            //int iIndex = 0;
            //foreach (AppealRecord sf in Datas)
            //{
            //    iIndex++;

            //    if (sf == appealRecord)
            //    {
            //        break;
            //    }
            //}

            var queryModel = table.GetQueryModel();

            if (table.SortDirections.Count() > 0)
            {
                Console.WriteLine(table.SortDirections[0].Name);
                Console.WriteLine(table.SortDirections[0].Value);
            }

            return Datas.IndexOf(appealRecord) + 1;

            //return iIndex;
        }

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

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

        private async void _OnReview(AppealRecord appealRecord)
        {
            if (OnReview.HasDelegate)
            {
                await OnReview.InvokeAsync(appealRecord);
            }
        }
        

        private async void _OnChangeReviewer(AppealRecord appealRecord)
        {
            if (OnChangeReviewer.HasDelegate)
            {
                await OnChangeReviewer.InvokeAsync(appealRecord);
            }
        }
    }
}
