"use client"; import { useState, useEffect, useCallback } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { templatesApi, type TemplateInfo } from "@/lib/api/client"; import { Upload, Loader2, FileSpreadsheet, FileText } from "lucide-react"; import { toast } from "sonner"; export default function SettingsTemplatesPage() { const [templates, setTemplates] = useState([]); const [loading, setLoading] = useState(true); const [uploading, setUploading] = useState(false); const [dragOver, setDragOver] = useState(false); const loadTemplates = useCallback(async () => { try { const list = await templatesApi.list(); setTemplates(list); } catch { toast.error("加载模板列表失败"); } finally { setLoading(false); } }, []); useEffect(() => { loadTemplates(); }, [loadTemplates]); const handleFile = async (file: File) => { const ext = file.name.toLowerCase().slice(file.name.lastIndexOf(".")); if (![".xlsx", ".xltx", ".docx", ".dotx"].includes(ext)) { toast.error("仅支持 .xlsx、.xltx、.docx、.dotx 文件"); return; } setUploading(true); try { await templatesApi.upload(file); toast.success(`已上传:${file.name}`); await loadTemplates(); } catch (e) { toast.error(e instanceof Error ? e.message : "上传失败"); } finally { setUploading(false); } }; const onDrop = (e: React.DragEvent) => { e.preventDefault(); setDragOver(false); const file = e.dataTransfer.files[0]; if (file) handleFile(file); }; const onDragOver = (e: React.DragEvent) => { e.preventDefault(); setDragOver(true); }; const onDragLeave = () => setDragOver(false); const onSelectFile = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) handleFile(file); e.target.value = ""; }; const formatDate = (ts: number) => new Date(ts * 1000).toLocaleString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", }); return (
← 设置
模板库

上传报价单 Excel(.xlsx / .xltx)或合同 Word(.docx / .dotx),生成报价/合同时可选择使用。

{uploading && (

上传中…

)}

已上传模板

{loading ? (

加载中…

) : templates.length === 0 ? (

暂无模板

) : ( 类型 文件名 大小 上传时间 {templates.map((t) => ( {t.type === "excel" ? ( ) : ( )} {t.name} {(t.size / 1024).toFixed(1)} KB {formatDate(t.uploaded_at)} ))}
)}
); }