419 lines
12 KiB
Vue
419 lines
12 KiB
Vue
<!-- eslint-disable vue/no-v-model-argument -->
|
|
<template>
|
|
<div class="pl-plan-contract-panel" v-loading="loadingDetails">
|
|
<slot
|
|
name="toolbar"
|
|
:open="openSelector"
|
|
:disabled="triggerDisabled"
|
|
:hint="hintText"
|
|
:hint-warn="hintWarn"
|
|
:count="selectedIds.length"
|
|
>
|
|
<div v-if="!readonly" class="pl-plan-contract-panel__toolbar">
|
|
<el-button class="btn-ghost" :disabled="triggerDisabled" @click="openSelector">
|
|
<el-icon><Search /></el-icon>选择合同
|
|
</el-button>
|
|
<span v-if="hintText" class="pl-plan-contract-panel__hint" :class="{ 'is-warn': hintWarn }">
|
|
{{ hintText }}
|
|
</span>
|
|
</div>
|
|
</slot>
|
|
|
|
<div v-if="detailRows.length" class="pl-plan-contract-cards">
|
|
<article v-for="row in detailRows" :key="row.id" class="pl-plan-contract-card">
|
|
<div class="pl-plan-contract-card__head">
|
|
<div class="pl-plan-contract-card__meta">
|
|
<span class="pl-plan-contract-card__no">{{ row.contractNo || row.id || '—' }}</span>
|
|
<DictTag dict-type="crm_contract_type" :value="row.contractType" />
|
|
<span class="pl-plan-contract-card__count">
|
|
{{ row.products?.length || 0 }} 个产品
|
|
</span>
|
|
</div>
|
|
<div class="pl-plan-contract-card__actions">
|
|
<el-button link type="primary" @click="handleViewContract(String(row.id))">
|
|
<el-icon><View /></el-icon>查看
|
|
</el-button>
|
|
<el-button v-if="!readonly" link type="danger" @click="removeContract(String(row.id))">
|
|
<el-icon><Delete /></el-icon>移除
|
|
</el-button>
|
|
</div>
|
|
</div>
|
|
|
|
<div v-if="row.products?.length" class="pl-plan-contract-card__body">
|
|
<div class="pl-plan-contract-card__table-scroll">
|
|
<el-table
|
|
:data="row.products"
|
|
class="list-table pl-plan-contract-card__table"
|
|
size="small"
|
|
stripe
|
|
>
|
|
<el-table-column label="产品名称" min-width="110" align="left" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ product.standardProductName || product.productName || '—' }}
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="别名" min-width="80" align="center" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ product.aliasName || '—' }}
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="品牌" min-width="72" align="center" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ formatBrandConstraintLabel(product.brandMode, product.brandName) }}
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="型号/规格" min-width="90" align="center" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ product.model || product.productModel || '—' }}
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="数量" min-width="64" align="right" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ product.quantity ?? '—' }}
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="单位" min-width="56" align="center">
|
|
<template #default="{ row: product }">
|
|
<DictTag dict-type="crm_product_unit" :value="product.unit" />
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="包装规格" min-width="90" align="center" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ product.packageSpec || '—' }}
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="送货方式" min-width="80" align="center" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ product.deliveryMethod || '—' }}
|
|
</template>
|
|
</el-table-column>
|
|
<el-table-column label="备注" min-width="90" align="left" show-overflow-tooltip>
|
|
<template #default="{ row: product }">
|
|
{{ product.remark || '—' }}
|
|
</template>
|
|
</el-table-column>
|
|
</el-table>
|
|
</div>
|
|
</div>
|
|
<p v-else class="pl-plan-contract-card__empty">暂无产品明细</p>
|
|
</article>
|
|
</div>
|
|
<p v-else-if="readonly || customerId" class="pl-plan-contract-panel__empty-hint">
|
|
{{ readonly ? '暂无关联合同' : '暂未选择合同' }}
|
|
</p>
|
|
|
|
<ContractSelector
|
|
v-if="!readonly"
|
|
v-model:visible="selectorVisible"
|
|
:customer-id="customerId"
|
|
:selectable-statuses="selectableStatuses"
|
|
multiple
|
|
:selected-ids="selectedIds"
|
|
hide-amount
|
|
@confirm-multiple="handleConfirmMultiple"
|
|
/>
|
|
|
|
<ContractDetailDialog
|
|
v-if="contractDetailDialogActive"
|
|
ref="contractDetailRef"
|
|
hide-product-price
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { Delete, Search, View } from '@element-plus/icons-vue'
|
|
import { getContract } from '@/api/crm/contract'
|
|
import type { ContractProductForm, ContractVO } from '@/api/crm/contract/types'
|
|
import { fetchProductDetail } from '@/api/crm/product'
|
|
import ContractSelector from '@/components/business/contract-selector/index.vue'
|
|
import DictTag from '@/components/DictTag/index.vue'
|
|
import { applyAliasDisplayMeta, formatBrandConstraintLabel } from '@/utils/crm-product-brand'
|
|
|
|
const ContractDetailDialog = defineAsyncComponent(
|
|
() => import('@/views/crm/contract/components/ContractDetailDialog.vue')
|
|
)
|
|
|
|
export interface PlanContractLinkForm {
|
|
contractId: string
|
|
contractNo?: string
|
|
}
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
modelValue: PlanContractLinkForm[]
|
|
customerId?: string
|
|
disabled?: boolean
|
|
required?: boolean
|
|
/** 只读:展示合同卡片,标题栏提供「查看」 */
|
|
readonly?: boolean
|
|
selectableStatuses?: string[]
|
|
}>(),
|
|
{
|
|
disabled: false,
|
|
required: false,
|
|
readonly: false,
|
|
selectableStatuses: () => ['approved', 'executing']
|
|
}
|
|
)
|
|
|
|
const emit = defineEmits<{
|
|
'update:modelValue': [value: PlanContractLinkForm[]]
|
|
view: [contractId: string]
|
|
}>()
|
|
|
|
const selectorVisible = ref(false)
|
|
const detailRows = ref<ContractVO[]>([])
|
|
const loadingDetails = ref(false)
|
|
const contractDetailDialogActive = ref(false)
|
|
const contractDetailRef = ref<InstanceType<typeof ContractDetailDialog>>()
|
|
|
|
const selectedIds = computed(() =>
|
|
(props.modelValue || []).map(item => String(item.contractId)).filter(Boolean)
|
|
)
|
|
|
|
const triggerDisabled = computed(() => props.disabled || !props.customerId)
|
|
|
|
const hintWarn = computed(
|
|
() => !!props.customerId && props.required && !selectedIds.value.length
|
|
)
|
|
|
|
const hintText = computed(() => {
|
|
if (!props.customerId) return '请先选择客户'
|
|
if (hintWarn.value) return '请至少选择一份合同'
|
|
if (selectedIds.value.length) return `已选 ${selectedIds.value.length} 份`
|
|
return ''
|
|
})
|
|
|
|
function openSelector() {
|
|
if (props.readonly || triggerDisabled.value) return
|
|
selectorVisible.value = true
|
|
}
|
|
|
|
async function enrichContractProducts(contract: ContractVO): Promise<ContractVO> {
|
|
const products = (contract.products || []) as ContractProductForm[]
|
|
if (!products.length) return contract
|
|
await Promise.all(
|
|
products.map(async line => {
|
|
if (!line.productId) {
|
|
line.standardProductName = line.standardProductName || line.productName
|
|
return
|
|
}
|
|
if (line.standardProductName || line.aliasName) return
|
|
try {
|
|
const res: any = await fetchProductDetail({ id: String(line.productId) })
|
|
const data = res?.data ?? res
|
|
if (data) applyAliasDisplayMeta(line, data)
|
|
} catch {
|
|
line.standardProductName = line.productName
|
|
line.aliasName = undefined
|
|
}
|
|
})
|
|
)
|
|
return { ...contract, products }
|
|
}
|
|
|
|
function emitLinksFromDetails(rows: ContractVO[]) {
|
|
emit(
|
|
'update:modelValue',
|
|
rows.map(row => ({
|
|
contractId: String(row.id),
|
|
contractNo: row.contractNo || undefined
|
|
}))
|
|
)
|
|
}
|
|
|
|
async function loadDetails(ids: string[]) {
|
|
if (!ids.length) {
|
|
detailRows.value = []
|
|
return
|
|
}
|
|
loadingDetails.value = true
|
|
try {
|
|
const rows: ContractVO[] = []
|
|
for (const id of ids) {
|
|
const existing = detailRows.value.find(item => String(item.id) === id)
|
|
if (existing?.products?.length) {
|
|
rows.push(await enrichContractProducts(existing))
|
|
continue
|
|
}
|
|
try {
|
|
const res = await getContract(id)
|
|
const detail = (res.data ?? res) as ContractVO
|
|
rows.push(await enrichContractProducts(detail))
|
|
} catch {
|
|
const link = (props.modelValue || []).find(item => String(item.contractId) === id)
|
|
rows.push({
|
|
id,
|
|
contractNo: link?.contractNo || id,
|
|
products: []
|
|
} as ContractVO)
|
|
}
|
|
}
|
|
detailRows.value = rows
|
|
} finally {
|
|
loadingDetails.value = false
|
|
}
|
|
}
|
|
|
|
async function handleConfirmMultiple(contracts: ContractVO[]) {
|
|
const enriched = await Promise.all((contracts || []).map(item => enrichContractProducts(item)))
|
|
detailRows.value = enriched
|
|
emitLinksFromDetails(detailRows.value)
|
|
}
|
|
|
|
function removeContract(id: string) {
|
|
const next = detailRows.value.filter(item => String(item.id) !== id)
|
|
detailRows.value = next
|
|
emitLinksFromDetails(next)
|
|
}
|
|
|
|
async function handleViewContract(contractId: string) {
|
|
const id = String(contractId || '').trim()
|
|
if (!id) {
|
|
ElMessage.warning('暂无关联合同')
|
|
return
|
|
}
|
|
emit('view', id)
|
|
contractDetailDialogActive.value = true
|
|
await nextTick()
|
|
contractDetailRef.value?.open(id)
|
|
}
|
|
|
|
defineExpose({
|
|
openSelector
|
|
})
|
|
|
|
watch(
|
|
() => props.customerId,
|
|
(id, prev) => {
|
|
if (props.readonly) return
|
|
if (prev && id !== prev) {
|
|
detailRows.value = []
|
|
emit('update:modelValue', [])
|
|
}
|
|
}
|
|
)
|
|
|
|
watch(
|
|
selectedIds,
|
|
ids => {
|
|
const current = detailRows.value.map(item => String(item.id)).join(',')
|
|
const next = ids.join(',')
|
|
if (current !== next) {
|
|
loadDetails(ids)
|
|
}
|
|
},
|
|
{ immediate: true }
|
|
)
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
.pl-plan-contract-panel {
|
|
width: 100%;
|
|
min-width: 0;
|
|
}
|
|
|
|
.pl-plan-contract-panel__toolbar {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.pl-plan-contract-panel__hint {
|
|
font-size: 12px;
|
|
color: #64748b;
|
|
|
|
&.is-warn {
|
|
color: #d97706;
|
|
}
|
|
}
|
|
|
|
.pl-plan-contract-cards {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 10px;
|
|
min-width: 0;
|
|
}
|
|
|
|
.pl-plan-contract-card {
|
|
min-width: 0;
|
|
overflow: hidden;
|
|
border: 1px solid var(--mint-border, #d9e8e2);
|
|
border-radius: 8px;
|
|
background: #fff;
|
|
}
|
|
|
|
.pl-plan-contract-card__head {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 10px;
|
|
padding: 8px 12px;
|
|
background: var(--primary-bg, #ecfdf5);
|
|
border-bottom: 1px solid #a7f3d0;
|
|
}
|
|
|
|
.pl-plan-contract-card__meta {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
align-items: center;
|
|
gap: 8px 10px;
|
|
min-width: 0;
|
|
}
|
|
|
|
.pl-plan-contract-card__actions {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
flex-shrink: 0;
|
|
gap: 4px;
|
|
}
|
|
|
|
.pl-plan-contract-card__no {
|
|
font-size: 13px;
|
|
font-weight: 700;
|
|
color: var(--primary-dark, #059669);
|
|
word-break: break-all;
|
|
}
|
|
|
|
.pl-plan-contract-card__count {
|
|
font-size: 12px;
|
|
color: #64748b;
|
|
}
|
|
|
|
.pl-plan-contract-card__body {
|
|
min-width: 0;
|
|
}
|
|
|
|
.pl-plan-contract-card__table-scroll {
|
|
width: 100%;
|
|
overflow-x: auto;
|
|
-webkit-overflow-scrolling: touch;
|
|
}
|
|
|
|
.pl-plan-contract-card__table {
|
|
width: max-content;
|
|
min-width: 100%;
|
|
|
|
:deep(.el-table__header th) {
|
|
padding: 6px 0;
|
|
font-size: 12px;
|
|
background: #f8fafc;
|
|
}
|
|
|
|
:deep(.el-table__body td) {
|
|
padding: 6px 0;
|
|
font-size: 12px;
|
|
}
|
|
}
|
|
|
|
.pl-plan-contract-card__empty,
|
|
.pl-plan-contract-panel__empty-hint {
|
|
margin: 0;
|
|
padding: 10px 12px;
|
|
font-size: 12px;
|
|
color: #94a3b8;
|
|
}
|
|
</style>
|