Files
shanxia2.0/web-admin/src/components/business/pl/PlVehicleSelect.vue
T
2026-09-14 11:57:58 +08:00

126 lines
3.2 KiB
Vue

<template>
<el-select
v-model="model"
:placeholder="placeholder"
filterable
remote
:remote-method="loadOptions"
:loading="loading"
clearable
style="width: 100%"
@focus="handleFocus"
@change="emit('change', $event)"
>
<el-option
v-for="item in displayOptions"
:key="item.id"
:label="item.licensePlate"
:value="item.licensePlate"
>
<div class="pl-vehicle-option">
<span>{{ item.licensePlate }}</span>
<span v-if="vehicleTypeLabel(item)" class="pl-vehicle-option__meta">{{
vehicleTypeLabel(item)
}}</span>
</div>
</el-option>
</el-select>
</template>
<script setup lang="ts">
import { vehicleApi, type Vehicle } from '@/api/pl'
import { pickBxList } from '@/utils/http/bx'
defineOptions({ name: 'PlVehicleSelect' })
const model = defineModel<string>({ default: '' })
const emit = defineEmits<{
change: [value: string]
}>()
const props = withDefaults(
defineProps<{
placeholder?: string
/** 透传车辆列表筛选,如 available */
vehicleStatus?: string
/** 透传车辆类型:self-自有 / carrier-三方;不传则两者都查 */
vehicleType?: string
}>(),
{
placeholder: '请选择车牌号(三方/自有车辆)'
}
)
const loading = ref(false)
const options = ref<Vehicle[]>([])
const vehicleTypeLabel = (item: Vehicle): string => {
if (item.id?.startsWith('legacy-')) return '历史'
if (item.vehicleType === 'self') return '自有'
if (item.vehicleType === 'carrier') return '三方'
return ''
}
/** 默认三方+自有;可按 vehicleType 收窄。禁止手填新建 */
const loadOptions = async (keyword: string) => {
loading.value = true
try {
const res = await vehicleApi.list({
...(props.vehicleStatus ? { vehicleStatus: props.vehicleStatus } : {}),
...(props.vehicleType ? { vehicleType: props.vehicleType } : {}),
...(keyword.trim() ? { q: keyword.trim() } : {}),
page: 1,
pageSize: 100
})
options.value = pickBxList<Vehicle>(res)
} catch (error) {
console.error('加载运输车辆列表失败', error)
options.value = []
} finally {
loading.value = false
}
}
const handleFocus = () => {
if (options.value.length === 0) {
loadOptions('')
}
}
/** 编辑态回显:历史出入库单中的车牌若不在车辆库,仍展示只读选项 */
const displayOptions = computed(() => {
const current = model.value?.trim()
if (!current) return options.value
if (options.value.some((item) => item.licensePlate === current)) {
return options.value
}
return [
{
id: `legacy-${current}`,
licensePlate: current,
vehicleType: '',
vehicleStatus: '',
createTime: ''
},
...options.value
]
})
</script>
<style scoped lang="scss">
.pl-vehicle-option {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
min-width: 0;
}
.pl-vehicle-option__meta {
flex-shrink: 0;
font-size: 12px;
color: var(--text-muted);
}
</style>