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

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

class SecondaryVerify extends Component {
  constructor(props) {
    super(props);
    this.state = { notSetting: false };
  }

  componentDidMount() {
    API.getSecondAuthForm({ mouldCode: "HRM", itemCode: "SALARY" }, this.props.salaryBillToken)
      .then(({ notSetting }) => {
        this.setState({ notSetting });
      });
    form.initFormFields(secondaryVerifyConditions);
  }

  componentWillUnmount() {
    this.setState({ notSetting: false }, () => form.resetForm());
  }

  doSecondAuth = () => {
    form.validateForm().then(f => {
      if (f.isValid) {
        const { salaryBillToken } = this.props;
        API.doSecondAuth({ mouldCode: "HRM", itemCode: "SALARY", ...form.getFormParams() }, salaryBillToken)
          .then(({ status, checkStatus, checkMsg }) => {
            if (status && checkStatus === "1") {
              message.success(checkMsg);
              this.props.onSuccess();
            } else {
              form.showError("authCode", checkMsg);
            }
          });
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { notSetting } = this.state;
    const itemRender = {
      authCode: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }} form={form} formParams={formParams}/>
          {
            notSetting &&
            <div style={{ clear: "both", paddingTop: 10 }}>
              {getLabel(111, "您还未设置二次验证密码，点击")}
              <a href="javascript:void(0);" onClick={this.props.onSetLogin}>{getLabel(111, "设置")}</a>
            </div>
          }
        </React.Fragment>);
      }
    };
    return (<MobileModal title={getLabel(111, "身份验证")} onConfirm={this.doSecondAuth}>
      <FormInfo center={false} custLabelCol={9} itemRender={itemRender} form={form}
                formFields={secondaryVerifyConditions}/>
    </MobileModal>);
  }
}

export default SecondaryVerify;
