@page "/user/finger/{userId}"
@page "/finger/{userId}"
@using log4net;

<RadzenStack Gap="1.5rem">
    <RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.Center">
        <RadzenLabel Text="选择手指" Component="DropDownBindValue" Style="margin-right: 8px; vertical-align: middle;" />
        <RadzenDropDown @bind-Value=@fingerName Data=@fingerNames Style="width: 100%; max-width: 400px;" Name="DropDownBindValue" />
    </RadzenStack>

    <RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.SpaceBetween">
        <RadzenStack Orientation="Orientation.Horizontal">
            <RadzenButton IsBusy="@regLoading" Text="录入" Click="() => RegZWJSubmit()" Style="width: 80px;" />
            <RadzenButton Text="取消" Click="() => RegZWJCancel()" ButtonStyle="ButtonStyle.Light" />
        </RadzenStack>
    </RadzenStack>
</RadzenStack>

@code {
    [Parameter] public int userId { get; set; }


    @inject DialogService dialogService;
    @inject FingerprintUtil FingerprintUtil;


    private readonly ILog logger = LogManager.GetLogger(typeof(FingerRegDialog));

    List<string> fingerNames = new List<string>()
    {
        "左小拇指","左无名指","左中指", "左食指", "左大拇指", "右小拇指", "右无名指", "右中指" ,"右食指", "右大拇指"

    };

    string fingerName = "左小拇指";
    bool regLoading = false;


    void RegZWJSubmit()
    {
        regLoading = true;
        InvokeAsync(StateHasChanged);
        FingerprintUtil.axCZKEM1.OnEnrollFingerEx += axCZKEM1_OnEnrollFingerEx;
        FingerprintUtil.SaveFingerprint(userId, fingerNames.FindIndex(n => n.Equals(fingerName)));
    }

    void RegZWJCancel()
    {

        FingerprintUtil.axCZKEM1.CancelOperation();
        regLoading = false;
        InvokeAsync(StateHasChanged);
        FingerprintUtil.axCZKEM1.OnEnrollFingerEx -= axCZKEM1_OnEnrollFingerEx;
        dialogService.Close();
    }

    private void axCZKEM1_OnEnrollFingerEx(string iEnrollNumber, int iFingerIndex, int iActionResult, int iTemplateLength)
    {

        FingerprintUtil.axCZKEM1.StartIdentify();
        FingerprintUtil.axCZKEM1.RefreshData(1);
        regLoading = false;
        InvokeAsync(StateHasChanged);
        dialogService.Close();
        logger.Info($"触发用户登记指纹事件1，用户id:{iEnrollNumber}指纹索引:{iFingerIndex}登记结果:{(iActionResult == 0)}");
        FingerprintUtil.axCZKEM1.OnEnrollFingerEx -= axCZKEM1_OnEnrollFingerEx;

    }

}
