<template>
  <div class="edit-password">
    <el-breadcrumb separator="/" class="head-nav">
      <el-breadcrumb-item :to="{ path: '/userCenter' }"
        >用户中心</el-breadcrumb-item
      >
      <el-breadcrumb-item>修改登录密码</el-breadcrumb-item>
    </el-breadcrumb>
    <p class="title">修改登录密码：</p>
    <el-form
      label-position="right"
      label-width="120px"
      :model="passwordForm"
      class="form"
    >
      <el-form-item label="手机号" class="form-item" v-if="passwordForm.phone">
        <span>{{ passwordForm.phone | filterPhone }}</span>
      </el-form-item>
      <el-form-item label="邮箱" class="form-item" v-if="passwordForm.email">
        <span>{{ passwordForm.email }}</span>
      </el-form-item>
      <el-form-item label="输入新密码" class="form-item">
        <el-input
          v-model="passwordForm.pwd"
          placeholder="请输入新密码，8~20位包含数字、大小写字母"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item label="验证码" class="form-item form-code">
        <el-input
          v-model="passwordForm.code"
          placeholder="请输入6位验证码"
        ></el-input>
        <code-btn @sendCode="sendCode" ref="child"></code-btn>
      </el-form-item>
    </el-form>
    <el-button class="confirm-btn" type="primary" @click="confirm()"
      >提交修改</el-button
    >
  </div>
</template>
<script >
import Vue from "vue";
import CodeBtn from "./VerCode.vue";
import { editPassword } from "./Api.service";
export default Vue.extend({
  components: {
    CodeBtn,
  },
  data() {
    return {
      passwordForm: {
        phone: this.$store.state.userInfos.phone,
        email: this.$store.state.userInfos.email,
        pwd: "",
        code: "",
      },
    };
  },
  created() {
    if (
      !this.$store.state.userInfos.phone &&
      !this.$store.state.userInfos.email
    ) {
      this.$router.push("/userCenter");
    }
  },
  methods: {
    // 验证码发送成功的回调
    async sendCode() {
      this.$message({
        message: "发送成功",
        type: "success",
      });
    },
    async confirm() {
      if (this.passwordForm.pwd.trim() === "") {
        this.$message("密码不可为空");
        return;
      } else if (this.passwordForm.code.trim() === "") {
        this.$message("验证码不可为空");
        return;
      }
      let info = {};
      if (this.$store.state.userInfos.phone !== "") {
        info = {
          phone: this.passwordForm.phone,
          code_type: 0,
          pwd: this.$md5(
            String(this.passwordForm.pwd) +
              this.$md5(String(this.passwordForm.pwd.length))
          ),
          code: this.passwordForm.code,
        };
      } else if (this.$store.state.userInfos.email !== "") {
        info = {
          email: this.passwordForm.email,
          pwd: this.$md5(
            String(this.passwordForm.pwd) +
              this.$md5(String(this.passwordForm.pwd.length))
          ),
          code: this.passwordForm.code,
          code_type: 1,
        };
      }
      const res = await editPassword(info);
      if (res && res.code === 200) {
        this.$message({
          message: "密码修改成功",
          type: "success",
        });
        //   this.$notify.success(res.message);
        this.$router.push("/userCenter");
      }
    },
  },
});
</script>
<style scoped>
.edit-password {
  padding: 16px;
  min-height: calc(100vh - 95px);
  min-width: 1200px;
  background: #f8fafb;
}
.head-nav {
  margin: 12px 0 40px 47px;
}
.title {
  margin-left: 47px;
  font-size: 20px;
  font-family: PingFangSC-Semibold;
  font-weight: 600;
  color: rgba(53, 53, 53, 1);
  line-height: 40px;
}
.form {
  width: 900px;
}
.form .el-input {
  width: 610px;
}
.form-code .el-input {
  width: 480px;
}
.form-item {
  margin-bottom: 28px;
}
.confirm-btn {
  margin-top: 10px;
  margin-left: 50px;
}
</style>

<style>
.edit-password .el-form-item__label {
  margin-right: 21px;
}
</style>

