<template>
  <div class="app-container common-list-page">
    <el-form
      :model="resetForm"
      :rules="resetFormRules"
      ref="resetForm"
      status-icon
      label-width="100px"
    >
      <el-form-item label="旧密码：" prop="oldPassword">
        <el-input type="password" v-model="resetForm.oldPassword" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码：" prop="newPassword">
        <el-input type="password" v-model="resetForm.newPassword" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码：" prop="newpassword1">
        <el-input type="password" v-model="resetForm.newpassword1" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
<!--        <el-button type="primary" @click.native.prevent="toAmend">确认修改</el-button>-->
        <el-button type="primary" @click="toAmend">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// import { mapGetters } from 'vuex'
import { queryOlderP, updateP } from '@/api/updatePassword'
export default {
  data() {
    // 必须包含大小写字母、数字、特殊字符长度再9-16位之间
    var valiOldPwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'))
      } else {
        callback()
      }
    }
    var regex = new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$')
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (!regex.test(value)) {
        callback(new Error('必须包含字母、数字,长度为6-16位之间'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.resetForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      user: JSON.parse(sessionStorage.getItem('user')),
      resetForm: {
        newpassword1: '',
        oldPassword: '',
        newPassword: ''
      },
      resetFormRules: {
        oldPassword: [
          { required: true, validator: valiOldPwd, trigger: 'blur' }
        ],
        newPassword: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        newpassword1: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
    // this.aaa()
  },
  methods: {
    // 查询原先密码
    getList() {
      // const data = {
      //   userId: this.user.primaryKey
      // }
      // queryOlderP(data).then(res => {
      //   if (res.code === 0) {
      //     this.resetForm.oldPassword = res.data.password
      //   }
      // })
    },
    // 修改密码
    toAmend() {
      this.$refs.resetForm.validate(valid => {
        if (valid) {
          // console.log('=================')
          const data = {
            userId: this.user.primaryKey,
            newPassword: this.resetForm.newPassword,
            oldPassword: this.resetForm.oldPassword
          }
          updateP(data).then(res => {
            if (res.code === 0) {
              const that = this
              that.$message({
                message: '修改密码成功',
                type: 'success',
                center: true,
                onClose() {
                  that.dialogVisible = false
                  that.getList()
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        //   api.materialQuery.toAmend(this.resetForm)
        //     .then(res => {
        //       if (res.code === 2){
        //         this.$message({
        //           message: res.msg,
        //           type: 'error',
        //           duration: '2000'
        //         })
        //         return false
        //       }
        //       if (res.code === 0) {
        //         this.$message.success('修改成功,3秒后跳转到登录页！');
        //         setTimeout(() => {
        //           this.logout()
        //         }, 3000)
        //       }
        //       ic
        //     })
        //     .catch(() => {})
        }
      })
    }
    // async logout() {
    //   await this.$store.dispatch('user/logout');
    //   this.$router.push(`/login`);
    // }
  }
}
</script>
<style lang="scss" scoped>
  .el-form {
    width: 60%;
    margin: 50px auto 0;
    text-align: center;
    button {
      margin: 20px 0 0;
    }
  }
</style>
