<template>
  <div class="norm-form">
    <el-dialog :model-value="visible" @close="close" @open="open" width="8rem" destroy-on-close center align-center>
      <template #header>
        <span class="ml10" style="font-size: 18px">修改密码</span>
      </template>
      <el-form
        :model="formInfo"
        ref="formInfoRef"
        :rules="formInfoRules"
        width="100%"
        label-width="140px"
        label-suffix=":"
      >
        <el-form-item label="原密码" prop="originalPassword">
          <el-input
            show-password
            @input="clearK($event, 'originalPassword')"
            maxlength="20"
            v-model="formInfo.originalPassword"
            :rows="3"
            placeholder="请输入原密码"
            resize="none"
          />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            show-password
            @input="clearK($event, 'newPassword')"
            maxlength="20"
            v-model="formInfo.newPassword"
            :rows="3"
            placeholder="请输入新密码"
            resize="none"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            show-password
            @input="clearK($event, 'confirmPassword')"
            maxlength="20"
            v-model="formInfo.confirmPassword"
            :rows="3"
            placeholder="请输入确认密码"
            resize="none"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button class="w90-h40" type="primary" @click="confirm(formInfoRef)" v-blur>确定</el-button>
          <el-button class="w90-h40" @click="cancel">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { commonCheck } from '@/utils/validate.js'
import { ElMessage } from 'element-plus'

const props = defineProps({
  visible: Boolean,
})

const emits = defineEmits(['update:visible', 'confirm'])

const formInfo = ref({
  originalPassword: '',
  newPassword: '',
  confirmPassword: '',
})
const formInfoRef = ref(null)
let formInfoRules = reactive({
  originalPassword: [{ required: true, validator: commonCheck, message: '内容不能为空！', trigger: 'blur' }],
  newPassword: [{ required: true, validator: commonCheck, trigger: 'blur' }],
  confirmPassword: [{ required: true, validator: commonCheck, trigger: 'blur' }],
})

const open = async () => {
  // formInfo.value = Object.assign(formInfo.value, props.detail.row)
}

const clearK = ($event, key) => {
  formInfo.value[key] = $event.trim()
}
const close = () => {
  formInfo.value = {}
  formInfoRef.value.resetFields()
  emits('update:visible', false)
}
const confirm = async EL => {
  if (!EL) return
  await EL.validate(valid => {
    if (valid) {
      if (formInfo.value.newPassword === formInfo.value.confirmPassword) {
        const temp = { ...formInfo.value }
        emits('confirm', temp)
        // emits('update:visible', false)
      } else {
        ElMessage.warning('两次密码输入不一致')
      }
    }
  })
}
const cancel = () => {
  emits('update:visible', false)
}
defineExpose({
  open,
  close,
})
</script>

<style lang="scss" scoped>
.norm-form {
  :deep(.el-dialog__header) {
    display: flex;
    align-items: center;
    margin-right: 0;
    padding-bottom: 13px;
    border-bottom: 1px solid #d8d8d8;
  }

  .el-form-item {
    margin-bottom: 20px;

    .el-select {
      width: 100%;
    }

    .el-radio {
      width: 60px;
    }

    :deep(.el-input-number) {
      width: 100%;
      .el-input__inner {
        text-align: left;
      }
    }

    :deep(.el-autocomplete) {
      width: 100%;
    }
  }
}
</style>
