<template>
  <el-dialog
    custom-class="side-dialog"
    :visible.sync="modalIsShow"
    title="安全设置"
    width="500px"
    :modal="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form ref="modalFormComp">
      <el-form-item label="加密“题库”，“卷库”">
        <el-switch v-model="modalForm.safeEnable"></el-switch>
        <p class="tips-info">开启后，进入题库、卷库模块，需要进行密码验证！</p>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button type="primary" :loading="isSubmit" @click="confirm"
        >确定</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { questionSecuritySettingsApi } from "../api";
import { mapActions } from "vuex";
import { USER_SIGNIN } from "../../../modules/portal/store/user";

export default {
  name: "QuestionSafetySetDialog",
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {
        safeEnable: false,
      },
    };
  },
  methods: {
    ...mapActions([USER_SIGNIN]),
    visibleChange() {
      this.modalForm = {
        safeEnable: this.$store.state.user.safeEnable,
      };
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async confirm() {
      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await questionSecuritySettingsApi(this.modalForm).catch(
        () => {}
      );
      this.isSubmit = false;

      if (!data) return;

      let user = this.$store.state.user;
      user.safeEnable = this.modalForm.safeEnable;
      this.USER_SIGNIN(user);

      this.$message.success("设置成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
