<template>
  <el-form
    ref="updatePassForm"
    :model="changePassUser"
    :rules="rules"
    label-width="80px"
    class="demo-ruleForm"
  >
    <el-form-item label="当前用户" prop="username">
      <el-input
        v-model="changePassUser.username"
        type="username"
      />
    </el-form-item>

    <el-form-item label="旧密码" prop="oldPassword">
      <el-input
        v-model="changePassUser.oldPassword"
        placeholder="请输入旧密码"
        show-password
      />
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input
        v-model="changePassUser.newPassword"
        placeholder="请输入新密码"
        type="password"
        utocomplete="off"
        show-password
      />
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input
        v-model="changePassUser.confirmPassword"
        placeholder="请确认新密码"
        type="password"
        show-password
      />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        size="mini"
        @click="updatePass('updatePassForm')"
      >保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

// import { updateUserPassword } from '@/api/user'
// import { updateUserPwd } from '@/api/system/user'
export default {
  data() {
    var validatePass = (_rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.changePassUser.confirmPassword !== '') {
          this.$refs.updatePassForm.validateField('confirmPassword')
        }
        callback()
      }
    }
    var validatePass2 = (_rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.changePassUser.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      changUserDiaVisible: false,
      changPassDiaVisible: false,
      search: '',
      changePassUser: {
        username: '',
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      // 表单校验
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        oldPassword: [
          { required: true, message: '旧密码不能为空', trigger: 'blur' },
          {
            min: 4,
            max: 12,
            message: '密码长度在4-12个字符之间',
            trigger: 'blur'
          }
        ],
        newPassword: [
          { required: true, validator: validatePass, trigger: 'blur' },
          {
            min: 4,
            max: 12,
            message: '密码长度在4-12个字符之间',
            trigger: 'blur'
          }
        ],
        confirmPassword: [
          { required: true, validator: validatePass2, trigger: 'blur' },
          {
            min: 4,
            max: 12,
            message: '密码长度在4-12个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    allUsers() {
      return this.$store.getters.allUsers
    },
    roles() {
      return this.$store.getters.allRoles
    }
    // authorities() {
    //   return this.$store.getters.allAuthorities
    // }
  },
  created() {
    console.log('created')
    this.$store.dispatch('user/getAllUsers')
    // this.$store.dispatch('authority/getAllAuthorities')
  },
  cancel() {
    this.$store.dispatch('role/getAllRoles')
    this.$store.dispatch('user/getAllUsers')
    this.changUserDiaVisible = false
    this.changPassDiaVisible = false
  },
  methods: {
    updatePass(formName) {
      console.log('start to update user password-1', this.changePassUser)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('start to update user password0', this.changePassUser)
          var user = {}
          user.username = this.changePassUser.username
          user.password = this.changePassUser.newPassword
          this.$store
            .dispatch('user/updateUserPassword', user)
            .then((res) => {
              if (res.data.success) {
                this.$message({ message: '修改用户密码成功', type: 'success' })
              }
            })
            .catch((_err) => {})
        }
      })
    },
    close() {
      this.$tab.closePage()
    }
  }
}
</script>
