@page "/login"
@layout EmptyLayout
@using System.ComponentModel;
@using log4net;
    
        
    
    
        
            
                @if(loginMode == 2)
                {
                    操作人:@globalStateService.Operator?.NickName
                    复核人:@globalStateService.Reviewer?.NickName
                }
            
        
        
            
                
                    登录
                
                
                    
                    
                    
                
            
        
    
@code {
    @inject FingerprintUtil FingerprintUtil;
    @inject PortUtil PortUtil;
    @inject NavigationManager na;
    @inject NotificationService _message
    @inject GlobalStateService globalStateService;
    @inject IUserDao userDao;
    @inject Microsoft.Extensions.Options.IOptions setting;
    private readonly ILog logger = LogManager.GetLogger(typeof(Index));
    private int loginMode = 1;
    private bool opFirst = true;
    bool isShow;
    private Pojo.User loginModel = new();
    protected override Task OnInitializedAsync()
    {
        FingerprintUtil.axCZKEM1.OnAttTransactionEx += axCZKEM1_OnAttTransactionEx;
        loginMode = setting.Value.loginMode;
        opFirst = setting.Value.opFirst;
        return 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;
                        PortUtil.mouseClickTime = DateTime.Now;
                        na.NavigateTo("/home");
                        logger.Info($"双人登录模式:操作人【{globalStateService.Operator.NickName}】复核人【{user.NickName}】登录");
                    } else
                    {
                        _message.Notify(
                            new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"验证重复,请使用其他账号", Duration = 3000 }
                        );
                    }
                } else
                {
                    // 此时为第一人登录
                    globalStateService.Operator = user;
                    InvokeAsync(StateHasChanged);
                    loginModel = new();
                }
            }
            // 操作人为第二人登录
            else
            {
                // 判断复核人是否已经登录,已经登录说明此时为第二人登录
                if (globalStateService.Reviewer != null)
                {
                    // 判断是否还是复核人的验证
                    if (globalStateService.Reviewer.Id != user.Id)
                    {
                        FingerprintUtil.axCZKEM1.OnAttTransactionEx -= axCZKEM1_OnAttTransactionEx;
                        globalStateService.Operator = user;
                        PortUtil.mouseClickTime = DateTime.Now;
                        na.NavigateTo("/home");
                        logger.Info($"双人登录模式:操作人【{user.NickName}】复核人【{globalStateService.Reviewer.NickName}】登录");
                    }
                    else
                    {
                        _message.Notify(
                            new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"验证重复,请使用其他账号", Duration = 3000 }
                        );
                    }
                }
                else
                {
                    // 此时为第一人登录
                    globalStateService.Reviewer = user;
                    InvokeAsync(StateHasChanged);
                    loginModel = new();
                }
            }
        } else
        {
            FingerprintUtil.axCZKEM1.OnAttTransactionEx -= axCZKEM1_OnAttTransactionEx;
            globalStateService.Operator = user;
            PortUtil.mouseClickTime = DateTime.Now;
            na.NavigateTo("/home");
            logger.Info($"单人登录模式:用户【{user.NickName}】登录");
        }
    }
    
    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);
            } 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()
    {
        
        Environment.Exit(0);
    }
}