using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Net.Http;
using System.Net.Http.Json;
using wispro.sp.share.webViewObject;

namespace wispro.sp.webclient.Pages
{
    public partial class LoginPages
    {
        [Inject] public HttpClient Http { get; set; }
        [Inject] public MessageService MsgSvr { get; set; }
        [Inject] public AuthenticationStateProvider AuthProvider { get; set; }

        loginDto model = new loginDto();
        bool isLoading;

        async void OnLogin()
        {
            isLoading = true;

            var httpResponse = await Http.PostAsJsonAsync<loginDto>($"http://localhost:39476/api/account/Login", model);
            userToken result = await httpResponse.Content.ReadFromJsonAsync<userToken>();

            if (string.IsNullOrWhiteSpace(result?.Token) == false)
            {
                _ = MsgSvr.Success($"登录成功");
                ((AuthProvider)AuthProvider).MarkUserAsAuthenticated(result);
            }
            else
            {
                _ = MsgSvr.Error($"用户名或密码错误");
            }
            isLoading = false;
            //await InvokeAsync(StateHasChanged);
        }
    }
}
