<template>
  <div class="edit-pwd-container">
    <el-form ref="pwdFormRef" :model="pwdForm" :rules="pwdRules" label-width="80px" class="pwd-form">
      <el-form-item label="原密码" prop="oldPassword">
        <el-input v-model="pwdForm.oldPassword" type="password" placeholder="请输入原密码" show-password size="default" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="pwdForm.newPassword" type="password" placeholder="请输入新密码" show-password size="default" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="pwdForm.confirmPassword" type="password" placeholder="请确认新密码" show-password size="default" maxlength="20"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const emit = defineEmits(['success'])

const pwdFormRef = ref(null)


const pwdForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 表单验证规则
const pwdRules = reactive({
  oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 8, max: 20, message: '密码长度必须在8-20位之间', trigger: 'blur' },
    { pattern: /^(?=.*[a-zA-Z])(?=.*\d).+$/, message: '密码必须包含字母和数字', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请确认新密码'))
        } else if (value !== pwdForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})

// 提交表单
const submitForm = async () => {
  if (!pwdFormRef.value) return
  try {
    // 表单验证
    const valid = await pwdFormRef.value.validate()
    if (!valid) return
    await ElMessageBox.confirm(
      '确认要修改密码吗?',
      '温馨提示',
      { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning' }
    )
    await new Promise(resolve => setTimeout(resolve, 800))
    emit('success')
  } catch (error) {
    if (error !== 'cancel' && !(error instanceof Error && error.message.includes('cancel'))) {
      ElMessage.error(error.message || '密码修改失败，请稍后重试')
    }
  }
}

// 重置表单
const resetForm = () => {
  if (pwdFormRef.value) {
    pwdFormRef.value.resetFields()
    pwdForm.oldPassword = ''
    pwdForm.newPassword = ''
    pwdForm.confirmPassword = ''
  }
}
defineExpose({ submitForm, resetForm })
</script>

<style scoped>
.edit-pwd-container {
  border: none !important;
  background: transparent !important;
  padding: 0 !important;
  box-shadow: none !important;
}

.pwd-form {
  width: 100%;
}

:deep(.el-form-item) {
  margin-bottom: 16px;
}

:deep(.el-form-item:last-child) {
  margin-bottom: 0;
}

:deep(.el-input) {
  --el-input-height: 32px !important;
  width: 100%;
  --el-input-border-radius: 4px;
}

:deep(.el-form-item__label) {
  font-size: 13px;
  color: #303133;
  font-weight: 500;
}
:deep(.el-input__wrapper) {
  padding: 0 10px;
  height: 32px !important;
}

:deep(.el-input__inner) {
  height: 32px !important;
  line-height: 32px !important;
  font-size: 13px;
}

:deep(.el-input__suffix-inner) {
  height: 32px !important;
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.el-icon) {
  font-size: 14px !important;
}
</style>