import { useState } from "react"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectItem } from "@/components/ui/select"; import jsPDF from "jspdf"; export default function EstimateCalculator() { const [form, setForm] = useState({ customer: "", sales: "", size: "A4", paper: "80P道林", quantity: 100, unitPrice: 0, total: 0, date: new Date().toLocaleDateString(), }); const calcPrice = () => { const pageCount = 16; const bindingCost = 8; let sizeRate = form.size === "A4" ? 1.2 : 0.8; let paperDiff = form.paper === "150P雙銅" ? 5 : 0; const unitPrice = pageCount * sizeRate + paperDiff + bindingCost; const total = unitPrice * form.quantity; setForm({ ...form, unitPrice, total }); }; const saveRecord = () => { const payload = { ...form, }; fetch("https://sheetdb.io/api/v1/wdzj4lvy92xo8", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ data: [payload] }) }).then(res => alert("估價紀錄已儲存")); }; const generatePDF = () => { const doc = new jsPDF(); doc.setFontSize(16); doc.text("估價單", 20, 20); doc.setFontSize(12); doc.text(`客戶姓名:${form.customer}`, 20, 40); doc.text(`業務姓名:${form.sales}`, 20, 50); doc.text(`尺寸:${form.size}`, 20, 60); doc.text(`紙張:${form.paper}`, 20, 70); doc.text(`數量:${form.quantity}`, 20, 80); doc.text(`單本價格:NT$${form.unitPrice.toFixed(0)}`, 20, 90); doc.text(`總價:NT$${form.total.toFixed(0)}`, 20, 100); doc.text(`估價日期:${form.date}`, 20, 110); doc.save("estimate.pdf"); }; return (
客戶姓名
setForm({ ...form, customer: e.target.value })} />
業務姓名
setForm({ ...form, sales: e.target.value })} />
尺寸
setForm({ ...form, size: value })} > A4 A5
紙張
setForm({ ...form, paper: value })} > 80P道林 150P雙銅
數量
setForm({ ...form, quantity: parseInt(e.target.value) }) } />
估價
單本價格:NT${form.unitPrice.toFixed(0)}
總價:NT${form.total.toFixed(0)}
儲存紀錄
匯出 PDF
); }