<template>
  <el-dialog
    class="reset-pwd"
    :visible.sync="modalIsShow"
    title="修改密码"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <div class="part-box part-box-pad part-box-border">
      <el-form
        ref="modalFormComp"
        :model="resetModel"
        :rules="resetRules"
        label-width="100px"
      >
        <el-form-item prop="oldPassword" label="旧密码：">
          <el-input
            size="large"
            type="password"
            v-model="resetModel.oldPassword"
            placeholder="请输入旧密码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="新密码：">
          <el-input
            size="large"
            type="password"
            v-model="resetModel.password"
            placeholder="请输入新密码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="rePassword" label="再次密码：">
          <el-input
            size="large"
            type="password"
            v-model="resetModel.rePassword"
            placeholder="请再次输入新密码"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button type="danger" @click="cancel" plain>取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updatePwd } from "../api";
import { password } from "@/plugins/formRules";
import { Base64 } from "@/plugins/crypto";

const initModalForm = {
  id: "",
  oldPassword: "",
  password: "",
  rePassword: ""
};
export default {
  name: "reset-pwd",
  props: {
    userPassword: {
      type: String
    }
  },
  data() {
    const equalToPswd = (rule, value, callback) => {
      if (value !== this.resetModel.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      modalIsShow: false,
      isSubmit: false,
      resetModel: { ...initModalForm },
      resetRules: {
        oldPassword: password,
        password,
        rePassword: [
          ...password,
          {
            validator: equalToPswd,
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      this.resetModel = { ...initModalForm };
      this.resetModel.oldPassword = this.userPassword || "";
      this.resetModel.id = this.$ls.get("user", { id: "" }).id;
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const datas = {
        id: this.resetModel.id,
        oldPassword: Base64(this.resetModel.oldPassword),
        password: Base64(this.resetModel.password)
      };
      const data = await updatePwd(datas).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

      this.isSubmit = false;
      this.$message.success("修改成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
