zhou zhou
4 天以前 33bd4dd1f0e41131cd8e5bbf87204a1f0b72bb08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
  <ElDialog
    :title="t('pages.basicInfo.whMat.batchGroupDialog.title')"
    :model-value="visible"
    width="640px"
    align-center
    destroy-on-close
    @update:model-value="handleCancel"
    @closed="handleClosed"
  >
    <ArtForm
      ref="formRef"
      v-model="form"
      :items="formItems"
      :rules="rules"
      :span="24"
      :gutter="20"
      label-width="120px"
      :show-reset="false"
      :show-submit="false"
    />
 
    <template #footer>
      <span class="dialog-footer">
        <ElButton @click="handleCancel">{{ t('common.cancel') }}</ElButton>
        <ElButton type="primary" @click="handleSubmit">{{ t('common.confirm') }}</ElButton>
      </span>
    </template>
  </ElDialog>
</template>
 
<script setup>
  import { computed, nextTick, reactive, ref, watch } from 'vue'
  import { useI18n } from 'vue-i18n'
  import ArtForm from '@/components/core/forms/art-form/index.vue'
 
  const props = defineProps({
    visible: { type: Boolean, default: false },
    groupOptions: { type: Array, default: () => [] }
  })
 
  const emit = defineEmits(['update:visible', 'submit'])
  const { t } = useI18n()
 
  const formRef = ref()
  const form = reactive({ groupId: '' })
 
  const formItems = computed(() => [
    {
      label: t('pages.basicInfo.whMat.dialog.fields.groupId'),
      key: 'groupId',
      type: 'treeselect',
      props: {
        data: props.groupOptions,
        props: {
          label: 'displayLabel',
          value: 'value',
          children: 'children'
        },
        placeholder: t('pages.basicInfo.whMat.dialog.placeholders.groupId'),
        clearable: false,
        checkStrictly: true,
        defaultExpandAll: true
      }
    }
  ])
 
  const rules = computed(() => ({
    groupId: [
      {
        required: true,
        message: t('pages.basicInfo.whMat.dialog.validation.groupId'),
        trigger: 'change'
      }
    ]
  }))
 
  function resetForm() {
    form.groupId = ''
    formRef.value?.clearValidate?.()
  }
 
  async function handleSubmit() {
    if (!formRef.value) return
    try {
      await formRef.value.validate()
      emit('submit', { ...form })
    } catch {
      return
    }
  }
 
  function handleCancel() {
    emit('update:visible', false)
  }
 
  function handleClosed() {
    resetForm()
  }
 
  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        resetForm()
        nextTick(() => {
          formRef.value?.clearValidate?.()
        })
      }
    },
    { immediate: true }
  )
</script>