import { useState, useEffect } from 'react'; import { useRouter } from 'next/router'; import Head from 'next/head'; import { toast } from 'react-hot-toast'; import { MagnifyingGlassIcon, ReceiptPercentIcon, CloudArrowDownIcon, EyeIcon, PrinterIcon, DocumentTextIcon, CheckCircleIcon, ClockIcon, ExclamationCircleIcon, ChevronLeftIcon, ChevronRightIcon, DocumentArrowDownIcon, CalendarIcon, BuildingOfficeIcon, CurrencyDollarIcon } from '@heroicons/react/24/outline'; import { supabase, TABLES } from '@/lib/supabase'; import { getDemoData } from '@/lib/demo-data'; import { formatTime, formatCurrency } from '@/utils'; import Layout from '@/components/Layout'; interface Invoice { id: string; invoice_number: string; user_id: string; user_name: string; user_email: string; order_id: string; invoice_type: 'individual' | 'enterprise'; // 个人发票信息 personal_name?: string; // 企业发票信息 company_name?: string; tax_number?: string; company_address?: string; company_phone?: string; bank_name?: string; bank_account?: string; // 发票金额信息 subtotal: number; tax_amount: number; total_amount: number; currency: string; // 发票状态 status: 'draft' | 'issued' | 'paid' | 'cancelled'; issue_date?: string; due_date?: string; paid_date?: string; // 服务明细 items: InvoiceItem[]; created_at: string; updated_at: string; } interface InvoiceItem { service_type: string; service_name: string; quantity: number; unit: string; unit_price: number; amount: number; } interface InvoiceFilters { search: string; status: 'all' | 'draft' | 'issued' | 'paid' | 'cancelled'; invoice_type: 'all' | 'individual' | 'enterprise'; date_range: 'all' | 'today' | 'week' | 'month' | 'quarter'; sortBy: 'created_at' | 'total_amount' | 'issue_date'; sortOrder: 'asc' | 'desc'; } export default function InvoicesPage() { const [invoices, setInvoices] = useState([]); const [loading, setLoading] = useState(true); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [totalCount, setTotalCount] = useState(0); const [isDemoMode, setIsDemoMode] = useState(false); const [filters, setFilters] = useState({ search: '', status: 'all', invoice_type: 'all', date_range: 'all', sortBy: 'created_at', sortOrder: 'desc' }); const router = useRouter(); const pageSize = 20; // 获取发票数据 const fetchInvoices = async (page = 1) => { try { setLoading(true); // 检查是否为演示模式 const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const isDemo = !supabaseUrl || supabaseUrl === 'https://demo.supabase.co' || supabaseUrl === ''; setIsDemoMode(isDemo); if (isDemo) { // 使用演示数据 const result = await getDemoData.invoices(); setInvoices(result); setTotalCount(result.length); setTotalPages(Math.ceil(result.length / pageSize)); setCurrentPage(page); } else { // 使用真实数据 - 这里需要根据实际数据库结构调整 // 暂时使用演示数据 const result = await getDemoData.invoices(); setInvoices(result); setTotalCount(result.length); setTotalPages(Math.ceil(result.length / pageSize)); setCurrentPage(page); } } catch (error) { console.error('Error fetching invoices:', error); toast.error('获取发票列表失败'); } finally { setLoading(false); } }; // 处理筛选变更 const handleFilterChange = (key: keyof InvoiceFilters, value: any) => { setFilters(prev => ({ ...prev, [key]: value })); }; // 应用筛选 const applyFilters = () => { setCurrentPage(1); fetchInvoices(1); }; // 重置筛选 const resetFilters = () => { setFilters({ search: '', status: 'all', invoice_type: 'all', date_range: 'all', sortBy: 'created_at', sortOrder: 'desc' }); setCurrentPage(1); fetchInvoices(1); }; // 获取状态颜色 const getStatusColor = (status: string) => { switch (status) { case 'paid': return 'bg-green-100 text-green-800'; case 'issued': return 'bg-blue-100 text-blue-800'; case 'draft': return 'bg-yellow-100 text-yellow-800'; case 'cancelled': return 'bg-red-100 text-red-800'; default: return 'bg-gray-100 text-gray-800'; } }; // 获取状态文本 const getStatusText = (status: string) => { switch (status) { case 'paid': return '已付款'; case 'issued': return '已开具'; case 'draft': return '草稿'; case 'cancelled': return '已取消'; default: return '未知'; } }; // 获取状态图标 const getStatusIcon = (status: string) => { switch (status) { case 'paid': return ; case 'issued': return ; case 'draft': return ; case 'cancelled': return ; default: return ; } }; // 下载发票 const handleDownloadInvoice = async (invoiceId: string) => { try { // 这里应该调用实际的发票下载API toast.success('发票下载已开始'); // 模拟下载过程 const invoice = invoices.find(inv => inv.id === invoiceId); if (invoice) { // 创建一个虚拟的下载链接 const element = document.createElement('a'); const file = new Blob([`发票号: ${invoice.invoice_number}\n金额: ${formatCurrency(invoice.total_amount)}`], { type: 'text/plain' }); element.href = URL.createObjectURL(file); element.download = `invoice-${invoice.invoice_number}.txt`; document.body.appendChild(element); element.click(); document.body.removeChild(element); } } catch (error) { console.error('Error downloading invoice:', error); toast.error('发票下载失败'); } }; // 打印发票 const handlePrintInvoice = (invoiceId: string) => { try { // 这里应该打开打印预览 toast.success('正在准备打印...'); window.print(); } catch (error) { console.error('Error printing invoice:', error); toast.error('发票打印失败'); } }; // 生成发票 const handleGenerateInvoice = async (orderId: string) => { try { // 这里应该调用生成发票API toast.success('发票生成成功'); fetchInvoices(); } catch (error) { toast.error('生成发票失败'); } }; useEffect(() => { fetchInvoices(); }, []); return ( 发票管理 - 口译服务管理后台
{/* 页面标题 */}

发票管理

{/* 搜索和筛选 */}
{/* 搜索框 */}
handleFilterChange('search', e.target.value)} className="block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md leading-5 bg-white placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:ring-1 focus:ring-blue-500 focus:border-blue-500" />
{/* 状态筛选 */}
{/* 类型筛选 */}
{/* 日期范围筛选 */}
{/* 发票列表 */} {loading ? (
) : (

发票列表 ({totalCount} 张发票)

{invoices.map((invoice) => (

发票号: {invoice.invoice_number}

{invoice.invoice_type === 'enterprise' ? ( {invoice.company_name} ) : ( {invoice.personal_name || invoice.user_name} )}

金额: ¥{invoice.total_amount.toFixed(2)} | 税额: ¥{invoice.tax_amount.toFixed(2)} | 总计: ¥{invoice.total_amount.toFixed(2)}

{invoice.tax_number && (

税号: {invoice.tax_number}

)}
{/* 状态 */}
{getStatusIcon(invoice.status)} {getStatusText(invoice.status)}
{/* 操作按钮 */}
{/* 日期信息 */}
创建: {formatTime(invoice.created_at)}
{invoice.issue_date && (
开具: {formatTime(invoice.issue_date)}
)} {invoice.paid_date && (
付款: {formatTime(invoice.paid_date)}
)}
))} {invoices.length === 0 && (

暂无发票

开始生成您的第一张发票

)}
{/* 分页 */} {totalPages > 1 && (

显示第 {(currentPage - 1) * pageSize + 1} 到{' '} {Math.min(currentPage * pageSize, totalCount)} {' '} 张,共 {totalCount} 张发票

)}
)}
); }