<script setup>
import { checkPassWord } from '@/utils/common'
import { Base64 } from 'js-base64'
import { apiSystemUser } from '@/api/system'


const show = defineModel('show', { type: Boolean, default: false })
const props = defineProps({
  data: Object
})
const emits = defineEmits(["success"])


let form = reactive({
  password: '',
  check: ''
})
watch(() => props.isShow, (newval) => {
  if (newval) {
    formRef.value?.resetFields()
    form.password = ''
    form.check = ''
  }
})
let formRef = ref(null)
const rules = reactive({
  password: [{
    validator: (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      }
      if (form.check !== '') {
        formRef.value?.validateField('check')
      }
      if (!checkPassWord(value)) {
        callback(new Error('密码要求8位及以上，包含数字、大写字母、小写字母、特殊字符中的三个类型'))
      } else {
        callback()
      }
    }, trigger: 'blur'
  }],
  check: [{
    validator: (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入确认密码'))
      } else if (value !== form.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }, trigger: 'blur'
  }]
})
async function handleSubmitForm() {
  await formRef.value?.validate()
  const res=await apiSystemUser.resetUserPwd({
    sysuserid:props.data.SysUserId,
    password:Base64.encode(form.password),
    repassword:Base64.encode(form.check),
  })
  if(res.Ret!==200) return
  ElMessage.success('重置密码成功')
  show.value=false
  emits("success")
}
</script>

<template>
  <el-dialog
    v-model="show"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    width="30%"
    draggable
    title="重置密码"
  >
    <div class="dialog-content">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="账号">
          <span>{{ props.data.SysRealName }}</span>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input
            v-model="form.password"
            style="width: 100%"
            :show-password="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="check">
          <el-input
            v-model="form.check"
            style="width: 100%"
            :show-password="true"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="btn-content">
        <el-button
          type="primary"
          style="width: 80px; margin-right: 24px"
          @click="handleSubmitForm"
          >保存</el-button
        >
        <el-button style="width: 80px" @click="show=false"
          >取消</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

  .btn-content {
    text-align: center;
    padding-bottom: 20px;
  }

</style>
