<script setup>
import { ref } from 'vue'
import { User } from '@element-plus/icons-vue'
import { PasswdService } from '@/api/user'
const dialogTableVisible = ref(false)
const form = ref()
const formModel = ref({
  id: '',
  oldPasswd: '',
  newPasswd: '',
  rePasswd: ''
})
// 整个表单的校验规则
const rules = {
  oldPasswd: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
  newPasswd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{8,30}$/,
      message: '新密码必须是 8~30 位非空字符 ',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        // 判断当前的 value 和 from 中收集的 password 是否一致
        if (value === formModel.value.oldPasswd) {
          callback(new Error('新旧密码不能一致'))
        } else {
          // 校验成功，callback()
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  rePasswd: [
    { required: true, message: '请输入确认密码', trigger: 'blur' },
    {
      pattern: /^\S{8,30}$/,
      message: '密码必须是 8~30 位非空字符 ',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        // 判断当前的 value 和 from 中收集的 password 是否一致
        if (value !== formModel.value.newPasswd) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          // 校验成功，callback()
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}
// 组件对外暴露一个方法 open ，基于 open 传来的参数，区分添加还是编辑
const open = (userId) => {
  formModel.value.id = userId
  dialogTableVisible.value = true
}
// 向外暴露
defineExpose({
  open
})
const emit = defineEmits(['success'])

const close = () => {
  dialogTableVisible.value = false
  form.value.clearValidate()
  // 重置数据
  formModel.value = {
    id: '',
    oldPasswd: '',
    newPasswd: '',
    rePasswd: ''
  }
}
// 提交修改
const submit = async () => {
  await form.value.validate()
  ElMessageBox.confirm('确认提交?', '提示', {
    confirmButtonText: 'OK',
    cancelButtonText: 'Cancel',
    type: 'warning'
  })
    .then(async () => {
      // 调用修改接口
      await PasswdService(formModel.value)
      ElMessage.success('密码修改成功')
      close()
      emit('success')
    })
    .catch(() => {})
}
</script>
<template>
  <el-dialog
    v-model="dialogTableVisible"
    title="修改密码"
    width="500"
    @close="close"
  >
    <h1>提示：新密码至少长度大于10，同时至少包含大小写字符、数字和特殊字符</h1>
    <hr />
    <el-form
      :model="formModel"
      :rules="rules"
      ref="form"
      size="large"
      autocomplete="off"
    >
      <el-form-item prop="oldPasswd">
        <el-input
          v-model="formModel.oldPasswd"
          :prefix-icon="User"
          placeholder="请输入旧密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="newPasswd">
        <el-input
          :prefix-icon="User"
          placeholder="请输入新密码"
          v-model="formModel.newPasswd"
        ></el-input>
      </el-form-item>
      <el-form-item prop="rePasswd">
        <el-input
          v-model="formModel.rePasswd"
          :prefix-icon="User"
          placeholder="请输入确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          class="button"
          type="success"
          auto-insert-space
          @click="submit"
          >提交修改</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
