@page "/EditPasswordDialog"
@using MasaBlazorApp3.Pojo.Config;
@using MasaBlazorApp3.Pojo.Vo;
@using MasaBlazorApp3.Util;
@using Microsoft.Extensions.Options;
@using Newtonsoft.Json;
@using log4net;
<style>
    .rz-dialog-content {
    padding: 0 !important;
    }
</style>

<RadzenRow Gap="0" Style="overflow: hidden;width:100%;height:100%">
  @*   <RadzenColumn Size="12" SizeMD="5">
        <RadzenCard class="rz-shadow-0 rz-border-radius-0 rz-text-align-center rz-p-12 align-items-center justify-content-center" Style="height: 100%; background: var(--rz-primary-light) no-repeat 100% 70% fixed url('data:image/svg+xml;base64,[encoded data, 4480 characters]')">
            @if (userI == 1)
            {
                <RadzenText TextStyle="TextStyle.H5" class="rz-color-white">
                    操作人 @globalStateService.Operator?.NickName
                    <p>正在修改密码</p>
                    </RadzenText>
            }
            else if (userI == 2)
            {
                <RadzenText TextStyle="TextStyle.H5" class="rz-color-white">
                    复核人 @globalStateService.Reviewer?.NickName
                    <p>正在修改密码</p>
                </RadzenText>
            }
        </RadzenCard>
    </RadzenColumn> *@
    <RadzenColumn Size="12" SizeMD="7">
        <RadzenCard Class="rz-shadow-0 rz-border-radius-0 rz-p-12" Style="width:100%;height:100%">
            @*  <RadzenText TextStyle="TextStyle.H5" TagName="TagName.H2" class="rz-mb-6">
                密码修改
            </RadzenText> *@
            <RadzenTemplateForm Data="@loginModel" Submit="@((Pojo.User args) => { Submit(args); })" Style="width:100%;height:100%">
                <div class="rz-form-row">
                    <label class="rz-label" for="oldPassword">原密码</label>
                    <div class="rz-form-input-wrapper">
                        <RadzenPassword id="oldPassword" Name="oldPassword" @bind-Value="@loginModel.OldPassword" Style="width:100%" />
                        <RadzenRequiredValidator Component="oldPassword" Text="请填写原密码" />
                    </div>
                </div>
                <div class="rz-form-row">
                    <label class="rz-label" for="password">新密码</label>
                    <div class="rz-form-input-wrapper">
                        <RadzenPassword id="password" Name="Password" @bind-Value="@loginModel.Password" Style="width:100%" />
                        <RadzenRequiredValidator Component="Password" Text="请填新密码" />
                    </div>
                </div>
                <div class="rz-form-row">
                    <label class="rz-label"></label>
                    <div class="rz-form-input-wrapper rz-login-buttons">
                        <RadzenButton Style="background:#255dd4;color:white;width:110px" ButtonType="ButtonType.Submit" Text="确定" />
                        <RadzenButton Style="border-color:#255dd4;width:110px" Variant="Variant.Outlined" Text="取消" Click="@Exit" />

                    </div>
                </div>
            </RadzenTemplateForm>
        </RadzenCard>
    </RadzenColumn>

</RadzenRow>

@code {
    @inject NotificationService _message
    @inject GlobalStateService globalStateService;
    @inject IUserDao userDao;
    @inject Radzen.DialogService dialogService;

    private Pojo.User loginModel = new();
    private readonly ILog logger = LogManager.GetLogger(typeof(LoginDialog));
    [Parameter] public int userI { get; set; }


    private void Submit(Pojo.User user)
    {
        if (userI == 1)
        {
            user.Username = globalStateService.Operator.Username;
        }
        else if (userI == 2)
        {
            user.Username = globalStateService.Reviewer.Username;
        }
        else
        {
            return;
        }
        Pojo.User u = userDao.GetByUsername(loginModel.Username);
        if (u != null)
        {
            if (Util.MD5.GetMD5Hash(loginModel.OldPassword).ToLower().Equals(u.Password))
            {
                u.Password = loginModel.Password;
                bool iRet=userDao.UpdateUserPassword(u);
                if (iRet)
                {
                    _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "提示", Detail = $"修改完成，退出后登录请使用新密码", Duration = 4000 });
                    logger.Error($"{loginModel.Username}修改密码失败，数据{JsonConvert.SerializeObject(u)}");
                    // 关闭弹窗
                    dialogService.Close(false);
                }
                else
                {
                    _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"修改失败", Duration = 4000 });
                    logger.Error($"{loginModel.Username}修改密码失败，数据{JsonConvert.SerializeObject(u)}");
                    // 关闭弹窗
                    dialogService.Close(false);
                }
            }
            else
            {
                _message.Notify(
                    new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"原密码错误", Duration = 4000 }
                );
                logger.Info($"用户【{u.Username}】修改密码，原密码输入错误");

            }

        }
        else
        {
            _message.Notify(
                new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"无此用户", Duration = 4000 }
            );
            logger.Info($"修改密码没有用户：【{loginModel.Username}】");
        }
    }
    private void Exit()
    {
        dialogService.Close(false);
    }
}
