@page "/loginDialog"
@using MasaBlazorApp3.Pojo.Config;
@using MasaBlazorApp3.Pojo.Vo;
@using MasaBlazorApp3.Util;
@using Microsoft.Extensions.Options;
@using Newtonsoft.Json;
@using log4net;
    
        
            @if (loginMode == 2)
            {
                @myText.Operator:@globalStateService.Operator?.NickName
                @myText.Checker:@globalStateService.Reviewer?.NickName
            }
        
    
    
        
            
                @myText.Login
            
            
                
                
                
            
        
    
@code {
    @inject Radzen.DialogService dialogService;
    @inject FingerprintUtil FingerprintUtil;
    @inject PortUtil PortUtil;
    @inject NavigationManager na;
    @inject NotificationService _message
    @inject GlobalStateService globalStateService;
    @inject IUserDao userDao;
    @inject Microsoft.Extensions.Options.IOptions setting;
    @inject Toolbelt.Blazor.I18nText.I18nText I18nText;
    private I18nText.local myText = new I18nText.local();
    private readonly ILog logger = LogManager.GetLogger(typeof(LoginDialog));
    private int loginMode = 1;
    private bool opFirst = true;
    bool isShow;
    private Pojo.User loginModel = new();
    protected override async Task OnInitializedAsync()
    {
        myText = await I18nText.GetTextTableAsync(this);
        FingerprintUtil.axCZKEM1.OnAttTransactionEx += axCZKEM1_OnAttTransactionEx;
        loginMode = setting.Value.loginMode;
        opFirst = setting.Value.opFirst;
        base.OnInitializedAsync();
    }
    private void SetUser(Pojo.User user)
    {
        // 判断是否为双人登录模式
        if (loginMode == 2)
        {
            // 判断是否是操作人优先登录
            if (opFirst)
            {
                // 判断操作人是否已经登录,已经登录说明此时为第二人登录
                if (globalStateService.Operator != null)
                {
                    // 判断是否还是操作人的验证
                    if (globalStateService.Operator.Id != user.Id)
                    {
                        FingerprintUtil.axCZKEM1.OnAttTransactionEx -= axCZKEM1_OnAttTransactionEx;
                        globalStateService.Reviewer = user;
                        logger.Info($"双人登录模式:操作人【{globalStateService.Operator.NickName}】复核人【{user.NickName}】登录");
                        dialogService.Close(true);
                    }
                    else
                    {
                        _message.Notify(
                            new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"验证重复,请使用其他账号", Duration = 3000 }
                        );
                    }
                }
                else
                {
                    // 此时为第一人登录
                    globalStateService.Operator = user;
                    InvokeAsync(StateHasChanged);
                }
            }
            // 操作人为第二人登录
            else
            {
                // 判断复核人是否已经登录,已经登录说明此时为第二人登录
                if (globalStateService.Reviewer != null)
                {
                    // 判断是否还是复核人的验证
                    if (globalStateService.Reviewer.Id != user.Id)
                    {
                        FingerprintUtil.axCZKEM1.OnAttTransactionEx -= axCZKEM1_OnAttTransactionEx;
                        globalStateService.Operator = user;
                        logger.Info($"双人登录模式:操作人【{user.NickName}】复核人【{globalStateService.Reviewer.NickName}】登录");
                        dialogService.Close(true);
                    }
                    else
                    {
                        _message.Notify(
                            new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"验证重复,请使用其他账号", Duration = 3000 }
                        );
                    }
                }
                else
                {
                    // 此时为第一人登录
                    globalStateService.Reviewer = user;
                    InvokeAsync(StateHasChanged);
                }
            }
        }
        else
        {
            FingerprintUtil.axCZKEM1.OnAttTransactionEx -= axCZKEM1_OnAttTransactionEx;
            globalStateService.Operator = user;
            logger.Info($"单人登录模式:用户【{user.NickName}】登录");
            dialogService.Close(true);
        }
    }
    private void Submit(Pojo.User user)
    {
        Pojo.User u = userDao.GetByUsername(loginModel.Username);
        if (u != null)
        {
            if (Util.MD5.GetMD5Hash(loginModel.Password).ToLower().Equals(u.Password))
            {
                SetUser(u);
                loginModel.Username="";
                loginModel.Password = "";
            }
            else
            {
                _message.Notify(
                    new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"密码错误", Duration = 4000 }
                );
                logger.Info($"用户【{u.NickName}】密码输入错误");
            }
        }
        else
        {
            _message.Notify(
                new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"无此用户", Duration = 4000 }
            );
            logger.Info($"没有用户:【{loginModel.Username}】");
        }
    }
    private void axCZKEM1_OnAttTransactionEx(string sEnrollNumber, int iIsInValid, int iAttState, int iVerifyMethod, int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond, int iWorkCode)
    {
        Pojo.User u = userDao.GetById(Convert.ToInt32(sEnrollNumber));
        if (u != null)
        {
            SetUser(u);
        }
        else
        {
            _message.Notify(
                new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"系统中没有ID为【{sEnrollNumber}】的用户", Duration = 4000 }
            );
            logger.Info($"指纹机验证通过id为【{sEnrollNumber}】,但是华康数据库中无此用户");
        }
    }
    private void Exit()
    {
        if (globalStateService.Operator != null)
        {
            globalStateService.Operator = null;
            globalStateService.Reviewer = null;
        }
        dialogService.Close(false);
    }
}