<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">
        <div style="display: flex;justify-content: space-between;width: 90%;">
          <el-input :placeholder="$t('mfa:form:input:placeholder:Codes')" v-model="form.Code" />
          <el-button size="small" @click.stop="sendMFACode" :disabled="flag || sendFlag" style="margin-left: 10px;"
            class="codeBtn">{{
              flag ?
                `${$t("mfa:form:sendMFACodeCountDown")} (${second}s)` : $t("mfa:form:sendMFACode") }}</el-button>
        </div>
      </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>
      <p style="text-align: left;font-size: 14px;margin:10px auto;width: 80%;">
        <el-checkbox v-model="form.isRemember" /><span style="margin-left: 10px;">{{ $t("mfa:tip:noLogin") }}</span>
      </p>
    </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,
        isRemember: true
      },
      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",
          },
        ],
      },
    };
  },
  mounted() {
    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);
  },
  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>
.codeBtn {
  color: #409EFF;
  border-color: #409EFF;
}

.codeBtn.is-disabled,
.codeBtn.is-disabled:focus,
.codeBtn.is-disabled:hover {
  color: #c0c4cc;
  cursor: not-allowed;
  background-image: none;
  background-color: #fff;
  border-color: #ebeef5;
}

.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>