<template>
  <!--MFA-->
  <el-dialog
    v-if="visible"
    :visible.sync="visible"
    width="540px"
    :close-on-click-modal="false"
    append-to-body
    center
    :show-close="status === 'login'"
    @close="cancel"
  >
    <div slot="title">
      {{ status === "login" ? $t("mfa:title") : $t("mfa:lock:title") }}
    </div>
    <el-form
      ref="mfaForm"
      label-position="right"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <!-- 邮箱 -->
      <p class="tip_mfa">
        <i class="el-icon-warning" style="color: #409eff"></i>
        <span>{{ tip }}</span>
      </p>
      <!-- 用户名 -->
      <el-form-item :label="$t('mfa:form:username')" prop="username">
        <span>{{ form.username }}</span>
      </el-form-item>
      <!-- 验证码 -->
      <el-form-item :label="$t('mfa:form:MFACode')" prop="Code">
        <el-input
          :placeholder="$t('mfa:form:input:placeholder:Codes')"
          v-model="form.Code"
          style="width: 240px; margin-right: 10px"
        />
        <el-button
          type="primary"
          size="small"
          @click.stop="sendMFACode"
          :disabled="flag || sendFlag"
          >{{ flag ? `${second}s` : $t("mfa:form:sendMFACode") }}</el-button
        >
      </el-form-item>
    </el-form>
    <div slot="footer">
      <!-- 取消 -->
      <!-- <el-button size="small" @click="cancel" v-if="status === 'login'">
        {{ $t("mfa:button:cancel") }}
      </el-button> -->
      <!-- 保存 -->
      <el-button
        type="primary"
        size="small"
        @click="save"
        :loading="loading"
        style="width: 80%"
      >
        {{
          status === "login"
            ? $t("mfa:button:save")
            : $t("mfa:lock:button:save")
        }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { sendMFAEmail, verifyMFACode } from "@/api/user.js";
export default {
  name: "MFA",
  data() {
    return {
      status: "login", // lock
      tip: null,
      visible: false,
      loading: false,
      timer: null,
      flag: false,
      sendFlag: false,
      second: 60,
      form: {
        Code: null,
        IdentityUserId: null,
        EMail: null,
        username: null,
      },
      rules: {
        Code: [
          {
            required: true,
            message: this.$t("mfa:ruleMessage:specify"),
            trigger: "blur",
          },
        ],
        EMail: [
          {
            required: true,
            message: this.$t("mfa:ruleMessage:specify"),
            trigger: "blur",
          },
        ],
        username: [
          {
            required: true,
            message: this.$t("mfa:ruleMessage:specify"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    open(data) {
      let { UserId, status, username, EMail } = data;
      this.form.IdentityUserId = UserId;
      this.status = status ? status : "login";
      this.form.username = username;
      this.form.EMail = EMail;
      this.visible = true;
      this.tip = this.$t("mfa:tip").replace("xx", EMail);
    },
    cancel() {
      this.visible = false;
      this.$emit("closed");
    },
    async save() {
      try {
        let validate = await this.$refs.mfaForm.validate();
        if (!validate) return;
        this.loading = true;
        let res = await verifyMFACode(this.form);
        this.loading = false;
        if (res.IsSuccess) {
          // if (this.status === "login") {
          //   this.$message.success(this.$t("mfa:message:verifySuccess"));
          // }
          this.$emit("success", this.form.IdentityUserId);
          this.cancel();
        }
      } catch (err) {
        console.log(err);
        this.loading = false;
      }
    },
    async sendMFACode() {
      try {
        if (this.flag || this.sendFlag) return;
        this.sendFlag = true;
        if (this.timer) {
          clearInterval(this.timer);
          this.timer = null;
        }
        let data = {
          IdentityUserId: this.form.IdentityUserId,
        };
        if (this.status === "lock") {
          data.MfaType = 1;
        }
        let res = await sendMFAEmail(data);
        this.sendFlag = false;
        if (res.IsSuccess) {
          this.flag = true;
          this.second = 60;
          this.timer = setInterval(() => {
            this.second--;
            if (this.second <= 0) {
              this.flag = false;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000);
        }
      } catch (err) {
        console.log(err);
        this.flag = false;
        this.sendFlag = false;
      }
    },
  },
  destroyed() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
};
</script>
<style lang="scss" scoped>
.tip_mfa {
  width: 86%;
  margin: auto;
  margin-bottom: 20px;
  text-align: left;
  padding: 0 10px;
  line-height: 30px;
  border-radius: 5px;
  background-color: #eee;
  i {
    margin-right: 5px;
  }
}
::v-deep .el-dialog__header {
  font-weight: bold;
}
</style>