'use client';
import React, { useState } from 'react';
import { AlertTriangle, Copy, FileText, LayoutGrid, ListOrdered, Loader2, Save, TrendingUp } from 'lucide-react';
import type { CalculationResult, ProductType } from '@/types';
import { formatNumber, formatVnd } from '@/lib/format';
import { PRODUCT_UNITS, WORK_TYPE_LABELS } from '@/lib/engine/constants';
import { Alert, Button, Segmented, cn } from '../ui';
import { ImpositionVisualizer } from '../ImpositionVisualizer';
import { BreakEvenChart } from '../BreakEvenChart';
export function ResultPanel({
result,
error,
calculating,
fresh,
quantity,
productType,
widthMm,
heightMm,
savedCode,
saving,
onSave,
onPrint,
onCopy,
onUseBestTech,
}: {
result: CalculationResult | null;
error: string | null;
calculating: boolean;
fresh: boolean;
quantity: number;
productType: ProductType;
widthMm: number;
heightMm: number;
savedCode?: string;
saving: boolean;
onSave: () => void;
onPrint: () => void;
onCopy: () => void;
onUseBestTech?: () => void;
}) {
const [tab, setTab] = useState<'breakdown' | 'imposition' | 'compare'>('breakdown');
if (!result) {
return (
{error ? (
{error}
) : (
Đang tính giá…
)}
);
}
const c = result.costs;
const q = result.quantities;
const unit = PRODUCT_UNITS[productType];
const stale = !fresh;
const techLabel =
result.chosenTech === 'offset'
? 'In offset'
: result.printing.machineName.includes('INTC')
? 'In nhanh kèm giấy'
: 'In nhanh gia công';
const rec = result.recommendation;
const otherCheaper = rec.bestTech && rec.bestTech !== result.chosenTech;
const baseUnitCost = q.productQty ? c.totalBaseCost / q.productQty : 0;
return (
{/* ---------- Tổng giá ---------- */}
{techLabel}
Vốn: {formatVnd(baseUnitCost)}/{unit}
{savedCode && {savedCode}}
{calculating && (
)}
Tổng thanh toán{c.vatAmount > 0 ? ` (gồm VAT ${c.vatPercent}%)` : ''}
{formatVnd(c.finalPrice)}
{formatVnd(c.unitPrice)} / {unit} · {formatNumber(quantity)} {unit}
{error && (
{error}
)}
{result.warnings.length > 0 && (
{result.warnings.map((w, i) => (
{w}
))}
)}
{otherCheaper && (
{rec.bestTech === 'digital' ? 'In nhanh' : 'In offset'} rẻ hơn:{' '}
{formatVnd(rec.bestTech === 'digital' ? rec.digitalTotal : rec.offsetTotal)}
{onUseBestTech && (
)}
)}
} loading={saving} disabled={stale || !!savedCode} onClick={onSave}>
{savedCode ? 'Đã lưu' : 'Lưu báo giá'}
} disabled={stale} onClick={onPrint}>
In phiếu
} disabled={stale} onClick={onCopy}>
Copy Zalo
0
? `${formatNumber(q.netPrintSheets)} + ${formatNumber(q.printWasteSheets + q.finishingWasteSheets)} bù hao`
: 'Không bù hao'
}
/>
{/* ---------- Chi tiết ---------- */}
Chi tiết },
{ value: 'imposition', label: Bình trang },
{ value: 'compare', label: So sánh },
]}
/>
{tab === 'breakdown' && }
{tab === 'imposition' && }
{tab === 'compare' && }
);
}
function Stat({ label, value, sub }: { label: string; value: string; sub?: string }) {
return (
{label}
{value}
{sub && {sub}}
);
}
function Line({ label, note, amount, strong, negative, className }: { label: React.ReactNode; note?: string; amount: number; strong?: boolean; negative?: boolean; className?: string }) {
return (
{negative ? '−' : ''}
{formatVnd(amount)}
);
}
function Breakdown({ result }: { result: CalculationResult }) {
const c = result.costs;
const showPreTax = c.vatPercent > 0 || c.discountAmount > 0;
return (
1. Giấy
2. In ấn{result.printing.workType ? ` · ${WORK_TYPE_LABELS[result.printing.workType]}` : ''}
{result.printing.lines.map((l, i) => (
))}
{c.finishingDetails.length > 0 && (
3. Gia công
{c.finishingDetails.map((f) => (
))}
)}
{c.discountAmount > 0 && }
{showPreTax && }
{c.vatPercent > 0 && }
);
}
function SectionLabel({ children }: { children: React.ReactNode }) {
return {children}
;
}