<template>
  <el-dialog
    class="danger-tips-dialog"
    :visible.sync="modalIsShow"
    width="400px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    append-to-body
    destroy-on-close
    @opened="visibleChange"
  >
    <p class="tips-info tips-error mb-2">确定要清除当前学校的基础数据集吗？</p>
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item prop="loginName" label="用户名：">
        <el-input
          v-model.trim="modalForm.loginName"
          placeholder="请输入你的用户名"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码：">
        <el-input
          v-model.trim="modalForm.password"
          type="password"
          placeholder="请输入你的密码"
          show-password
          clearable
        >
        </el-input>
      </el-form-item>
    </el-form>

    <template slot="footer">
      <template v-if="confirmTime % 2">
        <el-button type="primary" :loading="loading" @click="submit"
          >确认</el-button
        >
        <el-button @click="cancel">取消</el-button>
      </template>
      <template v-else>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" :loading="loading" @click="submit"
          >确认</el-button
        >
      </template>
    </template>
  </el-dialog>
</template>

<script>
import { schoolSetDataBackup } from "../../api";
import { MD5 } from "@/plugins/md5";

export default {
  name: "data-clear-tips-dialog",
  props: {
    school: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      loading: false,
      confirmTime: 0,
      maxConfirmTime: 3,
      modalForm: { loginName: "", password: "" },
      rules: {
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "change",
          },
        ],
        loginName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = { loginName: "", password: "" };
      this.confirmTime = 0;

      this.$nextTick(() => {
        this.$refs.modalFormComp.resetFields();
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      const user = this.$ls.get("user", {});
      if (
        this.modalForm.loginName !== user.loginName ||
        MD5(this.modalForm.password) !== user.pwd
      ) {
        this.$message.error("用户名或密码输入错误！");
        return;
      }

      this.confirmTime += 1;
      if (this.confirmTime < this.maxConfirmTime) {
        this.modalForm = { loginName: "", password: "" };
        return;
      }

      if (this.loading) return;
      this.loading = true;
      const res = await schoolSetDataBackup(this.school.id).catch(() => {});
      this.loading = false;
      if (!res) return;

      this.$message.success("操作成功！");
      this.cancel();
    },
  },
};
</script>
