import React, { useState, useEffect } from 'react'; import { PlusIcon, PencilIcon, TrashIcon, EyeIcon, MagnifyingGlassIcon, FunnelIcon } from '@heroicons/react/24/outline'; import { invoke } from '@tauri-apps/api/core'; import { useNotifications } from '../NotificationSystem'; import { PROJECT_ID } from './const'; interface OutfitItem { id: string; project_id: string; analysis_id?: string; name: string; category: string; description?: string; color_pattern?: any; design_styles?: string[]; brand?: string; size?: string; price?: number; purchase_date?: string; image_path?: string; tags?: string[]; notes?: string; created_at: string; updated_at: string; } interface OutfitItemListProps { projectId: string; onCreateItem?: () => void; onEditItem?: (item: OutfitItem) => void; } const OutfitItemList: React.FC = ({ projectId, onCreateItem, onEditItem }) => { const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [searchTerm, setSearchTerm] = useState(''); const [selectedCategory, setSelectedCategory] = useState(''); const [selectedItem, setSelectedItem] = useState(null); const { addNotification } = useNotifications(); // 加载服装单品列表 const loadItems = async () => { try { setLoading(true); const options = { project_id: PROJECT_ID, category: selectedCategory || null, limit: 100, offset: 0 }; const result = await invoke('list_outfit_items', { options }); setItems(result as OutfitItem[]); } catch (error) { console.error('加载服装单品失败:', error); addNotification({ type: 'error', title: '加载失败', message: '无法加载服装单品列表' }); } finally { setLoading(false); } }; // 删除服装单品 const handleDeleteItem = async (itemId: string) => { if (!window.confirm('确定要删除这个服装单品吗?')) { return; } try { await invoke('delete_outfit_item', { id: itemId }); addNotification({ type: 'success', title: '删除成功', message: '服装单品已删除' }); // 重新加载列表 loadItems(); } catch (error) { console.error('删除服装单品失败:', error); addNotification({ type: 'error', title: '删除失败', message: error instanceof Error ? error.message : '删除服装单品失败' }); } }; // 过滤服装单品 const filteredItems = items.filter(item => { const matchesSearch = !searchTerm || item.name.toLowerCase().includes(searchTerm.toLowerCase()) || item.description?.toLowerCase().includes(searchTerm.toLowerCase()) || item.brand?.toLowerCase().includes(searchTerm.toLowerCase()); const matchesCategory = !selectedCategory || item.category === selectedCategory; return matchesSearch && matchesCategory; }); // 获取所有类别 const categories = Array.from(new Set(items.map(item => item.category))).filter(Boolean); // 格式化价格 const formatPrice = (price?: number) => { if (!price) return ''; return `¥${price.toFixed(2)}`; }; // 格式化时间 const formatTime = (timeString: string) => { return new Date(timeString).toLocaleString('zh-CN'); }; useEffect(() => { if (projectId) { loadItems(); } }, [projectId, selectedCategory]); if (loading) { return (
加载中...
); } return (
{/* 工具栏 */}
{/* 搜索框 */}
setSearchTerm(e.target.value)} className="pl-10 pr-4 py-2 w-full border border-gray-300 rounded-md focus:ring-2 focus:ring-primary-500 focus:border-primary-500" />
{/* 类别筛选 */}
{/* 添加按钮 */}
{/* 统计信息 */}
共 {filteredItems.length} 个服装单品 {searchTerm || selectedCategory ? ( 从 {items.length} 个单品中筛选 ) : null}
{/* 服装单品列表 */} {filteredItems.length === 0 ? (

{searchTerm || selectedCategory ? '没有找到匹配的服装单品' : '暂无服装单品'}

{searchTerm || selectedCategory ? '请尝试调整搜索条件' : '请先上传图片进行AI分析,或手动添加服装单品'}

{!searchTerm && !selectedCategory && ( )}
) : (
{filteredItems.map((item) => (
{/* 图片预览 */}
{item.image_path ? ( {item.name} { (e.target as HTMLImageElement).style.display = 'none'; }} /> ) : (
暂无图片
)}
{/* 单品信息 */}

{item.name}

{item.category}
{item.brand && (

品牌: {item.brand}

)} {item.price && (

{formatPrice(item.price)}

)} {item.description && (

{item.description}

)}

创建时间: {formatTime(item.created_at)}

{/* 操作按钮 */}
))}
)} {/* 详情模态框 */} {selectedItem && (

服装单品详情

名称: {selectedItem.name}
类别: {selectedItem.category}
{selectedItem.brand && (
品牌: {selectedItem.brand}
)} {selectedItem.size && (
尺寸: {selectedItem.size}
)} {selectedItem.price && (
价格: {formatPrice(selectedItem.price)}
)} {selectedItem.description && (
描述:

{selectedItem.description}

)} {selectedItem.design_styles && selectedItem.design_styles.length > 0 && (
设计风格:
{selectedItem.design_styles.map((style, index) => ( {style} ))}
)} {selectedItem.tags && selectedItem.tags.length > 0 && (
标签:
{selectedItem.tags.map((tag, index) => ( {tag} ))}
)}
创建时间: {formatTime(selectedItem.created_at)}
更新时间: {formatTime(selectedItem.updated_at)}
)}
); }; export default OutfitItemList;