using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
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 CreateAppeal
    {

        private CreateAppealModel _Model;
        private IFeedbackRef feedbackRef;
        private List<Staff> _Staffs;

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

       
        [Inject] IUserService _UserService { get; set; }

        [Inject] 
        protected IHttpService _httpClient{get;set;}

        [Inject]
        protected IConfiguration _configuration { get; set; }

        #region 文件上传控件设定
        //List<UploadFileItem> fileList = new List<UploadFileItem>();

        Dictionary<string, object> attrs ;

        void HandleChange(UploadInfo fileinfo)
        {
            try
            {
                _Model.FileList.Where(file => file.State == UploadState.Success && !string.IsNullOrWhiteSpace(file.Response)).ForEach(file =>
                {
                    var result = file.GetResponse<List<AttachFile>>();
                    file.Url = $"{_configuration.GetValue<string>("APIUrl")}AttachFiles/Download?id={result[0].Id}";
                });
            }
            catch { }
        }

        private async Task<bool> RemoveFile(UploadFileItem fileinfo)
        {
            try
            {
                var result = fileinfo.GetResponse<List<AttachFile>>();
                return await _httpClient.Get<bool>($"{_configuration.GetValue<string>("APIUrl")}AttachFiles/Delete?id={result[0].Id}");
            }
            catch {
                return false;
            }
        }
        
        #endregion


        protected override async void OnInitialized()
        {
            attrs = new Dictionary<string, object>
            {
                {"Action", $"{_configuration.GetValue<string>("APIUrl")}AttachFiles/PostFile" },
                {"Name", "files" },
                {"Multiple", false }
            };

            _Model = base.Options ?? new CreateAppealModel();

            //if(!string.IsNullOrEmpty(_Model.AppealType.ReviewerExpress))
            //{
                _Staffs =await _UserService.GetReviewers(_Model.AppealRecord.ItemId, _Model.AppealType.Id);
            //}
            //else
            //{
            //    _Staffs =await _UserService.GetAll();
            //}
            base.OnInitialized();
            feedbackRef = base.FeedbackRef;
            StateHasChanged();
        }

        string _ErrorMessage;

        private async void OnFinish()
        {
            await base.OnFeedbackOkAsync(new ModalClosingEventArgs() { Cancel = true });
        }

        private void OnCancel()
        {

            base.OnFeedbackCancelAsync(new ModalClosingEventArgs() { Cancel = true });
            //_ = feedbackRef.CloseAsync();
        }

        
    }
}
