import React from 'react'; import { Banknote, Globe, CreditCard, Info } from 'lucide-react'; import { VendorState, CalculationResult } from '../types'; import { formatNumber } from '../utils/math'; interface Props { id: string; title: string; state: VendorState; onChange: (state: VendorState) => void; result: CalculationResult; bestOptionId: string | null; usdAvailable: number; } export const VendorCard: React.FC = ({ id, title, state, onChange, result, bestOptionId, usdAvailable }) => { const hasPrice = parseFloat(state.price) > 0; const options = [ { key: 'cash', label: 'Tiền mặt', icon: Banknote, value: result.cashVnd, possible: result.isCashPossible, formula: result.formulas.cash }, { key: 'online', label: 'Online Transaction', icon: Globe, value: result.onlineVnd, possible: true, formula: result.formulas.online }, { key: 'pos', label: 'Quẹt POS', icon: CreditCard, value: result.posVnd, possible: true, formula: result.formulas.pos }, ]; const handlePriceChange = (value: string) => { // Nếu là USD, cho phép nhập dấu chấm và tối đa 2 số thập phân if (state.currency === 'USD') { const cleaned = value.replace(/[^0-9.]/g, ''); const parts = cleaned.split('.'); if (parts.length > 2) return; // Không cho nhập 2 dấu chấm onChange({ ...state, price: cleaned }); } else { const numericStr = value.replace(/\D/g, ''); onChange({ ...state, price: numericStr }); } }; const getDisplayPrice = () => { if (state.currency === 'USD') return state.price; return formatNumber(state.price); }; return (

{title}

handlePriceChange(e.target.value)} placeholder="Nhập giá..." className="flex-1 w-full min-w-0 bg-white border border-slate-300 rounded-xl px-3 py-2.5 font-bold text-slate-800 outline-none focus:border-indigo-500 focus:ring-2 focus:ring-indigo-500/20 text-lg shadow-sm" />
{['USD', 'IDR'].map((cur) => ( ))}
{options.map((opt) => { const optionId = `${id}-${opt.key}`; const isBest = bestOptionId === optionId; const isIlliquid = opt.key === 'cash' && !opt.possible && hasPrice; return (
{opt.label} {hasPrice && opt.formula && (
{opt.formula}
)}
{opt.value > 0 ? `${formatNumber(opt.value)} ₫` : '---'}
{opt.key === 'pos' && ( )} {isIlliquid && (
Vượt thanh khoản! (Cần {result.usdNeeded.toFixed(1)}$ / Có {usdAvailable}$)
)}
); })}
); };