| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 'use client';
- import { useState, useEffect } from 'react';
- import { X, Loader2, Save, ArrowRight } from 'lucide-react';
- import { toast } from 'react-hot-toast';
- import { orderService } from '@/lib/services';
- import { OrderDetail } from '@/types';
- interface OrderPriceAdjustModalProps {
- isOpen: boolean;
- onClose: () => void;
- order: OrderDetail | null;
- onSuccess: () => void;
- }
- export default function OrderPriceAdjustModal({
- isOpen,
- onClose,
- order,
- onSuccess,
- }: OrderPriceAdjustModalProps) {
- const [loading, setLoading] = useState(false);
- const [formData, setFormData] = useState({
- adjustment_delta: '',
- reason: '管理员手动调价',
- });
- useEffect(() => {
- if (isOpen) {
- setFormData({
- adjustment_delta: '',
- reason: '管理员手动调价',
- });
- }
- }, [isOpen, order]);
- if (!isOpen || !order) return null;
- const baseAmount = order ? (order.base_amount ?? order.total_amount ?? order.amount ?? 0) : 0;
- const currency = order.base_currency || 'CNY';
- const symbol = currency === 'CNY' ? '¥' : currency === 'USD' ? '$' : `${currency} `;
- const deltaValue = parseFloat(formData.adjustment_delta) || 0;
- const deltaCents = Math.round(deltaValue * 100);
- const estimatedFinalAmount = Math.max(0, baseAmount + deltaCents);
- const handleSubmit = async (e: React.FormEvent) => {
- e.preventDefault();
- if (!order?.id) return;
- if (isNaN(deltaValue)) {
- toast.error('请输入有效的金额');
- return;
- }
- if (deltaValue === 0) {
- toast.error('调整金额不能为 0');
- return;
- }
- setLoading(true);
- try {
- await orderService.adjustPrice(order.id, {
- adjustment_delta: deltaCents,
- reason: formData.reason,
- });
- toast.success('价格调整成功');
- onSuccess();
- onClose();
- } catch (error: any) {
- toast.error('调整失败: ' + (error.response?.data?.message || '未知错误'));
- } finally {
- setLoading(false);
- }
- };
- return (
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-4 font-mono">
- <div className="bg-neutral-50 border-2 border-black shadow-[8px_8px_0px_#000] w-full max-w-md overflow-hidden animate-in zoom-in-95 duration-200">
- {/* Header */}
- <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
- <div className="flex items-center gap-2 text-xs font-bold uppercase tracking-wider">
- <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
- <span>[ PRICE ADJUSTMENT // #{order.id} ]</span>
- </div>
- <button
- onClick={onClose}
- className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
- >
- ✕
- </button>
- </div>
- <form onSubmit={handleSubmit} className="p-6 bg-white space-y-4">
- {/* Price Preview */}
- <div className="bg-neutral-50 p-4 border-2 border-black shadow-[2px_2px_0px_#000] space-y-2 text-xs">
- <div className="flex items-center justify-between text-neutral-500 uppercase">
- <span>原订单金额</span>
- <span>调价增减</span>
- <span>最终核算应付</span>
- </div>
- <div className="flex items-center justify-between font-mono font-black">
- <div className="text-black">
- {symbol}{(baseAmount / 100).toFixed(2)}
- </div>
- <div className="flex items-center gap-1">
- <span
- className={`px-1.5 py-0.5 border border-black text-xs ${deltaValue < 0
- ? 'bg-amber-200 text-black'
- : deltaValue > 0
- ? 'bg-rose-100 text-rose-800'
- : 'bg-neutral-200 text-black'
- }`}
- >
- {deltaValue > 0 ? `+${deltaValue.toFixed(2)}` : deltaValue.toFixed(2)}
- </span>
- <ArrowRight size={12} className="text-neutral-500" />
- </div>
- <div className="text-sm text-black bg-amber-300 px-1.5 py-0.5 border border-black">
- {symbol}{(estimatedFinalAmount / 100).toFixed(2)}
- </div>
- </div>
- </div>
- {/* Input field: delta */}
- <div className="space-y-1">
- <label className="block text-xs font-bold text-black uppercase">
- [ 01 // 调整金额差额 ({currency} / 元) ]: <span className="text-black">*</span>
- </label>
- <div className="relative">
- <input
- type="number"
- step="0.01"
- required
- className="w-full border-2 border-black bg-neutral-50 p-2.5 text-xs font-mono focus:bg-white outline-none shadow-[2px_2px_0px_#000] text-black"
- placeholder="例如: -50 (立减) 或 100 (加价)"
- value={formData.adjustment_delta}
- onChange={(e) => setFormData({ ...formData, adjustment_delta: e.target.value })}
- />
- <span className="absolute right-3 top-2.5 text-xs font-bold text-neutral-500">
- {currency}
- </span>
- </div>
- <p className="text-[10px] text-neutral-500 font-sans">
- * 输入负数(如 -50)表示优惠减免,输入正数(如 50)表示加收加急费
- </p>
- </div>
- {/* Input field: reason */}
- <div className="space-y-1">
- <label className="block text-xs font-bold text-black uppercase">
- [ 02 // 调价原因与核销备注 ]: <span className="text-black">*</span>
- </label>
- <input
- type="text"
- required
- className="w-full border-2 border-black bg-neutral-50 p-2.5 text-xs font-sans focus:bg-white outline-none shadow-[2px_2px_0px_#000] text-black"
- value={formData.reason}
- onChange={(e) => setFormData({ ...formData, reason: e.target.value })}
- placeholder="例如:老客户优惠立减 / 加急服务费"
- />
- </div>
- {/* Actions */}
- <div className="pt-2 flex items-center gap-3 border-t-2 border-dashed border-neutral-300">
- <button
- type="button"
- onClick={onClose}
- className="flex-1 px-4 py-2 border-2 border-black bg-neutral-100 text-black text-xs font-bold shadow-[2px_2px_0px_#000] hover:bg-neutral-200 transition active:translate-x-0.5 active:translate-y-0.5 uppercase"
- >
- [ 取消 ]
- </button>
- <button
- type="submit"
- disabled={loading || !formData.adjustment_delta || deltaValue === 0}
- className="flex-1 bg-amber-300 hover:bg-amber-400 text-black border-2 border-black py-2 text-xs font-black shadow-[3px_3px_0px_#000] transition active:translate-x-0.5 active:translate-y-0.5 active:shadow-none flex items-center justify-center gap-1.5 uppercase disabled:opacity-50"
- >
- {loading ? <Loader2 size={14} className="animate-spin" /> : <><Save size={13} /> <span>[ 确认调价生效 ]</span></>}
- </button>
- </div>
- </form>
- </div>
- </div>
- );
- }
|