<script setup lang="ts">
|
import { computed, ref, watch } from 'vue';
|
import { jsonClone } from '@sa/utils';
|
import { fetchCreateCheckitem, fetchGetRid, fetchUpdateCheckitem } from '@/service/api/qm/checkitem';
|
import { fetchGetInstrumentList } from '@/service/api/md/instrument';
|
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
|
import { $t } from '@/locales';
|
|
defineOptions({
|
name: 'CheckitemOperateDrawer'
|
});
|
|
interface Props {
|
/** the type of operation */
|
operateType: NaiveUI.TableOperateType;
|
/** the edit row data */
|
rowData?: Api.Qm.Checkitem | null;
|
/** preset stdCode for add */
|
stdCode?: string | 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 enableOptions = [
|
{ label: '启用', value: 1 },
|
{ label: '停用', value: 0 }
|
];
|
|
const categoryOptions = [
|
{ label: '成品', value: 0 },
|
{ label: '辅料', value: 1 }
|
];
|
|
const checkLevelOptions = [
|
{ label: 'A', value: 'A' },
|
{ label: 'B', value: 'B' },
|
{ label: 'C', value: 'C' },
|
{ label: 'D', value: 'D' }
|
];
|
|
const ismixOptions = [
|
{ label: '是', value: 1 },
|
{ label: '否', value: 0 }
|
];
|
|
const delOptions = [
|
{ label: '正常', value: 0 },
|
{ label: '删除', value: 1 }
|
];
|
|
type Model = Api.Qm.CheckitemOperateParams;
|
|
const model = ref<Model>(createDefaultModel());
|
const ridOptions = ref<{ label: string; value: string | number }[]>([]);
|
const instrumentOptions = ref<{ label: string; value: string | number }[]>([]);
|
|
async function fetchRidOptions(searchText?: string) {
|
const params: any = {};
|
if (searchText) {
|
params.itemName = searchText;
|
}
|
if (props.stdCode) {
|
params.stdCode = props.stdCode;
|
}
|
|
const { data } = await fetchGetRid(params);
|
if (data) {
|
ridOptions.value = data;
|
} else {
|
ridOptions.value = [];
|
}
|
}
|
|
function handleRidSelectSearch(query: string) {
|
fetchRidOptions(query);
|
}
|
|
async function fetchInstrumentOptions(searchText?: string) {
|
const params: any = {};
|
if (searchText) {
|
params.instrumentName = searchText;
|
}
|
const { data } = await fetchGetInstrumentList(params);
|
if (data && data.rows) {
|
instrumentOptions.value = data.rows.map(item => ({
|
label: item.instrumentName,
|
value: item.instrumentCode
|
}));
|
} else {
|
instrumentOptions.value = [];
|
}
|
}
|
|
function handleInstrumentSelectSearch(query: string) {
|
fetchInstrumentOptions(query);
|
}
|
|
function createDefaultModel(): Model {
|
return {
|
id: '',
|
itemCode: '',
|
itemName: '',
|
unit: '',
|
enable: 1,
|
del: 0,
|
itemDes: '',
|
stdCode: '',
|
instrumentDes: '',
|
location: '',
|
checkLevel: '',
|
ismix: null,
|
rid: null,
|
category: null,
|
instrumentCode: '',
|
score: null
|
};
|
}
|
|
type RuleKey = Extract<keyof Model, 'id'>;
|
|
const rules: Record<RuleKey, App.Global.FormRule> = {
|
id: createRequiredRule('编码不能为空')
|
};
|
|
async function handleUpdateModelWhenEdit() {
|
model.value = createDefaultModel();
|
ridOptions.value = []; // Clear options initially
|
|
if (props.operateType === 'edit' && props.rowData) {
|
Object.assign(model.value, jsonClone(props.rowData));
|
}
|
|
// Fetch options for rid
|
await fetchRidOptions();
|
// Fetch options for instrument
|
await fetchInstrumentOptions();
|
|
if (props.operateType === 'add' && props.stdCode) {
|
model.value.stdCode = props.stdCode;
|
}
|
}
|
|
function closeDrawer() {
|
visible.value = false;
|
}
|
|
async function handleSubmit() {
|
await validate();
|
|
const {
|
id,
|
itemCode,
|
itemName,
|
unit,
|
enable,
|
del,
|
itemDes,
|
stdCode,
|
instrumentDes,
|
location,
|
checkLevel,
|
ismix,
|
rid,
|
category,
|
instrumentCode,
|
score
|
} = model.value;
|
|
// request
|
if (props.operateType === 'add') {
|
const payload = {
|
itemCode,
|
itemName,
|
unit,
|
enable,
|
del,
|
itemDes,
|
stdCode,
|
instrumentDes,
|
location,
|
checkLevel,
|
ismix,
|
rid,
|
category,
|
instrumentCode,
|
score
|
};
|
const { error } = await fetchCreateCheckitem(payload);
|
if (error) return;
|
}
|
|
if (props.operateType === 'edit') {
|
const payload = {
|
id,
|
itemCode,
|
itemName,
|
unit,
|
enable,
|
del,
|
itemDes,
|
stdCode,
|
instrumentDes,
|
location,
|
checkLevel,
|
ismix,
|
rid,
|
category,
|
instrumentCode,
|
score
|
};
|
const { error } = await fetchUpdateCheckitem(payload);
|
if (error) return;
|
}
|
|
window.$message?.success($t('common.updateSuccess'));
|
closeDrawer();
|
emit('submitted');
|
}
|
|
watch(visible, () => {
|
if (visible.value) {
|
handleUpdateModelWhenEdit();
|
restoreValidation();
|
}
|
});
|
</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="itemCode">
|
<NInput v-model:value="model.itemCode" placeholder="请输入检验项目代码" />
|
</NFormItem>
|
<NFormItem label="检验项目名称" path="itemName">
|
<NInput v-model:value="model.itemName" placeholder="请输入检验项目名称" />
|
</NFormItem>
|
<NFormItem label="单位" path="unit">
|
<NInput v-model:value="model.unit" placeholder="请输入单位" />
|
</NFormItem>
|
<NFormItem label="启用" path="enable">
|
<NSelect v-model:value="model.enable" :options="enableOptions" placeholder="请选择启用" clearable />
|
</NFormItem>
|
<NFormItem label="删除" path="del">
|
<NSelect v-model:value="model.del" :options="delOptions" placeholder="请选择删除" clearable />
|
</NFormItem>
|
<NFormItem label="检验项描述" path="itemDes">
|
<NInput v-model:value="model.itemDes" placeholder="请输入检验项描述" />
|
</NFormItem>
|
<NFormItem label="检测仪器" path="instrumentCode">
|
<NSelect
|
v-model:value="model.instrumentCode"
|
:options="instrumentOptions"
|
placeholder="请选择检测仪器"
|
clearable
|
filterable
|
@search="handleInstrumentSelectSearch"
|
@focus="() => fetchInstrumentOptions()"
|
/>
|
</NFormItem>
|
<NFormItem label="仪器描述" path="instrumentDes">
|
<NInput v-model:value="model.instrumentDes" placeholder="请输入仪器描述" />
|
</NFormItem>
|
<NFormItem label="缺陷位置" path="location">
|
<NInput v-model:value="model.location" placeholder="请输入缺陷位置-外观用" />
|
</NFormItem>
|
<NFormItem label="级别" path="checkLevel">
|
<NSelect v-model:value="model.checkLevel" :options="checkLevelOptions" placeholder="请选择级别" clearable />
|
</NFormItem>
|
<NFormItem label="是否合成项" path="ismix">
|
<NSelect v-model:value="model.ismix" :options="ismixOptions" placeholder="请选择是否合成项" clearable />
|
</NFormItem>
|
<NFormItem label="上级项" path="rid">
|
<NSelect
|
v-model:value="model.rid"
|
:options="ridOptions"
|
placeholder="请选择上级项"
|
clearable
|
filterable
|
@search="handleRidSelectSearch"
|
@focus="() => fetchRidOptions()"
|
/>
|
</NFormItem>
|
<NFormItem label="类别" path="category">
|
<NSelect v-model:value="model.category" :options="categoryOptions" placeholder="请选择类别" clearable />
|
</NFormItem>
|
<NFormItem label="分值" path="score">
|
<NInputNumber v-model:value="model.score" placeholder="请输入分值" :precision="2" />
|
</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>
|