<template>
  <div>
    <yd-navbar title="修改密码">
      <!--  <router-link to="/yys" slot="left">
        <yd-navbar-back-icon></yd-navbar-back-icon>
      </router-link> -->
    </yd-navbar>
    <yd-cell-group>
      <yd-cell-item>
        <span slot="left">账 号：</span>
        <yd-input slot="right" v-model="username" disabled></yd-input>
      </yd-cell-item>
      <yd-cell-item>
        <span slot="left">原&nbsp;密&nbsp;码：</span>
        <yd-input
          slot="right"
          required
          type="password"
          v-model="oldPwd"
          placeholder="原密码"
          ref="oldPassword"
        ></yd-input>
      </yd-cell-item>
      <yd-cell-item>
        <span slot="left">新&nbsp;密&nbsp;码：</span>
        <yd-input
          slot="right"
          required
          type="password"
          min="6"
          v-model="newPwd"
          placeholder="新密码"
          ref="newPassword"
        ></yd-input>
      </yd-cell-item>
      <yd-cell-item>
        <span slot="left">确认密码：</span>
        <yd-input
          slot="right"
          required
          type="password"
          min="6"
          v-model="confirmPwd"
          placeholder="确认新密码"
          ref="confirmPassword"
        ></yd-input>
      </yd-cell-item>
    </yd-cell-group>
    <yd-button
      type="primary"
      shape="circle"
      bgcolor="#10AEFF"
      color="#FFF"
      style="width: 70%; height: 35px"
      @click.native="onSubmit"
      >修改</yd-button
    >
  </div>
</template>

<script>
export default {
  data() {
    return {
      userid: null,
      username: "", //账号
      oldPwd: "", //原密码
      newPwd: "", //新密码
      confirmPwd: "", //新密码
    };
  },
  created() {
    this.userid = localStorage.getItem("userid");
    if (!this.userid) {
      this.$dialog.toast({
        mes: "获取不到您的用户标识，请重新登陆",
      });
      return;
    }
    var username = localStorage.getItem("username");
    if (!username) {
      this.$router.push("/yys_login");
    } else {
      this.username = username;
    }
  },
  mounted() {},
  methods: {
    onSubmit() {
      // const mob = _this.$refs.username
      const oldPwd = this.$refs.oldPassword;
      const newPwd = this.$refs.newPassword;
      const confirmPwd = this.$refs.confirmPassword;

      if (oldPwd.iserror) {
        this.$notify('原密码' + oldPwd.errorMsg);
        return;
      }
      if (newPwd.iserror) {
        this.$notify('新密码' + newPwd.errorMsg);
        return;
      }
      if (confirmPwd.iserror) {
        this.$notify('确认密码' + confirmPwd.errorMsg);
        return;
      }
      if (newPwd.value != confirmPwd.value) {
        this.$notify('两次密码不一致');
        return;
      }
      var data = {
        userid: this.userid,
        username: this.username,
        oldPwd: oldPwd.value.trim(),
        newPwd: newPwd.value.trim(),
      };
      this.$http.post("/updatePwd", JSON.stringify(data))
          .then(({ data }) => {
            if (data.success) {
              this.$notify({type: 'success', message: data.message});
              this.$router.push("/yys");
            } else {
              this.$notify(data.message);
            }
          });
    },
  },
};
</script>
<style scoped="less">
</style>