305 lines
14 KiB
Vue
305 lines
14 KiB
Vue
<template>
|
||
<dialog
|
||
ref="dialogElement"
|
||
class="sheet-backdrop batch-disposal-modal"
|
||
aria-labelledby="batch-disposal-title"
|
||
@click.self="close"
|
||
@cancel.prevent="close"
|
||
@keydown.tab="keepDialogTabFocus"
|
||
>
|
||
<form class="sheet-panel space-y-4" @submit.prevent="submit">
|
||
<header>
|
||
<h3 id="batch-disposal-title" ref="dialogTitle" class="section-title" tabindex="-1">{{ dialogHeading }}</h3>
|
||
<p class="mt-2 text-sm text-text-secondary">{{ picking ? '搜索并勾选一项或多项资产,然后预检。' : '预览并选择要申请的资产。提交后进入报废审批。' }}</p>
|
||
</header>
|
||
|
||
<DisposalAssetPicker v-if="props.pickAssets" v-show="picking" @cancel="close" @preview="previewSelection" />
|
||
|
||
<div v-if="importMode" class="field-stack">
|
||
<input id="batch-disposal-file" ref="fileInput" type="file" accept=".xlsx,.xls,.csv" class="hidden" :disabled="busy" @change="handleFile">
|
||
<div class="flex flex-wrap items-center gap-2">
|
||
<button type="button" class="btn-secondary text-sm" :disabled="busy" @click="fileInput?.click()">{{ fileName ? '更换文件' : '选择报废清单' }}</button>
|
||
<span v-if="fileName" class="min-w-0 break-all text-sm text-text-primary">{{ fileName }}</span>
|
||
</div>
|
||
<p class="text-sm text-text-secondary">支持 Excel、CSV,清单需包含“编码”“资产编码”或“设备编码”列。</p>
|
||
</div>
|
||
|
||
<div v-if="loading || readingFile" class="panel text-sm text-text-secondary" role="status">{{ importMode ? '正在读取并检查报废清单…' : '正在检查所选资产…' }}</div>
|
||
<p v-if="errorMessage" class="text-sm text-status-error" role="alert">{{ errorMessage }}</p>
|
||
|
||
<section v-if="preview" class="space-y-4" aria-label="报废预览">
|
||
<div class="flex flex-wrap items-center gap-2" aria-live="polite">
|
||
<span class="badge badge-neutral">共 {{ preview.candidates.length }} 项</span>
|
||
<span class="badge badge-success">可申请 {{ eligibleCandidates.length }} 项</span>
|
||
<span class="badge badge-warning">不可申请 {{ preview.candidates.length - eligibleCandidates.length }} 项</span>
|
||
<span class="badge badge-neutral">已选 {{ selectedCandidates.length }} 项审批</span>
|
||
<span v-if="selectedQuantity !== null && selectedCandidates.length" class="badge badge-neutral">
|
||
含配件共 {{ selectedQuantity }} 件
|
||
</span>
|
||
</div>
|
||
<p v-if="preview.duplicate_codes.length" class="break-words text-sm text-text-secondary">
|
||
重复编码只计一次:{{ preview.duplicate_codes.join('、') }}
|
||
</p>
|
||
<p v-if="hasEligibleGroups" class="text-sm text-text-secondary">含配件的资产组默认不选中。查看配件后,可勾选确认整组申请。</p>
|
||
<div v-if="eligibleCandidates.length" class="flex flex-wrap gap-2">
|
||
<button type="button" class="btn-secondary text-sm" :disabled="busy" @click="selectStandalone">选中可申请单件</button>
|
||
<button type="button" class="btn-secondary text-sm" :disabled="busy || !selectedCandidates.length" @click="selectedIds = []">清空选择</button>
|
||
</div>
|
||
|
||
<ul v-if="preview.candidates.length" class="max-h-72 space-y-2 overflow-y-auto" aria-label="资产预览列表" tabindex="0">
|
||
<li v-for="candidate in preview.candidates" :key="`${candidate.equipment_id ?? ''}:${candidate.equipment_code}`" class="panel space-y-2">
|
||
<div class="flex flex-wrap items-start justify-between gap-2">
|
||
<div class="min-w-0 flex-1 break-words">
|
||
<p class="text-sm font-semibold text-text-primary">{{ candidate.equipment_name || candidate.equipment_code }}</p>
|
||
<p v-if="candidate.equipment_name" class="mt-1 font-mono text-xs text-text-tertiary">
|
||
{{ candidate.equipment_code }}
|
||
<span v-if="candidate.items[0]?.total_quantity !== undefined"> · {{ candidate.items[0].total_quantity }} 件</span>
|
||
</p>
|
||
</div>
|
||
<span class="badge" :class="canApply(candidate) ? 'badge-neutral' : 'badge-warning'">{{ candidate.status || '不可申请' }}</span>
|
||
</div>
|
||
|
||
<p v-if="!canApply(candidate)" class="text-sm text-status-warning">{{ candidate.blocked_reason || '该资产不可申请报废' }}</p>
|
||
<details v-if="candidate.items.length > 1" class="text-sm text-text-secondary">
|
||
<summary class="cursor-pointer text-text-primary">查看 {{ candidate.items.length - 1 }} 项配件</summary>
|
||
<ul class="mt-2 space-y-1 pl-4">
|
||
<li v-for="item in candidate.items.filter((item) => item.id !== candidate.equipment_id)" :key="item.id" class="break-words">
|
||
<span class="font-mono text-xs">{{ item.equipment_code }}</span> {{ item.equipment_name }}
|
||
<span v-if="item.total_quantity !== undefined"> · {{ item.total_quantity }} 件</span>
|
||
</li>
|
||
</ul>
|
||
</details>
|
||
<label v-if="canApply(candidate)" class="flex items-center gap-2 text-sm text-text-primary">
|
||
<input
|
||
v-model="selectedIds"
|
||
type="checkbox"
|
||
:value="candidate.equipment_id"
|
||
:disabled="busy"
|
||
:aria-label="candidate.items.length > 1 ? `确认整组申请 ${candidate.equipment_code} 及其配件` : `申请资产 ${candidate.equipment_code}`"
|
||
class="rounded"
|
||
>
|
||
{{ candidate.items.length > 1 ? '确认整组申请(主资产及配件)' : '申请此资产' }}
|
||
</label>
|
||
</li>
|
||
</ul>
|
||
<p v-else class="text-sm text-text-secondary">没有可预览的资产。</p>
|
||
</section>
|
||
|
||
<fieldset v-if="eligibleCandidates.length" class="field-grid" :disabled="busy">
|
||
<div class="field-stack">
|
||
<label for="batch-disposal-reason" class="field-label">报废原因 *</label>
|
||
<textarea id="batch-disposal-reason" v-model="reason" class="input" rows="2" required placeholder="填写本批申请的共同原因"></textarea>
|
||
</div>
|
||
<div class="field-stack">
|
||
<label for="batch-disposal-method" class="field-label">处置方式 *</label>
|
||
<select id="batch-disposal-method" v-model="method" class="select">
|
||
<option value="报废">报废</option>
|
||
<option value="转让">转让</option>
|
||
<option value="捐赠">捐赠</option>
|
||
</select>
|
||
</div>
|
||
<div class="field-stack sm:col-span-2">
|
||
<label for="batch-disposal-notes" class="field-label">备注</label>
|
||
<textarea id="batch-disposal-notes" v-model="notes" class="input" rows="2" placeholder="其他说明"></textarea>
|
||
</div>
|
||
</fieldset>
|
||
|
||
<footer v-if="!picking" class="flex flex-col-reverse gap-3 sm:flex-row sm:justify-end">
|
||
<button type="button" class="btn-secondary" :disabled="submitting" @click="close">取消</button>
|
||
<button v-if="props.pickAssets" type="button" class="btn-secondary" :disabled="busy" @click="backToSelection">返回选资产</button>
|
||
<button v-if="lastRequest && errorMessage" type="button" class="btn-secondary" :disabled="busy" @click="loadPreview(lastRequest)">重新预览</button>
|
||
<button type="submit" class="btn-danger" :disabled="!canSubmit" :aria-busy="submitting">
|
||
{{ submitting ? '正在提交…' : `提交 ${selectedCandidates.length} 项审批` }}
|
||
</button>
|
||
</footer>
|
||
</form>
|
||
</dialog>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, nextTick, onMounted, ref } from 'vue'
|
||
import { keepDialogTabFocus } from '@/utils/dialogFocus'
|
||
import DisposalAssetPicker from '@/components/DisposalAssetPicker.vue'
|
||
import {
|
||
createBatchDisposal,
|
||
previewBatchDisposal,
|
||
type BatchDisposalCandidate,
|
||
type BatchDisposalPreview,
|
||
type BatchDisposalPreviewParams
|
||
} from '@/api/disposal'
|
||
|
||
const props = defineProps<{ equipmentIds: number[]; pickAssets?: boolean }>()
|
||
const emit = defineEmits<{ close: []; submitted: [result: { count: number; equipmentIds: number[] }] }>()
|
||
|
||
const importMode = !props.pickAssets && props.equipmentIds.length === 0
|
||
const picking = ref(Boolean(props.pickAssets))
|
||
const dialogHeading = computed(() => importMode ? '导入报废清单' : picking.value ? '选择报废资产' : '批量报废')
|
||
const dialogElement = ref<HTMLDialogElement | null>(null)
|
||
const dialogTitle = ref<HTMLElement | null>(null)
|
||
const preview = ref<BatchDisposalPreview | null>(null)
|
||
const lastRequest = ref<BatchDisposalPreviewParams | null>(null)
|
||
const fileInput = ref<HTMLInputElement | null>(null)
|
||
const fileName = ref('')
|
||
const errorMessage = ref('')
|
||
const readingFile = ref(false)
|
||
const loading = ref(false)
|
||
const submitting = ref(false)
|
||
const selectedIds = ref<number[]>([])
|
||
const reason = ref('')
|
||
const method = ref('报废')
|
||
const notes = ref('')
|
||
|
||
const canApply = (candidate: BatchDisposalCandidate): candidate is BatchDisposalCandidate & { equipment_id: number } =>
|
||
candidate.equipment_id !== null && candidate.items.length > 0 && !candidate.blocked_reason
|
||
|
||
const busy = computed(() => readingFile.value || loading.value || submitting.value)
|
||
const eligibleCandidates = computed(() => (preview.value?.candidates || []).filter(canApply))
|
||
const hasEligibleGroups = computed(() => eligibleCandidates.value.some((candidate) => candidate.items.length > 1))
|
||
const selectedCandidates = computed(() => {
|
||
const selected = new Set(selectedIds.value)
|
||
return Array.from(new Map(eligibleCandidates.value
|
||
.filter((candidate) => selected.has(candidate.equipment_id))
|
||
.map((candidate) => [candidate.equipment_id, candidate])).values())
|
||
})
|
||
const selectedQuantity = computed(() => {
|
||
const quantities = selectedCandidates.value.flatMap((candidate) => candidate.items.map((item) => item.total_quantity))
|
||
return quantities.every((quantity) => quantity !== undefined)
|
||
? quantities.reduce((sum, quantity) => sum + quantity, 0)
|
||
: null
|
||
})
|
||
const canSubmit = computed(() => !picking.value && !busy.value && selectedCandidates.value.length > 0 && Boolean(reason.value.trim()))
|
||
|
||
const selectStandalone = () => {
|
||
selectedIds.value = Array.from(new Set([
|
||
...selectedIds.value,
|
||
...eligibleCandidates.value.filter((candidate) => candidate.items.length === 1).map((candidate) => candidate.equipment_id)
|
||
]))
|
||
}
|
||
|
||
const close = () => {
|
||
if (submitting.value) return
|
||
dialogElement.value?.close()
|
||
emit('close')
|
||
}
|
||
|
||
const loadPreview = async (params: BatchDisposalPreviewParams) => {
|
||
if (loading.value || submitting.value) return
|
||
lastRequest.value = params
|
||
preview.value = null
|
||
selectedIds.value = []
|
||
errorMessage.value = ''
|
||
loading.value = true
|
||
try {
|
||
const response = await previewBatchDisposal(params)
|
||
if (!response.data) throw new Error('没有读取到报废预览,请重新预览')
|
||
preview.value = response.data
|
||
selectStandalone()
|
||
} catch (error) {
|
||
errorMessage.value = error instanceof Error ? error.message : '预览失败,请重试'
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const previewSelection = (equipmentIds: number[]) => {
|
||
if (!picking.value || !equipmentIds.length) return
|
||
picking.value = false
|
||
void nextTick(() => dialogTitle.value?.focus({ preventScroll: true }))
|
||
void loadPreview({ equipment_ids: equipmentIds })
|
||
}
|
||
|
||
const backToSelection = () => {
|
||
if (busy.value) return
|
||
preview.value = null
|
||
lastRequest.value = null
|
||
selectedIds.value = []
|
||
errorMessage.value = ''
|
||
picking.value = true
|
||
void nextTick(() => dialogTitle.value?.focus({ preventScroll: true }))
|
||
}
|
||
|
||
const handleFile = async (event: Event) => {
|
||
const input = event.target as HTMLInputElement
|
||
const file = input.files?.[0]
|
||
input.value = ''
|
||
if (!file || busy.value) return
|
||
|
||
fileName.value = file.name
|
||
preview.value = null
|
||
lastRequest.value = null
|
||
selectedIds.value = []
|
||
errorMessage.value = ''
|
||
readingFile.value = true
|
||
try {
|
||
const [{ parseDisposalImport }, source] = await Promise.all([
|
||
import('@/utils/disposalImport'),
|
||
/\.csv$/i.test(file.name) ? file.text() : file.arrayBuffer()
|
||
])
|
||
const codes = parseDisposalImport(source)
|
||
if (!codes.length) throw new Error('清单中没有可读取的资产编码,请检查编码列和数据')
|
||
await loadPreview({ equipment_codes: codes })
|
||
} catch (error) {
|
||
errorMessage.value = error instanceof Error ? error.message : '清单读取失败,请检查文件格式'
|
||
} finally {
|
||
readingFile.value = false
|
||
}
|
||
}
|
||
|
||
const submit = async () => {
|
||
if (!canSubmit.value) return
|
||
const assets = selectedCandidates.value.map((candidate) => {
|
||
const quantities = candidate.items.flatMap((item) => item.total_quantity === undefined ? [] : [{
|
||
equipment_id: item.id,
|
||
total_quantity: item.total_quantity
|
||
}])
|
||
return {
|
||
equipment_id: candidate.equipment_id,
|
||
item_ids: candidate.items.map((item) => item.id),
|
||
confirmed_quantities: quantities.length === candidate.items.length ? quantities : undefined
|
||
}
|
||
})
|
||
submitting.value = true
|
||
errorMessage.value = ''
|
||
try {
|
||
const response = await createBatchDisposal({
|
||
assets,
|
||
disposal_reason: reason.value.trim(),
|
||
disposal_method: method.value,
|
||
notes: notes.value.trim() || undefined
|
||
})
|
||
if (!response.data) throw new Error('未收到审批结果,请核对报废记录')
|
||
dialogElement.value?.close()
|
||
emit('submitted', { count: response.data.count, equipmentIds: assets.flatMap((asset) => asset.item_ids) })
|
||
} catch (error) {
|
||
errorMessage.value = error instanceof Error ? error.message : '提交失败,请重试'
|
||
} finally {
|
||
submitting.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(() => {
|
||
dialogElement.value?.showModal()
|
||
dialogTitle.value?.focus({ preventScroll: true })
|
||
if (!importMode && !picking.value) void loadPreview({ equipment_ids: props.equipmentIds })
|
||
})
|
||
</script>
|
||
|
||
<style scoped>
|
||
.batch-disposal-modal {
|
||
width: 100%;
|
||
height: 100%;
|
||
max-width: none;
|
||
max-height: none;
|
||
margin: 0;
|
||
color: inherit;
|
||
}
|
||
|
||
.batch-disposal-modal:not([open]) {
|
||
display: none;
|
||
}
|
||
|
||
.batch-disposal-modal::backdrop {
|
||
background: transparent;
|
||
}
|
||
</style>
|