广丰卷烟厂数采质量分析系统
zhuguifei
7 天以前 2b31fa203f3435a582be51f45899d99164c9917a
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue';
import type { SelectOption } from 'naive-ui';
import { NSelect } from 'naive-ui';
import { jsonClone } from '@sa/utils';
import { fetchCreateJudge, fetchUpdateJudge } from '@/service/api/qm/judge';
import { fetchGetStdList } from '@/service/api/qm/std';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';
 
defineOptions({
  name: 'JudgeOperateDrawer'
});
 
interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.Qm.Judge | null;
}
 
const props = defineProps<Props>();
 
interface Emits {
  (e: 'submitted'): void;
}
 
const emit = defineEmits<Emits>();
 
const visible = defineModel<boolean>('visible', {
  default: false
});
 
const { formRef, validate, restoreValidation } = useNaiveForm();
const { createRequiredRule } = useFormRules();
 
const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: '新增判定依据',
    edit: '编辑判定依据'
  };
  return titles[props.operateType];
});
 
const statusOptions: SelectOption[] = [
  { label: '启用', value: 1 },
  { label: '归档', value: 0 }
];
 
const categoryOptions: SelectOption[] = [
  { label: '成品', value: 0 },
  { label: '辅料', value: 1 }
];
 
const stdOptions = ref<SelectOption[]>([]);
 
type Model = Api.Qm.JudgeOperateParams;
 
const model = ref<Model>(createDefaultModel());
 
function createDefaultModel(): Model {
  return {
    matCode: '',
    matName: '',
    status: null,
    stdCod: '',
    cdate: null,
    oper: '',
    des: '',
    category: null,
    typeCode: '',
    typeName: '',
    judgeName: ''
  };
}
 
type RuleKey = Extract<keyof Model, 'id'>;
 
const rules: Record<RuleKey, App.Global.FormRule> = {};
 
function handleUpdateModelWhenEdit() {
  model.value = createDefaultModel();
 
  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model.value, jsonClone(props.rowData));
  }
}
 
function closeDrawer() {
  visible.value = false;
}
 
async function fetchStdOptions() {
  const response = await fetchGetStdList({ category: model.value.category });
  const stdListData = response.data?.rows || [];
 
  if (stdListData.length > 0) {
    stdOptions.value = stdListData.map((item: Api.Qm.Std) => ({
      label: item.stdName + item.stdCode,
      value: String(item.id)
    }));
  }
}
 
async function handleSubmit() {
  await validate();
 
  const { matCode, matName, status, stdCod, cdate, oper, des, category, typeCode, typeName, judgeName } = model.value;
 
  // request
  if (props.operateType === 'add') {
    const { error } = await fetchCreateJudge({
      matCode,
      matName,
      status,
      stdCod,
      cdate,
      oper,
      des,
      category,
      typeCode,
      typeName,
      judgeName
    });
    if (error) return;
  }
 
  if (props.operateType === 'edit') {
    const { error } = await fetchUpdateJudge({
      matCode,
      matName,
      status,
      stdCod,
      cdate,
      oper,
      des,
      category,
      typeCode,
      typeName,
      judgeName
    });
    if (error) return;
  }
 
  window.$message?.success($t('common.updateSuccess'));
  closeDrawer();
  emit('submitted');
}
 
watch(visible, () => {
  if (visible.value) {
    handleUpdateModelWhenEdit();
    restoreValidation();
 
    // Fetch stdOptions when the drawer becomes visible
    fetchStdOptions();
  }
});
 
watch(
  () => model.value.category,
  () => {
    fetchStdOptions();
  }
);
</script>
 
<template>
  <NDrawer v-model:show="visible" :title="title" display-directive="show" :width="800" class="max-w-90%">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <NFormItem label="判定名称" path="judgeName">
          <NInput v-model:value="model.judgeName" placeholder="请输入判定名称" />
        </NFormItem>
        <NFormItem label="物料牌号代码" path="matCode">
          <NInput v-model:value="model.matCode" placeholder="请输入物料牌号代码" />
        </NFormItem>
        <NFormItem label="物料牌号" path="matName">
          <NInput v-model:value="model.matName" placeholder="请输入物料牌号" />
        </NFormItem>
        <NFormItem label="物料类型" path="category">
          <NSelect v-model:value="model.category" :options="categoryOptions" placeholder="请选择物料类型" />
        </NFormItem>
        <NFormItem label="状态" path="status">
          <NSelect v-model:value="model.status" :options="statusOptions" placeholder="请选择状态" />
        </NFormItem>
        <NFormItem label="判定规程" path="stdCod">
          <NSelect v-model:value="model.stdCod" :options="stdOptions" placeholder="请选择判定规程" />
        </NFormItem>
        <NFormItem label="创建时间" path="cdate">
          <NDatePicker
            v-model:formatted-value="model.cdate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            clearable
          />
        </NFormItem>
        <NFormItem label="生效人/操作人员" path="oper">
          <NInput v-model:value="model.oper" placeholder="请输入生效人/操作人员" />
        </NFormItem>
        <NFormItem label="备注" path="des">
          <NInput v-model:value="model.des" placeholder="请输入备注" />
        </NFormItem>
 
        <NFormItem label="辅料类型编码" path="typeCode">
          <NInput v-model:value="model.typeCode" placeholder="请输入辅料类型编码" />
        </NFormItem>
        <NFormItem label="辅料类型名称" path="typeName">
          <NInput v-model:value="model.typeName" placeholder="请输入辅料类型名称" />
        </NFormItem>
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
          <NButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>
 
<style scoped></style>