/*
  * 登录密码验证
  *
  * @Author:    黎永顺
  * @Date:      2025/4/17
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaLocaleProvider } from "ecCom";
import { WeaForm } from "comsMobx";
import FormInfo from "../../components/FormInfo";
import { loginCondition } from "./conditions";
import MobileModal from "./mobileModal";
import * as API from "../../apis/mySalaryBenefits";
import { RSAEcrypt } from "../../util/RSAUtil";

const form = new WeaForm();
const getLabel = WeaLocaleProvider.getLabel;

class LoginVerify extends Component {

  componentDidMount() {
    form.initFormFields(loginCondition);
  }

  componentWillUnmount() {
    form.resetForm();
  }

  save = async () => {
    form.validateForm().then(f => {
      if (f.isValid) {
        RSAEcrypt("1", { ...form.getFormParams() }, this.props.salaryBillToken)
          .then(RSAParam => {
            API.checkPassword({ ...RSAParam }).then(({ result }) => {
              if (result) {
                this.props.onSetPwdSet();
              } else {
                form.showError("password", getLabel(504343, "登录密码错误"));
              }
            });
          });
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const itemRender = {};
    return (<MobileModal title={getLabel(111, "请先输入登录密码")} onConfirm={this.save}>
      <FormInfo center={false} itemRender={itemRender} form={form} formFields={loginCondition}/>
    </MobileModal>);
  }
}

export default LoginVerify;
