<template>
  <div class="change-password-modal">
    <el-dialog append-to-body title="修改密码" :visible="show" width="30%" :before-close="changeShow">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码" prop="pass">
          <el-input type="text" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="checkPass">
          <el-input type="text" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPassAgain">
          <el-input type="text" v-model="ruleForm.checkPassAgain" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')"> 取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { updateUserPwd } from '@/api/login'
export default {
  name: 'ChangePassword',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入原始密码'))
      } else if (value === this.ruleForm.checkPass) {
        callback(new Error('新旧密码一致'))
      } else {
        callback()
      }
    }
    var validatePassNew = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (value === this.ruleForm.pass) {
        callback(new Error('新旧密码一致'))
      } else if (value.length < 5) {
        callback(new Error('密码不能低于5位数'))
      } else {
        callback()
      }
    }
    var validatePassAgain = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value != this.ruleForm.checkPass) {
        callback(new Error('两次输入新密码不一致'))
      } else {
        callback()
      }
    }
    // 这里存放数据
    return {
      show: false,
      ruleForm: {
        pass: '',
        checkPass: '',
        checkPassAgain: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePassNew, trigger: 'blur' }],
        checkPassAgain: [{ validator: validatePassAgain, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submit() {
      this.show = !this.show
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let param = {
            newPassword: this.ruleForm.checkPass,
            oldPassword: this.ruleForm.pass
          }
          const { code } = await updateUserPwd(param)
          if (code == 200) {
            this.changeShow()
            this.$message.success('修改成功')
            this.ruleForm.pass = ''
            this.ruleForm.checkPass = ''
            this.ruleForm.checkPassAgain = ''
            this.$store.dispatch('user/loginOutCopy')
          }
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.changeShow()
    },
    changeShow() {
      this.show = !this.show
    }
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
