import React, { useState, useEffect } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { Card, Descriptions, Tag, Spin, Empty, Row, Col, Statistic, Table, Button, Divider, Typography, Badge } from 'antd' import { ArrowLeftOutlined, ShopOutlined, FileTextOutlined, DollarOutlined, UserOutlined, PhoneOutlined } from '@ant-design/icons' const { Title, Text } = Typography interface Contact { name: string position: string phone: string is_primary?: boolean } interface Supplier { id: number code: string name: string supply_category: string country: string contacts: Contact[] remark: string total_purchase_amount: number total_paid: number total_payable: number created_at: string } interface Project { id: number project_code: string name: string contract_amount: number status: string customer_id: number supplier_id: number } const SupplierDetail: React.FC = () => { const { id } = useParams<{ id: string }>() const navigate = useNavigate() const [supplier, setSupplier] = useState(null) const [projects, setProjects] = useState([]) const [loading, setLoading] = useState(true) useEffect(() => { fetchSupplierDetail() fetchRelatedProjects() }, [id]) const fetchSupplierDetail = async () => { try { const res = await fetch(`/api/suppliers/${id}`) const data = await res.json() if (data.success) setSupplier(data.data) } catch (error) { console.error('获取供应商详情失败:', error) } finally { setLoading(false) } } const fetchRelatedProjects = async () => { try { // 获取所有项目,筛选关联到此供应商的 const res = await fetch('/api/projects') const data = await res.json() if (data.success) { // 供应商暂无supplier_id关联,先显示空 // 后续可以在项目中添加供应商关联字段 const supplierProjects = (data.data || []).filter((p: Project) => p.supplier_id === parseInt(id)) setProjects(supplierProjects) } } catch (error) { console.error('获取项目失败:', error) } } if (loading) return if (!supplier) return const totalContract = projects.reduce((sum, p) => sum + (parseFloat(p.contract_amount) || 0), 0) // 供应商暂无已付/应付数据,暂时显示0 const totalPaid = 0 const totalPayable = 0 const projectColumns = [ { title: '项目编号', dataIndex: 'project_code', key: 'project_code', width: 120 }, { title: '项目名称', dataIndex: 'name', key: 'name', render: (v: string) => {v} }, { title: '合同金额', dataIndex: 'contract_amount', key: 'contract_amount', align: 'right' as const, render: (v: string) => `¥${(parseFloat(v) || 0).toLocaleString()}` }, { title: '状态', dataIndex: 'status', key: 'status', align: 'center' as const, render: (v: string) => } ] return (
<ShopOutlined style={{ marginRight: 8, color: '#1890ff' }} /> {supplier.name} {/* ========== 卡片1:基本信息 ========== */} 基本信息} style={{ marginBottom: 24, borderRadius: 8 }} headStyle={{ background: '#f5f5f5', fontWeight: 600 }}> {supplier.code} {supplier.supply_category || '-'} {supplier.country || '-'} {supplier.remark && ( <>
备注:
{supplier.remark}
)}
联系人
{(supplier.contacts || []).map((contact, i) => (
{contact.name || '未命名'} {contact.is_primary && 主联系人}
{contact.position &&
职位:{contact.position}
} {contact.phone &&
电话:{contact.phone}
}
))}
{(supplier.contacts || []).length === 0 && }
{/* ========== 卡片2:关联项目 ========== */} 关联项目} style={{ marginBottom: 24, borderRadius: 8 }} headStyle={{ background: '#f5f5f5', fontWeight: 600 }}> {projects.length > 0 ? ( ) : ( )} {/* ========== 卡片3:财务信息 ========== */} 财务信息} style={{ borderRadius: 8 }} headStyle={{ background: '#f5f5f5', fontWeight: 600 }}>
项目明细
{projects.length > 0 ? (
) : ( )} ) } export default SupplierDetail