WEB/src/views/storage/receiving/component/DetailsPanel.vue
2026-04-15 21:01:12 +08:00

397 lines
12 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="receiving-details-container">
<el-form :model="form" label-width="120px" class="details-form">
<el-row :gutter="30">
<!-- 第一列:收货明细 -->
<el-col :span="12">
<el-form-item
v-for="field in detailpanel"
:key="field.prop"
:label="field.label"
:prop="field.prop"
>
<!-- 特殊处理:货主行,根据绑定状态显示不同内容 -->
<template v-if="field.prop === 'store_name'">
<div style="display:flex; gap:8px; align-items:center; width: 100%;">
<!-- 已绑定货主:显示文本,禁用按钮 -->
<template v-if="storeBound">
<div style="flex: 1; padding: 0px 12px; background-color: #f5f7fa; border-radius: 4px; color: #606266; border: 1px solid #dcdfe6; height: 32px; line-height: 32px;">
{{ form.store_name || '未设置' }}
</div>
</template>
<!-- 未绑定货主:显示下拉选择,按钮可用 -->
<template v-else>
<el-select
v-model="form.store_id"
:placeholder="'请选择' + field.label"
style="flex: 1;"
filterable
:loading="storeListLoading"
@focus="loadStoreList"
>
<el-option
v-for="item in (localAvailableFields.find((f: { value: string }) => f.value === 'store_id')?.options || [])"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</template>
<el-button
size="small"
type="primary"
@click="handleBindStore"
:disabled="storeBound"
>
货主绑定
</el-button>
</div>
</template>
<template v-else-if="field.type === 'select'">
<el-select
v-model="(form as any)[field.prop]"
:placeholder="'请选择' + field.label"
style="width: 100%"
>
<el-option
v-for="item in localAvailableFields.find((f: { value: string }) => f.value === field.prop)?.options || []"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</template>
<template v-else>
<el-input
v-model="(form as any)[field.prop]"
:disabled="field.disabled"
:placeholder="'请输入' + field.label"
/>
</template>
</el-form-item>
</el-col>
<!-- 第二列:收货图片 -->
<el-col :span="12">
<div class="receiving-images">
<div v-if="imagesArr.length > 0" class="images-grid">
<div v-for="(src, idx) in imagesArr" :key="idx" class="img-cell" @click="handlePreview(src)">
<el-image :src="src" fit="cover" style="width:100%; height:100%" />
</div>
</div>
<div v-else class="image-placeholder">
<div class="placeholder-content">
<el-icon :size="48" color="#909399"><Picture /></el-icon>
<p>暂无收货图片</p>
</div>
</div>
</div>
</el-col>
</el-row>
</el-form>
<!-- 底部操作按钮 -->
<div class="bottom">
<el-button-group class="toolbar-btn-group">
<el-button type="primary" icon="Check" disabled :loading="saveLoading">
保存
</el-button>
<el-button type="danger" icon="Refresh" @click="handleReset">
重置
</el-button>
</el-button-group>
</div>
<!-- 图片预览对话框 -->
<el-dialog v-model="previewVisible" title="图片预览" width="60%" align-center append-to-body>
<img :src="dialogImageUrl" alt="预览" style="width: 100%; max-height: 70vh; object-fit: contain;" />
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, watch, computed, reactive } from 'vue'
import { availableFields, detailpanel } from '../receiving-config'
import { ElMessage, ElMessageBox } from 'element-plus'
import * as receivingApi from '@/api/storage_receiving/receiving'
import * as indexApi from '@/api/index/index'
import { Picture, Check, Refresh } from '@element-plus/icons-vue'
const props = defineProps<{
id: string | number,
row?: any
}>()
const form = ref({
receiving_number: '',
store_name: '',
store_id: '',
detail_status: '',
creator_name: '',
delivery_method: '',
status: '',
loading_method: '',
total_items: 0,
total_quantity: 0,
total_boxes: 0,
total_pallets: 0,
remark: ''
})
const saveLoading = ref(false)
const handleSave = async () => {
try {
saveLoading.value = true
// 这里可以调用接口保存数据
ElMessage.success('保存成功')
} catch (error) {
ElMessage.error('保存失败')
} finally {
saveLoading.value = false
}
}
const handleReset = () => {
if (props.row) {
form.value = { ...props.row }
// normalize store id/name
form.value.store_id = props.row.store?.id || props.row.store_id || ''
form.value.store_name = props.row.store?.store_remask || props.row.store_name || form.value.store_name
}
ElMessage.info('表单已重置')
}
// 图片预览
const dialogImageUrl = ref('')
const previewVisible = ref(false)
const imagesArr = computed(() => {
const r = props.row || {}
const imgs = r.images || []
if (!Array.isArray(imgs)) return []
return imgs.map((it: any) => {
if (!it) return ''
if (typeof it === 'string') return it
return it.file_url || it.url || it.path || ''
}).filter((s: string) => !!s)
})
// 创建availableFields的本地响应式副本
const localAvailableFields = reactive(JSON.parse(JSON.stringify(availableFields)))
// 货主列表加载状态
const storeListLoading = ref(false)
const storeListLoaded = ref(false)
// 加载货主列表
const loadStoreList = async () => {
// 如果已加载过,直接返回
if (storeListLoaded.value) return
storeListLoading.value = true
try {
const res: any = await indexApi.loadStores({ type: 'receiving' })
if (res && res.code === 200 && res.stores) {
// 更新本地availableFields中store_id的options
const storeIdField = localAvailableFields.find((f: any) => f.value === 'store_id')
if (storeIdField) {
storeIdField.options = res.stores.map((store: any) => ({
value: store.value,
label: store.label
}))
}
storeListLoaded.value = true
} else {
ElMessage.warning('加载货主列表失败')
}
} catch (e) {
console.error('加载货主列表出错:', e)
ElMessage.error('加载货主列表失败')
} finally {
storeListLoading.value = false
}
}
// 绑定状态:绑定后不可更改(根据是否已有货主决定)
const storeBound = ref(false)
// 触发绑定流程
const handleBindStore = async () => {
const storeId = form.value.store_id
const receivingNumber = form.value.receiving_number
const receivingId = props.row?.id
if (!storeId) {
ElMessage.warning('请先在下拉框中选择货主')
return
}
if (!receivingId || !receivingNumber) {
ElMessage.warning('缺少收货单必要信息,无法绑定')
return
}
const status = props.row?.status
const hasStore = !!(props.row && (props.row.store || props.row.store_id))
// 拒绝条件1:取消状态始终拒绝
if (status === 'cancelled') {
ElMessage.error('取消状态的收货单无法绑定货主')
return
}
// 拒绝条件2:收货中/已完成状态且已有货主则拒绝
if ((status === 'receiving' || status === 'completed') && hasStore) {
ElMessage.error('当前状态下已有货主,无法重新绑定')
return
}
// 获取货主名称用于确认信息
const storeLabel = getStoreLabel(storeId)
// 显示确认对话框,提示绑定后不可更改
try {
await ElMessageBox.confirm(
`将货主绑定为:<strong>${storeLabel}</strong>?\n\n<span style="color:#f56c6c;">绑定后不可更改,请确认无误。</span>`,
'确认绑定货主',
{
confirmButtonText: '确定绑定',
cancelButtonText: '取消',
type: 'warning',
dangerouslyUseHTMLString: true
}
)
// 调用后端接口进行绑定
const payload = { id: receivingId, receiving_number: receivingNumber, store_id: storeId }
const res: any = await receivingApi.bindReceivingStore(payload)
if (res && (res.code === 200 || res.flag === 'success')) {
ElMessage.success(res.message || '货主绑定成功')
storeBound.value = true
// 更新本地表单数据
form.value.store_name = storeLabel
// 更新props.row以保持数据同步
if (props.row) {
props.row.store = props.row.store || {}
props.row.store.id = storeId
props.row.store.store_remask = storeLabel
props.row.store_id = storeId
}
} else {
ElMessage.error((res && res.message) || '货主绑定失败,请稍后重试')
}
} catch (e: any) {
// 用户取消或其他错误
if (e === 'cancel') return
console.error('bind store error', e)
ElMessage.error('绑定过程中出错,请稍后重试')
}
}
const getStoreLabel = (id: any) => {
const opts = (localAvailableFields.find((f: { value: string }) => f.value === 'store_id') || {}).options || []
const found = opts.find((o: any) => o.value === id)
return found ? found.label : String(id)
}
const handlePreview = (src: string) => {
dialogImageUrl.value = src
previewVisible.value = true
}
// 监听 row 变化,更新表单数据和绑定状态
watch(() => props.row, (newRow) => {
if (newRow) {
form.value = { ...newRow }
form.value.store_id = newRow.store?.id || newRow.store_id || ''
form.value.store_name = newRow.store?.store_remask || newRow.store_name || form.value.store_name
// 根据是否已有货主来设置 storeBound 状态
// 如果 row 中 store 或 store_id 存在,则说明已绑定货主,不可更改
const hasStore = !!(newRow.store || newRow.store_id)
storeBound.value = hasStore
}
}, { immediate: true })
onMounted(() => {
if (props.row) {
form.value = { ...props.row }
}
})
</script>
<style scoped lang="scss">
.receiving-details-container {
height: 100%;
display: flex;
flex-direction: column;
box-sizing: border-box;
background-color: #fff;
}
.details-form {
flex: 1;
overflow-y: auto;
padding: 20px;
width: 100%;
box-sizing: border-box;
/* 隐藏滚动条 */
&::-webkit-scrollbar {
display: none;
}
-ms-overflow-style: none;
scrollbar-width: none;
}
.image-placeholder {
width: 100%;
height: 400px;
background-color: #f5f7fa;
border: 1px dashed #dcdfe6;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
.placeholder-content {
text-align: center;
color: #909399;
p {
margin-top: 10px;
font-size: 14px;
}
}
}
.receiving-images {
.images-grid {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.img-cell {
width: 100px;
height: 100px;
border: 1px solid #eef2f6;
border-radius: 4px;
overflow: hidden;
cursor: pointer;
background-color: #fff;
}
}
.bottom {
flex-shrink: 0;
text-align: right;
padding: 0px 20px;
border-top: 1px solid #ebeef5;
background-color: #fff;
z-index: 10;
}
</style>