import React, { useState, useEffect } from 'react' import { Table, Button, Modal, Form, Input, Select, message, Space, Tag, Card, Row, Col, Statistic } from 'antd' import { PlusOutlined, EditOutlined, DeleteOutlined, SearchOutlined, ShopOutlined } from '@ant-design/icons' import type { ColumnsType } from 'antd/es/table' interface Supplier { id: number code: string name: string type: string country: string total_purchase_amount: number total_paid: number total_payable: number rating: number created_at: string } const SupplierPage: React.FC = () => { const [suppliers, setSuppliers] = useState([]) const [loading, setLoading] = useState(false) const [modalVisible, setModalVisible] = useState(false) const [editingSupplier, setEditingSupplier] = useState(null) const [searchText, setSearchText] = useState('') const [form] = Form.useForm() // 统计数据 const stats = { total: suppliers.length, totalPurchase: suppliers.reduce((sum, s) => sum + s.total_purchase_amount, 0), totalPayable: suppliers.reduce((sum, s) => sum + s.total_payable, 0), avgRating: suppliers.length > 0 ? suppliers.reduce((sum, s) => sum + s.rating, 0) / suppliers.length : 0 } // 获取供应商列表 const fetchSuppliers = async () => { setLoading(true) try { const response = await fetch('/api/suppliers') const data = await response.json() if (data.success) { setSuppliers(data.data || []) } else { message.error('获取供应商列表失败') } } catch (error) { console.error('获取供应商失败:', error) message.error('网络错误') } finally { setLoading(false) } } useEffect(() => { fetchSuppliers() }, []) // 表格列定义 const columns: ColumnsType = [ { title: '编号', dataIndex: 'code', key: 'code', width: 120, sorter: (a, b) => a.code.localeCompare(b.code) }, { title: '名称', dataIndex: 'name', key: 'name', render: (text) => {text} }, { title: '类型', dataIndex: 'type', key: 'type', width: 100, render: (type) => { const typeMap: Record = { 'china': { color: 'red', text: '中国供应商' }, 'local': { color: 'green', text: '本地供应商' }, 'international': { color: 'blue', text: '国际供应商' } } const info = typeMap[type] || { color: 'default', text: type } return {info.text} } }, { title: '国家', dataIndex: 'country', key: 'country', width: 100, render: (country) => ( {country} ) }, { title: '评分', dataIndex: 'rating', key: 'rating', width: 100, render: (rating) => { const stars = '★'.repeat(rating) + '☆'.repeat(5 - rating) return (
= 4 ? '#52c41a' : rating >= 3 ? '#faad14' : '#ff4d4f' }}> {stars}
) }, sorter: (a, b) => a.rating - b.rating }, { title: '采购金额', dataIndex: 'total_purchase_amount', key: 'total_purchase_amount', width: 150, render: (amount) => `¥${amount.toLocaleString()}`, sorter: (a, b) => a.total_purchase_amount - b.total_purchase_amount }, { title: '应付金额', dataIndex: 'total_payable', key: 'total_payable', width: 150, render: (amount) => ( 0 ? '#ff4d4f' : '#52c41a' }}> ¥{amount.toLocaleString()} ), sorter: (a, b) => a.total_payable - b.total_payable }, { title: '操作', key: 'actions', width: 120, render: (_, record) => ( {/* 表格 */} `共 ${total} 条` }} scroll={{ x: 1000 }} /> {/* 模态框 */} { setModalVisible(false); form.resetFields(); setEditingSupplier(null) }} onOk={() => form.submit()} width={600} >
) } export default SupplierPage