using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
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;

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


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

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

        Dictionary<string, object> attrs = new Dictionary<string, object>
        {
            {"Action", "http://localhost:39476/api/AttachFiles/PostFile" },
            {"Name", "files" },
            {"Multiple", false }
        };

        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 = $"http://localhost:39476/api/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>($"http://localhost:39476/api/AttachFiles/Delete?id={result[0].Id}");
            }
            catch {
                return false;
            }
        }
        
        #endregion


        protected override async void OnInitialized()
        {
            _Model = base.Options ?? new CreateAppealModel();
            base.OnInitialized();
            feedbackRef = base.FeedbackRef;
        }

        string _ErrorMessage;

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

        private void OnCancel()
        {

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

        
    }
}
