<template>
  <div class="reset-paasword pg-main">
    <div class="pg-title">
      <span>修改密码</span>
    </div>
    <div class="pg-select-infos">
      <div class="form-body" style="width: 500px; padding-bottom: 20px">
        <Form
          ref="editPasswordForm"
          :model="editPasswordForm"
          :label-width="100"
          label-position="right"
          :rules="passwordValidate"
        >
          <FormItem
            label="原密码"
            prop="oldPass"
            :error="oldPassError"
            width="100px"
          >
            <Input
              type="password"
              v-model="editPasswordForm.oldPass"
              placeholder="请输入现在使用的密码"
            ></Input>
          </FormItem>
          <FormItem label="新密码" prop="newPass">
            <Input
              type="password"
              v-model="editPasswordForm.newPass"
              placeholder="请输入新密码，至少6位字符"
            ></Input>
          </FormItem>
          <FormItem type="password" label="确认新密码" prop="rePass">
            <Input
              type="password"
              v-model="editPasswordForm.rePass"
              placeholder="请再次输入新密码"
            ></Input>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="saveEditPass">保存</Button>
          </FormItem>
        </Form>
      </div>
    </div>
  </div>
</template>

<script>
import { axios } from "../../libs/http";
export default {
  name: "reset-paasword",
  data() {
    // const validePhone = (rule, value, callback) => {
    //   var re = /^1[0-9]{10}$/;
    //   if (!re.test(value)) {
    //     callback(new Error("请输入正确格式的手机号"));
    //   } else {
    //     callback();
    //   }
    // };
    const valideRePassword = (rule, value, callback) => {
      if (value !== this.editPasswordForm.newPass) {
        callback(new Error("两次输入密码不一致"));
      } else {
        callback();
      }
    };
    return {
      oldPassError: "",
      editPasswordForm: {
        oldPass: "",
        newPass: "",
        rePass: "",
      },
      passwordValidate: {
        oldPass: [{ required: true, message: "请输入原密码", trigger: "blur" }],
        newPass: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { min: 6, message: "请至少输入6个字符", trigger: "blur" },
          { max: 32, message: "最多输入32个字符", trigger: "blur" },
        ],
        rePass: [
          { required: true, message: "请再次输入新密码", trigger: "blur" },
          { validator: valideRePassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    saveEditPass() {
      this.$refs["editPasswordForm"].validate((valid) => {
        if (valid) {
          this.savePassLoading = true;
          axios
            .post("/system/user/resetPwd", {
              oldPwd: this.editPasswordForm.oldPass,
              newPwd: this.editPasswordForm.newPass,
            })
            .then((response) => {
              let data = response.data;
              if (data.code == 0) {
                this.$Notice.success({
                  title: "系统提示",
                  desc: "<p>修改密码成功</p>",
                });
                this.editPasswordForm = {
                  oldPass: "",
                  newPass: "",
                  rePass: "",
                };
              }
            });
        }
      });
    },
  },
  mounted() {
    //this.init();
  },
};
</script>
