269 lines
10 KiB
Vue
269 lines
10 KiB
Vue
<!--
|
||
features/chat/components/shipping-review/ReviewFieldConflictPanel.vue
|
||
用途:复核工作台建单前字段冲突集中裁决区(采用候选值 / 手工值)。
|
||
依赖:vue-i18n、shipping.api.resolveWorkspaceShippingOrderReviewFieldConflict、useMessage。
|
||
逻辑:展示 Case.field_conflicts;选项标注 evidence_role(主 PDF / 辅助 / 正文);
|
||
裁决成功后 emit refreshed;可 emit focus-conflict 定位证据。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { computed, ref } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { BusinessError, resolveLocalizedErrorMessage } from '@/shared/api/client';
|
||
import { useMessage } from '@/shared/composables/useMessage';
|
||
import { resolveWorkspaceShippingOrderReviewFieldConflict } from '@/features/chat/api/shipping.api';
|
||
import { showReviewResumeOutcome } from '@/features/chat/shipping-review/showReviewResumeOutcome';
|
||
import type { ShippingOrderReviewFieldConflict } from '@/features/chat/types/shipping.api.types';
|
||
|
||
const props = defineProps<{
|
||
caseNo: string;
|
||
draftVersion: number | null | undefined;
|
||
conflicts: ShippingOrderReviewFieldConflict[] | null | undefined;
|
||
editable: boolean;
|
||
}>();
|
||
|
||
const emit = defineEmits<{
|
||
refreshed: [];
|
||
'focus-conflict': [payload: { fieldCode: string; lineIndex: number | null; lineKey: string | null }];
|
||
}>();
|
||
|
||
const { t, te } = useI18n();
|
||
const msg = useMessage();
|
||
const resolvingKey = ref<string | null>(null);
|
||
const customOpenKey = ref<string | null>(null);
|
||
const customValue = ref('');
|
||
|
||
const openConflicts = computed(() => props.conflicts || []);
|
||
|
||
/**
|
||
* 字段编码本地化标签(无文案时回退 field_code)。
|
||
*/
|
||
function fieldLabel(fieldCode: string): string {
|
||
const key = `chat.shippingOrderReview.fieldConflict.fields.${fieldCode}`;
|
||
return te(key) ? t(key) : fieldCode;
|
||
}
|
||
|
||
/**
|
||
* 冲突原因本地化。
|
||
*/
|
||
function reasonLabel(reason: string): string {
|
||
const key = `chat.shippingOrderReview.fieldConflict.reasons.${reason}`;
|
||
return te(key) ? t(key) : reason;
|
||
}
|
||
|
||
/**
|
||
* 证据角色本地化(主 PDF / 辅助 / 邮件正文)。
|
||
*/
|
||
function evidenceRoleLabel(role: string | undefined | null): string {
|
||
const code = String(role || '').trim();
|
||
if (!code) return '';
|
||
const key = `chat.shippingOrderReview.fieldConflict.evidenceRole.${code}`;
|
||
return te(key) ? t(key) : code;
|
||
}
|
||
|
||
/**
|
||
* 定位冲突字段到草稿/证据区。
|
||
*/
|
||
function onFocus(conflict: ShippingOrderReviewFieldConflict) {
|
||
emit('focus-conflict', {
|
||
fieldCode: conflict.field_code,
|
||
lineIndex: conflict.line_index ?? null,
|
||
lineKey: conflict.line_key ?? null,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 聚焦第一个未解决冲突(Footer / Resolver 委托)。
|
||
*/
|
||
function focusFirstConflict() {
|
||
const first = openConflicts.value[0];
|
||
if (!first) return;
|
||
onFocus(first);
|
||
void Promise.resolve().then(() => {
|
||
document
|
||
.querySelector(`[data-testid="review-field-conflict-${first.conflict_key}"]`)
|
||
?.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||
});
|
||
}
|
||
|
||
defineExpose({
|
||
focusFirstConflict,
|
||
});
|
||
|
||
/**
|
||
* 采用某一候选值。
|
||
*/
|
||
async function adoptOption(conflict: ShippingOrderReviewFieldConflict, optionIndex: number) {
|
||
if (!props.editable || !props.caseNo || props.draftVersion == null || resolvingKey.value) return;
|
||
resolvingKey.value = conflict.conflict_key;
|
||
try {
|
||
const result = await resolveWorkspaceShippingOrderReviewFieldConflict(props.caseNo, conflict.conflict_key, {
|
||
resolution: 'adopt_option',
|
||
option_index: optionIndex,
|
||
expected_draft_version: props.draftVersion,
|
||
reason: t('chat.shippingOrderReview.fieldConflict.resolveReason'),
|
||
});
|
||
showReviewResumeOutcome(result.resume_outcome, msg, t, t('chat.shippingOrderReview.fieldConflict.resolveSuccess'));
|
||
emit('refreshed');
|
||
} catch (error: unknown) {
|
||
const message = error instanceof BusinessError
|
||
? resolveLocalizedErrorMessage(error.code, error.message)
|
||
: (error instanceof Error ? error.message : t('chat.shippingOrderReview.fieldConflict.resolveFailed'));
|
||
msg.warning(message);
|
||
} finally {
|
||
resolvingKey.value = null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 展开/收起手工值输入。
|
||
*/
|
||
function toggleCustom(conflictKey: string) {
|
||
if (customOpenKey.value === conflictKey) {
|
||
customOpenKey.value = null;
|
||
customValue.value = '';
|
||
return;
|
||
}
|
||
customOpenKey.value = conflictKey;
|
||
customValue.value = '';
|
||
}
|
||
|
||
/**
|
||
* 提交手工值裁决。
|
||
*/
|
||
async function submitCustom(conflict: ShippingOrderReviewFieldConflict) {
|
||
const value = customValue.value.trim();
|
||
if (!value || !props.editable || props.draftVersion == null || resolvingKey.value) return;
|
||
resolvingKey.value = conflict.conflict_key;
|
||
try {
|
||
const result = await resolveWorkspaceShippingOrderReviewFieldConflict(props.caseNo, conflict.conflict_key, {
|
||
resolution: 'custom',
|
||
custom_value: value,
|
||
expected_draft_version: props.draftVersion,
|
||
reason: t('chat.shippingOrderReview.fieldConflict.resolveReason'),
|
||
});
|
||
customOpenKey.value = null;
|
||
customValue.value = '';
|
||
showReviewResumeOutcome(result.resume_outcome, msg, t, t('chat.shippingOrderReview.fieldConflict.resolveSuccess'));
|
||
emit('refreshed');
|
||
} catch (error: unknown) {
|
||
const message = error instanceof BusinessError
|
||
? resolveLocalizedErrorMessage(error.code, error.message)
|
||
: (error instanceof Error ? error.message : t('chat.shippingOrderReview.fieldConflict.resolveFailed'));
|
||
msg.warning(message);
|
||
} finally {
|
||
resolvingKey.value = null;
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<section
|
||
v-if="openConflicts.length"
|
||
class="rounded-xl border border-rose-200 bg-rose-50/70 p-3 shadow-sm dark:border-rose-900 dark:bg-rose-950/40"
|
||
data-testid="review-field-conflict-panel"
|
||
>
|
||
<header class="flex flex-wrap items-start justify-between gap-2">
|
||
<div>
|
||
<h2 class="text-sm font-semibold text-rose-950 dark:text-rose-50">
|
||
{{ t('chat.shippingOrderReview.fieldConflict.title', { count: openConflicts.length }) }}
|
||
</h2>
|
||
<p class="mt-0.5 text-xs text-rose-800 dark:text-rose-200">
|
||
{{ t('chat.shippingOrderReview.fieldConflict.subtitle') }}
|
||
</p>
|
||
</div>
|
||
</header>
|
||
|
||
<ul class="mt-3 space-y-2">
|
||
<li
|
||
v-for="conflict in openConflicts"
|
||
:key="conflict.conflict_key"
|
||
class="rounded-lg border border-rose-200 bg-white/95 px-2.5 py-2 dark:border-rose-900 dark:bg-slate-900"
|
||
:data-testid="`review-field-conflict-${conflict.conflict_key}`"
|
||
>
|
||
<div class="flex flex-wrap items-center justify-between gap-2">
|
||
<div class="text-xs font-medium text-rose-950 dark:text-rose-50">
|
||
<span v-if="conflict.line_index != null">#{{ conflict.line_index + 1 }} · </span>
|
||
<span class="font-mono">{{ fieldLabel(conflict.field_code) }}</span>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
class="text-[11px] text-primary underline-offset-2 hover:underline"
|
||
data-testid="review-field-conflict-focus"
|
||
@click="onFocus(conflict)"
|
||
>
|
||
{{ t('chat.shippingOrderReview.fieldConflict.focusEvidence') }}
|
||
</button>
|
||
</div>
|
||
<p class="mt-1 text-[11px] text-muted">{{ reasonLabel(conflict.reason) }}</p>
|
||
|
||
<div class="mt-2 grid gap-1 text-[11px] text-secondary sm:grid-cols-2">
|
||
<div
|
||
v-for="(option, idx) in conflict.options"
|
||
:key="`${conflict.conflict_key}-${idx}`"
|
||
class="rounded border border-default bg-surface-soft px-2 py-1.5"
|
||
>
|
||
<div class="flex flex-wrap items-center gap-1 text-muted">
|
||
<span>
|
||
{{ t('chat.shippingOrderReview.fieldConflict.optionLabel', { index: idx + 1 }) }}
|
||
<span v-if="option.sources?.length" class="ml-1">({{ option.sources.join(', ') }})</span>
|
||
</span>
|
||
<span
|
||
v-if="evidenceRoleLabel(option.evidence_role)"
|
||
class="rounded px-1 py-0.5 text-[10px] font-medium"
|
||
:class="option.evidence_role === 'primary_pdf'
|
||
? 'bg-sky-100 text-sky-800 dark:bg-sky-950 dark:text-sky-200'
|
||
: 'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-200'"
|
||
:data-testid="`review-field-conflict-evidence-role-${idx}`"
|
||
:data-evidence-role="option.evidence_role"
|
||
>
|
||
{{ evidenceRoleLabel(option.evidence_role) }}
|
||
</span>
|
||
</div>
|
||
<div class="mt-0.5 font-mono text-primary">{{ option.display_value || option.value_key || '—' }}</div>
|
||
<button
|
||
v-if="editable"
|
||
type="button"
|
||
class="mt-1.5 rounded border border-rose-300 bg-rose-100 px-2 py-0.5 text-[11px] font-medium text-rose-900 hover:bg-rose-200 disabled:opacity-50 dark:border-rose-800 dark:bg-rose-950 dark:text-rose-100"
|
||
:disabled="resolvingKey != null"
|
||
:data-testid="`review-field-conflict-adopt-${idx}`"
|
||
@click="adoptOption(conflict, idx)"
|
||
>
|
||
{{ t('chat.shippingOrderReview.fieldConflict.adoptOption') }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div v-if="editable" class="mt-2">
|
||
<button
|
||
type="button"
|
||
class="text-[11px] text-secondary underline-offset-2 hover:underline"
|
||
:disabled="resolvingKey != null"
|
||
data-testid="review-field-conflict-custom-toggle"
|
||
@click="toggleCustom(conflict.conflict_key)"
|
||
>
|
||
{{ t('chat.shippingOrderReview.fieldConflict.customValue') }}
|
||
</button>
|
||
<div v-if="customOpenKey === conflict.conflict_key" class="mt-1.5 flex flex-wrap gap-2">
|
||
<input
|
||
v-model="customValue"
|
||
type="text"
|
||
class="min-w-[10rem] flex-1 rounded border border-default bg-surface px-2 py-1 text-xs"
|
||
data-testid="review-field-conflict-custom-input"
|
||
:placeholder="t('chat.shippingOrderReview.fieldConflict.customPlaceholder')"
|
||
>
|
||
<button
|
||
type="button"
|
||
class="rounded bg-primary px-2.5 py-1 text-xs font-medium text-on-primary disabled:opacity-50"
|
||
:disabled="!customValue.trim() || resolvingKey != null"
|
||
data-testid="review-field-conflict-custom-submit"
|
||
@click="submitCustom(conflict)"
|
||
>
|
||
{{ t('chat.shippingOrderReview.fieldConflict.submitCustom') }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
</template>
|