'use client'; import { useState, useEffect, useMemo } from 'react'; import api from '@/lib/api'; import { Loader2, X, Settings, Package, MapPin, DollarSign, Cpu, Sparkles, FileJson, Check, Tag, Layers, ArrowRight, TrendingUp, Info } from 'lucide-react'; import { SchemaItem } from '@/types'; import { toast } from 'react-hot-toast'; interface ProductModalProps { isOpen: boolean; onClose: () => void; product?: any; onSubmit: (data: any) => Promise; onManageSchemas: () => void; } const POPULAR_COUNTRIES = [ 'France', 'Germany', 'Italy', 'Spain', 'United Kingdom', 'United States', 'Japan', 'Singapore', 'Switzerland', 'Australia', ]; const POPULAR_PROVIDERS = ['TROOV', 'TLS', 'VFS', 'BLS', 'Capago']; const VISA_OPTIONS = [ { label: '旅游签证', value: 'Tourist', desc: '短期旅行观光' }, { label: '商务签证', value: 'Business', desc: '商务考察会议' }, { label: '探亲访友', value: 'Family', desc: '探访亲友团聚' }, { label: '留学签证', value: 'Student', desc: '院校留学研学' }, { label: '工作签证', value: 'Work', desc: '海外雇佣就业' }, { label: '过境签证', value: 'Transit', desc: '转机过境签' }, { label: '电子签证', value: 'e-Visa', desc: '在线电子直签' }, ]; export default function ProductModal({ isOpen, onClose, product, onSubmit, onManageSchemas, }: ProductModalProps) { const [loading, setLoading] = useState(false); const [schemas, setSchemas] = useState([]); const [selectedSchemaDetail, setSelectedSchemaDetail] = useState(null); const [loadingSchemaDetail, setLoadingSchemaDetail] = useState(false); const [form, setForm] = useState({ title: '', description: '', country: '', city: '', price_amount: 0, price_currency: 'CNY', provider: 'TROOV', visa_type: 'Tourist', schema_id: 0, enabled: 1, recommend_score: 0, }); useEffect(() => { if (isOpen) { // 1. 加载 Schema 列表 api .get('/api/vas/schema/list') .then((res) => { setSchemas(Array.isArray(res.data?.data) ? res.data.data : []); }) .catch(() => { setSchemas([]); }); // 2. 初始化表单 if (product) { setForm({ title: product.title || '', description: product.description || '', country: product.country || '', city: product.city || '', price_amount: (product.price_amount || 0) / 100, // 分转元 price_currency: product.price_currency || 'CNY', provider: product.provider || 'TROOV', visa_type: product.visa_type || 'Tourist', schema_id: product.schema_id || 0, enabled: product.enabled ? 1 : 0, recommend_score: product.recommend_score ?? 0, }); if (product.schema_id) { fetchSchemaDetail(product.schema_id); } else { setSelectedSchemaDetail(null); } } else { setForm({ title: '', description: '', country: '', city: '', price_amount: 0, price_currency: 'CNY', provider: 'TROOV', visa_type: 'Tourist', schema_id: 0, enabled: 1, recommend_score: 0, }); setSelectedSchemaDetail(null); } } }, [isOpen, product]); // 当选择的 schema_id 变化时,拉取详情以渲染即时预览 const fetchSchemaDetail = async (schemaId: number) => { if (!schemaId) { setSelectedSchemaDetail(null); return; } setLoadingSchemaDetail(true); try { const res = await api.get('/api/vas/schema/detail', { params: { schema_id: schemaId, id: schemaId }, }); setSelectedSchemaDetail(res.data?.data || res.data); } catch (e) { const fallback = schemas.find((s) => s.id === schemaId); setSelectedSchemaDetail(fallback || null); } finally { setLoadingSchemaDetail(false); } }; const handleSchemaChange = (schemaId: number) => { setForm((prev) => ({ ...prev, schema_id: schemaId })); fetchSchemaDetail(schemaId); }; // 解析选中的 Schema 字段列表 const parsedSchemaInfo = useMemo(() => { if (!selectedSchemaDetail) return null; const raw = selectedSchemaDetail.schema_json || selectedSchemaDetail.content; let properties: Record = {}; let required: string[] = []; if (raw) { try { const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw; properties = parsed.properties || {}; required = parsed.required || []; } catch (e) {} } else if (selectedSchemaDetail.properties) { properties = selectedSchemaDetail.properties; required = selectedSchemaDetail.required || []; } return { name: selectedSchemaDetail.name, id: selectedSchemaDetail.id, fieldsCount: Object.keys(properties).length, properties, required, }; }, [selectedSchemaDetail]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!form.title.trim()) { toast.error('请填写商品标题'); return; } if (!form.country.trim()) { toast.error('请填写国家或地区'); return; } setLoading(true); try { await onSubmit({ ...form, price_amount: Math.round(Number(form.price_amount) * 100), // 元转分 id: product?.id, recommend_score: Number(form.recommend_score || 0), }); onClose(); } catch (e: any) { console.error(e); } finally { setLoading(false); } }; if (!isOpen) return null; return (
{/* Modal Header */}

{product ? `编辑服务商品 #${product.id}` : '发布新服务商品'}

{product && ( ID #{product.id} )}

完整配置商品基础属性、地域分类、定价机制与动态表单 Schema 绑定

{/* Modal Body Form */}
{/* 1. 基础服务信息卡片 */}

1. 基础服务信息

setForm({ ...form, title: e.target.value })} />