<template>
  <div class="password-form">
    <el-col :span="12" :offset="5">
      <el-form :model="passwordTemp" :rules="passwordRules" ref="changePasswordForm" label-width="100px" label-suffix="：">
        <el-form-item label="旧密码" prop="oldPassword" :verify="['NotNull', 'Length<33']">
          <el-input type="password" v-model="passwordTemp.oldPassword" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input type="password" v-model="passwordTemp.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="重复新密码" prop="confirmPassword">
          <el-input type="password" v-model="passwordTemp.confirmPassword" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
    </el-col>
  </div>
</template>

<script>
import util from '@src/common/util.js'
export default {
  data() {
    return {
      passwordTemp: {
        oldPassword: '',
        password: '',
        confirmPassword: ''
      },
      passwordVeriry: {
        maxLen: 18,
        minLen: 5
      },
      passwordRules: {
        confirmPassword: [
          { required: true, message: '请重复输入一次密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value !== this.passwordTemp.password) {
                callback(new Error('两次输入密码不一致!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value.length < this.passwordVeriry.minLen || value.length > this.passwordVeriry.maxLen) {
                callback(new Error(`必须是${this.passwordVeriry.minLen}-${this.passwordVeriry.maxLen}位的字符`))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted() {
    this.initPwdCheck()
  },
  methods: {
    async initPwdCheck() {
      let res = await axios.get('/ui/users/initpwdcheck')
      if (res.data.status === 1) {
        this.passwordVeriry = {maxLen: res.data.maxLen, minLen: res.data.minLen}
      }
    },
    async changePassword() {
      try {
        await util.validateForm(this.$refs['changePasswordForm'])
      } catch (e) {
        util.showErrorMessageBox(e)
        return
      }
      let res = await axios
        .put('/ui/application/changepassword', {
          oldPassword: this.passwordTemp.oldPassword,
          password: this.passwordTemp.password,
          confirmPassword: this.passwordTemp.confirmPassword
        })
      util.showResponseMessage(res.data)
      if (res.data.status === 1) {
        this.$refs['changePasswordForm'].resetFields()
        this.$emit('close-dialog')
      }
    }
  }
}
</script>

<style scoped>
.password-form {
  margin-top: 50px;
  height: 200px;
}
.el-dialog__footer{
  padding: 0;
}
</style>
