<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
    <el-form :formData="formData" :rules="formRule">
      <el-form-item label="密码">
        <el-input v-model="formData.password" placeholder="请输入用户密码"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submit" :disabled="submitDisable">确定</el-button>
        <el-button type="default" @click="dialogVisible = false">取消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts" name="UpdateUserPasswordTable">
import { ElMessage } from 'element-plus';
import { updatePassword } from '@/api/member/user'
defineOptions({ name: 'UpdateUserPasswordTable' })
const { t } = useI18n() // 国际化
const submitDisable = ref(false) // 提交按钮是否禁用


const formData = ref({
  userId: undefined,
  password: undefined,
}) // 表单数据
const formRule = reactive({
  password: [{ required: true, message: '密码不能为空', trigger: 'burl' }],
})
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  formData.value.userId = id as any
  dialogVisible.value = true
  dialogTitle.value = '重置密码'
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗


const submit = async () => {
  submitDisable.value = true
  try {
    await updatePassword(formData.value)
    ElMessage.success(t('common.success'))
    dialogVisible.value = false
  } finally {
    submitDisable.value = false
  }
}
</script>