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

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

class SecondarypwdVerify extends Component {
  constructor(props) {
    super(props);
    this.state = {
      src: (window.ecologyContentPath || "") + "/weaver/weaver.file.MakeValidateCode?notneedvalidate=1&isView=1&validatetype=0&validatenum=4",
      num: 0
    };
  }

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

  componentWillUnmount() {
    form.resetForm();
  }

  save = async () => {
    form.validateForm().then(f => {
      if (f.isValid) {
        const { secondaryPwd1, secondaryPwd2, validatecode } = form.getFormParams();
        if (secondaryPwd1 !== secondaryPwd2) {
          form.showError("secondaryPwd2", getLabel(504376, "密码确认不正确！"));
          return;
        }
        RSAEcrypt("1", { secondaryPwd1, secondaryPwd2 }).then(RSAParam => {
          API.saveSecondaryPwd({ ...RSAParam, validatecode }, this.props.salaryBillToken)
            .then(({ sign, message: msg }) => {
              if (sign === "1") {
                message.success(msg);
                this.props.onSuccess();
              } else {
                form.showError("validatecode", msg);
                this.setState({ num: this.state.num + 1 }, () => {
                  this.setState({ src: `${window.ecologyContentPath || ""}/weaver/weaver.file.MakeValidateCode?notneedvalidate=1&isView=1&validatetype=0&validatenum=4&seriesnum_=${this.state.num}` });
                });
              }
            });
        });
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const itemRender = {
      validatecode: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }} form={form} formParams={formParams}/>
          <img
            style={{ height: 30, cursor: "pointer", position: "absolute", left: "118%" }}
            src={this.state.src}
            onClick={() => {
              this.setState({ num: this.state.num + 1 }, () => {
                this.setState({ src: `${window.ecologyContentPath || ""}/weaver/weaver.file.MakeValidateCode?notneedvalidate=1&isView=1&validatetype=0&validatenum=4&seriesnum_=${this.state.num}` });
              });
            }} alt=""/>
        </React.Fragment>);
      }
    };
    return (<MobileModal title={getLabel(111, "二次验证密码设置")} onConfirm={this.save}>
      <FormInfo className="secondarypwd-form" center={false} itemRender={itemRender} form={form}
                formFields={secondarypwdCondition}/>
    </MobileModal>);
  }
}

export default SecondarypwdVerify;
