<template>
  <el-dialog title="重置密码" :visible.sync="visible" width="500px" append-to-body @close="handleClose">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" status-icon>
      <el-form-item label="旧密码" prop="userPassword">
        <el-input v-model="form.userPassword" placeholder="请输入旧密码" type="password" show-password clearable size="small" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="form.newPassword" placeholder="请输入新密码" type="password" show-password clearable size="small" />
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" placeholder="请再次输入新密码" type="password" show-password clearable size="small" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" :loading="loading" @click="submitForm">确 定</el-button>
      <el-button size="small" @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updatePassword } from '@/api/generatedApi/system'

export default {
  name: 'ResetPasswordDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: ''
    }
  },
  data() {
    // 确认密码验证
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.form.newPassword) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        userPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        userPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
        newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }, { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }],
        confirmPassword: [{ required: true, message: '请再次输入新密码', trigger: 'blur' }, { validator: validateConfirmPassword, trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            userId: this.userId,
            userPassword: this.form.userPassword,
            newPassword: this.form.newPassword
          }

          updatePassword(data)
            .then(response => {
              if (response.code === '0') {
                this.$message.success('密码重置成功')
                this.handleClose()
                this.$emit('success')
              } else {
                this.$message.error(response.msg || '密码重置失败')
              }
            })
            .catch(error => {
              console.error('密码重置出错：', error)
              this.$message.error('密码重置出错')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}
</style>
