<template>
  <el-dialog :close-on-click-modal="false" title="修改密码" :visible.sync="dialogVisible">
    <el-form :model="resepassword" :rules="resetRules" ref="resetRef" label-width="100px" >
        <el-form-item label="账号 :" prop="userId">
          <el-input clearable v-model="resepassword.userId" disabled></el-input>
        </el-form-item>
        <el-form-item label="旧密码 :" prop="oldpassword">
          <el-input clearable v-model="resepassword.oldpassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码 :" prop="newpassword">
          <el-input clearable v-model="resepassword.newpassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码 :" prop="confirmpassword">
          <el-input clearable v-model="resepassword.confirmpassword" type="password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import { resetPw } from '@/api/system_manage'
export default {
  data() {
    return {
      dialogVisible: false,
      resepassword: {
        userId: ''
      },
      resetRules: {
        userId: [
          { required: true, message: '', trigger: 'blur' }
        ],
        oldpassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newpassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        confirmpassword: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    value: {
      type: Boolean
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      if (val) {
        this.resepassword.userId = this.$store.state.app.userInfo.displayname
      }
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  methods: {
    submit() {
      this.$refs.resetRef.validate(valid => {
        if (valid) {
          if (
            this.resepassword.newpassword !== this.resepassword.confirmpassword
          ) {
            this.$message({
              type: 'warning',
              message: '新密码和确认密码不一致, 请重新填写!'
            })
            return false
          }
          resetPw({
            originalPassword: this.resepassword.oldpassword,
            newPassword: this.resepassword.newpassword,
            confirmedPassword: this.resepassword.newpassword,
            userId: this.resepassword.userId
          }).then(res => {
            console.log(res)
            if (!res) return false
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            this.dialogVisible = false
          })
        }
      })
    }
  }
}
</script>
