171 lines
4.6 KiB
Vue
171 lines
4.6 KiB
Vue
<template>
|
||
<el-dialog
|
||
v-model="visible"
|
||
title="包材快速搜索"
|
||
width="860px"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="!loading"
|
||
@closed="emit('closed')"
|
||
>
|
||
<!-- <el-alert
|
||
type="info"
|
||
:closable="false"
|
||
show-icon
|
||
class="search-alert"
|
||
title="setting.packageMaterial.search"
|
||
description="按包材编码/名称模糊匹配,最多返回 50 条启用中的包材;关键字为空时返回前 50 条。"
|
||
/> -->
|
||
|
||
<el-form :inline="true" size="small" class="search-form" @submit.prevent="handleSearch">
|
||
<el-form-item label="关键字">
|
||
<el-input
|
||
v-model="keyword"
|
||
clearable
|
||
placeholder="编码 / 名称"
|
||
style="width: 260px"
|
||
@keyup.enter="handleSearch"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item>
|
||
<el-button type="primary" :loading="loading" @click="handleSearch">搜索</el-button>
|
||
<el-button :disabled="loading" @click="handleReset">重置</el-button>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<div class="result-meta">
|
||
共 {{ list.length }} 条(最多 50 条)
|
||
</div>
|
||
|
||
<el-table
|
||
:data="list"
|
||
border
|
||
size="small"
|
||
max-height="420"
|
||
v-loading="loading"
|
||
row-key="id"
|
||
empty-text="暂无包材"
|
||
highlight-current-row
|
||
@row-dblclick="handleSelect"
|
||
>
|
||
<el-table-column prop="id" label="ID" width="72" />
|
||
<el-table-column prop="material_code" label="包材编码" min-width="130" show-overflow-tooltip />
|
||
<el-table-column prop="material_name" label="包材名称" min-width="150" show-overflow-tooltip />
|
||
<el-table-column prop="material_type" label="类型" min-width="100" show-overflow-tooltip />
|
||
<el-table-column prop="spec" label="规格" min-width="120" show-overflow-tooltip />
|
||
<el-table-column label="状态" width="88" align="center">
|
||
<template #default="{ row }">
|
||
<el-tag :type="formatPackageMaterialStatus(row.status).type" size="small">
|
||
{{ formatPackageMaterialStatus(row.status).text }}
|
||
</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column v-if="selectable" label="操作" width="80" fixed="right" align="center">
|
||
<template #default="{ row }">
|
||
<el-button link type="primary" size="small" @click="handleSelect(row)">选择</el-button>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
|
||
<template #footer>
|
||
<el-button size="small" @click="visible = false">关闭</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, watch } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import { packageMaterialSearch, type PackageMaterialSearchItem } from '@/api/setting.packagematerial/packagematerial'
|
||
import {
|
||
formatPackageMaterialStatus,
|
||
isPackageMaterialApiSuccess,
|
||
parsePackageMaterialSearchResponse,
|
||
type PackageMaterialRecord
|
||
} from '../package-material-config'
|
||
|
||
const visible = defineModel<boolean>({ default: false })
|
||
|
||
const props = withDefaults(
|
||
defineProps<{
|
||
/** 是否显示「选择」操作列并 emit select */
|
||
selectable?: boolean
|
||
/** 打开时预填关键字 */
|
||
initialKeyword?: string
|
||
}>(),
|
||
{
|
||
selectable: false,
|
||
initialKeyword: ''
|
||
}
|
||
)
|
||
|
||
const emit = defineEmits<{
|
||
(e: 'closed'): void
|
||
(e: 'select', row: PackageMaterialSearchItem): void
|
||
}>()
|
||
|
||
const loading = ref(false)
|
||
const keyword = ref('')
|
||
const list = ref<PackageMaterialRecord[]>([])
|
||
|
||
watch(
|
||
() => visible.value,
|
||
(open) => {
|
||
if (!open) return
|
||
keyword.value = String(props.initialKeyword || '').trim()
|
||
void loadList()
|
||
}
|
||
)
|
||
|
||
async function loadList() {
|
||
loading.value = true
|
||
try {
|
||
const payload: { keyword?: string } = {}
|
||
const text = keyword.value.trim()
|
||
if (text) payload.keyword = text
|
||
const res = await packageMaterialSearch(payload)
|
||
if (!isPackageMaterialApiSuccess(res)) {
|
||
throw new Error((res as any)?.message || '包材搜索失败')
|
||
}
|
||
list.value = parsePackageMaterialSearchResponse(res)
|
||
} catch (error: any) {
|
||
list.value = []
|
||
ElMessage.error(error?.message || '包材搜索失败')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function handleSearch() {
|
||
void loadList()
|
||
}
|
||
|
||
function handleReset() {
|
||
keyword.value = ''
|
||
void loadList()
|
||
}
|
||
|
||
function handleSelect(row: PackageMaterialRecord) {
|
||
emit('select', row)
|
||
if (props.selectable) {
|
||
visible.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.search-alert {
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.search-form {
|
||
margin-bottom: 8px;
|
||
}
|
||
|
||
.result-meta {
|
||
margin-bottom: 8px;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
}
|
||
</style>
|