Files
shanxia2.0/web-admin/src/views/pl/quota-statistics/ContractCertLinkDialog.vue
T
wuzilongandCursor 522b4be705 feat(pl): 装送货段增加同市标记,并同步额度证件与计划表单。
同市易制毒段只校验购买证;同步 Web/H5 表单、额度统计展示与相关回归测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-12 16:34:58 +08:00

221 lines
7.0 KiB
Vue

<template>
<el-dialog
v-model="visible"
:title="mode === 'create' ? '新增关联证件' : '编辑适用产品'"
width="1040px"
align-center
class="dialog-form-layout quota-contract-cert-dialog"
:close-on-click-modal="false"
@closed="reset"
>
<el-form v-if="mode === 'create'" label-position="top" class="form-grid-2">
<el-form-item label="证件类型">
<el-select v-model="certType" :disabled="saving" @change="changeType">
<el-option label="购买证" value="purchase" />
<el-option label="运输证" value="transport" />
</el-select>
</el-form-item>
<el-form-item label="证件">
<el-select
v-model="certId"
filterable
remote
clearable
:remote-method="searchCerts"
:loading="certLoading"
:disabled="saving"
placeholder="证件编号或品名"
@focus="focusCerts"
@change="pickCert"
>
<el-option
v-for="item in certOptions"
:key="item.id"
:label="certOptionLabel(item)"
:value="item.id"
:disabled="
item.status === 'void' ||
item.status === 'closed' ||
takenIds.has(`${certType}:${item.id}`)
"
/>
</el-select>
</el-form-item>
</el-form>
<p v-else class="quota-contract-cert-dialog__current">
<el-tag size="small" :type="certKindTagType(certType)">{{ certKindLabel(certType) }}</el-tag>
<span v-if="certNo">{{ certNo }}</span>
</p>
<CertContractBinder
v-if="ready && certId && contractId"
:key="binderKey"
mode="bound"
:contract-id="contractId"
:contract-no="contractNo"
:contract-name="contractName"
:saved-products="savedProducts"
editable
:saving="saving"
:save-text="mode === 'create' ? '保存关联' : '保存产品'"
@save="save"
/>
</el-dialog>
</template>
<script setup lang="ts">
import {
quotaCertApi,
type CertBinding,
type CertType,
type QuotaCert,
type ScopeProduct
} from '@/api/pl/quota-cert'
import CertContractBinder from '../quota-cert/CertContractBinder.vue'
import { certKindLabel, certKindTagType } from '../quota-cert/cert-list-face'
import { bindingPayload, replaceCertProducts } from '../quota-cert/model'
const emit = defineEmits<{ saved: [] }>()
const visible = ref(false)
const ready = ref(false)
const saving = ref(false)
const certLoading = ref(false)
const mode = ref<'create' | 'edit'>('create')
const contractId = ref('')
const contractNo = ref('')
const contractName = ref('')
const certType = ref<CertType>('purchase')
const certId = ref('')
const certNo = ref('')
const savedProducts = ref<ScopeProduct[]>([])
const certOptions = ref<QuotaCert[]>([])
const takenIds = ref(new Set<string>())
let epoch = 0
const binderKey = computed(
() =>
`${mode.value}|${certType.value}|${certId.value}|${savedProducts.value.map((row) => row.contractProductId).join(',')}`
)
function certOptionLabel(item: QuotaCert) {
const name = [item.certNo, item.productNameOnCert].filter(Boolean).join(' · ')
if (item.status === 'void') return `${name}(已作废)`
if (item.status === 'closed') return `${name}(已核销)`
if (takenIds.value.has(`${certType.value}:${item.id}`)) return `${name}(已关联)`
return name
}
function reset() {
epoch++
ready.value = false
saving.value = false
certLoading.value = false
certId.value = ''
certNo.value = ''
savedProducts.value = []
certOptions.value = []
takenIds.value = new Set()
}
async function open(payload: {
contractId: string
contractNo?: string
contractName?: string
mode: 'create' | 'edit'
certType?: CertType
certId?: string
certNo?: string
}) {
reset()
const current = ++epoch
mode.value = payload.mode
contractId.value = payload.contractId
contractNo.value = payload.contractNo || ''
contractName.value = payload.contractName || ''
certType.value = payload.certType || 'purchase'
certId.value = payload.certId || ''
certNo.value = payload.certNo || ''
visible.value = true
try {
const bindings = await quotaCertApi.bindings(payload.contractId)
if (current !== epoch) return
takenIds.value = new Set(bindings.map((row) => `${row.certType}:${row.certId}`))
if (payload.mode === 'edit' && payload.certType && payload.certId) {
const currentBinding = bindings.find(
(row) => row.certType === payload.certType && row.certId === payload.certId
)
savedProducts.value = (currentBinding?.products || []).map((product) => ({ ...product }))
}
if (payload.mode === 'create') await searchCerts('')
if (current === epoch) ready.value = true
} catch {
if (current === epoch) visible.value = false
}
}
function changeType() {
certId.value = ''
certNo.value = ''
savedProducts.value = []
void searchCerts('')
}
function focusCerts() {
if (!certOptions.value.length) void searchCerts('')
}
async function searchCerts(keyword = '') {
const current = epoch
const kind = certType.value
certLoading.value = true
try {
const rows = await quotaCertApi.list(kind, { keyword })
if (current !== epoch || kind !== certType.value) return
certOptions.value = rows
} finally {
if (current === epoch) certLoading.value = false
}
}
function pickCert(id?: string) {
const cert = certOptions.value.find((item) => item.id === id)
certNo.value = cert?.certNo || ''
savedProducts.value = []
}
async function save(payload: { products: ScopeProduct[] }) {
if (saving.value || !contractId.value || !certId.value) return
if (mode.value === 'create' && !payload.products.length) {
ElMessage.warning('请选择适用产品')
return
}
if (mode.value === 'create' && takenIds.value.has(`${certType.value}:${certId.value}`)) {
ElMessage.warning('该证件已关联,请在对应页签中编辑')
return
}
const current = epoch
saving.value = true
try {
const existing = await quotaCertApi.bindings(contractId.value)
if (current !== epoch) return
const next: CertBinding[] = replaceCertProducts(
existing,
certType.value,
certId.value,
payload.products
)
await quotaCertApi.saveBindings(contractId.value, bindingPayload(next))
if (current !== epoch) return
ElMessage.success(mode.value === 'create' ? '证件已关联' : '适用产品已保存')
visible.value = false
emit('saved')
} finally {
if (current === epoch) saving.value = false
}
}
defineExpose({ open })
</script>
<style scoped>
.quota-contract-cert-dialog__current {
display: flex;
align-items: center;
justify-content: flex-start;
gap: 8px;
margin: 0 0 12px;
color: #1e293b;
font-weight: 600;
}
</style>