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;
using wispro.sp.web.Services;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using System.Linq;
using System.Security.Claims;
using Blazored.LocalStorage;
using System;
using System.Text.Json;
using wispro.sp.entity;
using wispro.sp.share;
using Microsoft.AspNetCore.Components.Web;

namespace wispro.sp.web.Pages
{
    public partial class LoginPages
    {
        [Inject] public HttpClient Http { get; set; }
        [Inject] public MessageService MsgSvr { get; set; }
        //[Inject] public AuthenticationStateProvider AuthProvider { get; set; }
        [Inject] public IUserService _UserService { get; set; }
        [Inject] public IAuthService authService { get; set; }

        [Inject] public NavigationManager navigation { get; set; }

        loginDto model = new loginDto();
        bool isLoading;

        async void OnLogin()
        {
            isLoading = true;

            
            bool result = await authService.LoginAsync(model);
            
            if (result)
            {
                if (model.Password == "12345678")
                {
                    navigation.NavigateTo("/ChangePassword/true");
                }
                else
                {
                    navigation.NavigateTo("/Home");
                }
            }
            else
            {
                _ = MsgSvr.Error($"用户名或密码错误");
                
            }
            
            isLoading = false;
        }

        bool ShowResetPassword = false;
        private Staff EditingStaff = new Staff();
        void ResetPassword()
        {
            ShowResetPassword = true;
            EditingStaff = new Staff();
        }

        private async System.Threading.Tasks.Task HandleOk(MouseEventArgs e)
        {
            if(String.IsNullOrEmpty(EditingStaff.Account ) || string.IsNullOrEmpty(EditingStaff.Mail)){
                await MsgSvr.Success("请输入账号名称和邮箱!");
                return;
            }
            var data = await authService.ResetPassword(EditingStaff.Account,EditingStaff.Mail);

            if (data.Success)
            {
                await MsgSvr.Success("重置成功，新密码已发您邮箱!");
                ShowResetPassword = false;
            }
            else
            {
                await MsgSvr.Error($"请求发生错误 {data.ErrorMessage}");
            }


        }

        private void HandleCancel(MouseEventArgs e)
        {
            ShowResetPassword = false;
        }
    }
}
