using Microsoft.AspNetCore.Components;
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 AppealRecords
    {
        AppealRecordFilter _filter = new AppealRecordFilter();
        AntDesign.Internal.IForm _form;
        bool ShowSearchPanel = true;
        bool _ShowDetail = false;
        List<AppealType> AppealTypes;
        private List<AppealRecord> showRecords;

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

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

        [Parameter]
        public int pageIndex { get; set; }

        [Parameter]
        public int pageSize { get; set; }

        protected async override Task OnInitializedAsync()
        {
            await _authService.CanVisitResource();

            AppealTypes = await _atService.GetItems();
            showRecords = await _atService.GetUserAppeals(0);
            await base.OnInitializedAsync();
        }

        string GetDescription(AppealRecord context)
        {
            if(context.State == 0)
            {
                if (context.Item != null)
                {
                    return $"[{context.CreateTime.ToString("yyyy-MM-dd hh:mm:ss")}] {context.Creater.Name}提交[{context.Item.CaseNo}]{context.Type.Name}申诉，正等待{context.Reviewer.Name}审核中......";
                }
                else
                {
                    return $"{context.Creater.Name}提交提交{context.Type.Name}申诉，正等待{context.Reviewer.Name}审核中......";
                }
            }
            else
            {
                if (context.Item != null)
                {
                    return $"[{context.CreateTime.ToString("yyyy-MM-dd hh:mm:ss")}] {context.Creater.Name}提交[{context.Item.CaseNo}]{context.Type.Name}申诉，{context.Reviewer.Name}已审核！";
                }
                else
                {
                    return $"{context.Creater.Name}提交提交{context.Type.Name}申诉，{context.Reviewer.Name}已审核！";
                }
            }
        }

        async void OnSearch()
        {
            showRecords = await _atService.GetUserAppeals(_filter);
            StateHasChanged();
        }

        AppealRecord CurrentAppealRecord;
        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;
        }
        void CloseDetail()
        {
            _ShowDetail = false;
        }
    }
}
