QRCodesView.tsx
10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React, { useState, useCallback } from 'react';
import { QRCodeSVG } from 'qrcode.react';
import { Download, Plus } from 'lucide-react';
import { Button } from '../ui/button';
import { Input } from '../ui/input';
import { Label } from '../ui/label';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '../ui/select';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '../ui/table';
import { cn } from '../ui/utils';
// Notes:
// 1. Enter quantity to generate the corresponding number of QR codes
// 2. QR code size can be selected
// 3. Each generated QR has a numeric ID for manual entry when scanning is not available (e.g. recharge)
// 4. Generated QR codes can be bulk exported for batch printing
// 5. QR report shows generation status and usage status
export type QRSize = 'small' | 'medium' | 'large';
const SIZE_OPTIONS: { value: QRSize; label: string; pixels: number }[] = [
{ value: 'small', label: 'Small (80px)', pixels: 80 },
{ value: 'medium', label: 'Medium (120px)', pixels: 120 },
{ value: 'large', label: 'Large (160px)', pixels: 160 },
];
export type QRUsageStatus = 'Unused' | 'Used';
export interface QRCodeItem {
id: string;
numericId: string; // For manual entry when scan doesn't work (e.g. recharge)
value: string; // Encoded in QR (same as numericId or extended code)
usageStatus: QRUsageStatus;
createdAt: string;
}
const PREFIX = 'QR';
let nextNumericId = 100001;
function generateNumericId(): string {
return String(nextNumericId++);
}
export function QRCodesView() {
const [quantity, setQuantity] = useState<number>(10);
const [sizeOption, setSizeOption] = useState<QRSize>('medium');
const [items, setItems] = useState<QRCodeItem[]>([]);
const [activeTab, setActiveTab] = useState<'generate' | 'report'>('generate');
const pixels = SIZE_OPTIONS.find((s) => s.value === sizeOption)?.pixels ?? 120;
const handleGenerate = useCallback(() => {
const count = Math.min(Math.max(1, Math.floor(quantity) || 0), 500);
const newItems: QRCodeItem[] = [];
for (let i = 0; i < count; i++) {
const numericId = generateNumericId();
newItems.push({
id: `${PREFIX}-${numericId}`,
numericId,
value: numericId,
usageStatus: 'Unused',
createdAt: new Date().toISOString().slice(0, 10),
});
}
setItems((prev) => [...prev, ...newItems]);
}, [quantity]);
const [showPrintView, setShowPrintView] = useState(false);
const handleBulkExportPrint = useCallback(() => {
if (items.length === 0) return;
setShowPrintView(true);
}, [items.length]);
const handlePrint = useCallback(() => {
window.print();
}, []);
const handleClosePrintView = useCallback(() => {
setShowPrintView(false);
}, []);
const setUsageStatus = useCallback((id: string, status: QRUsageStatus) => {
setItems((prev) => prev.map((item) => (item.id === id ? { ...item, usageStatus: status } : item)));
}, []);
return (
<div className="h-full flex flex-col bg-white">
{/* Print-only view: full grid of QRs for batch printing */}
{showPrintView && (
<div className="fixed inset-0 z-50 bg-white p-6 overflow-auto print:block">
<div className="flex flex-wrap items-center justify-between gap-4 mb-4 no-print">
<h2 className="text-lg font-semibold">QR Codes – Batch Print</h2>
<div className="flex gap-2">
<Button onClick={handlePrint}>Print</Button>
<Button variant="outline" onClick={handleClosePrintView}>Close</Button>
</div>
</div>
<div className="grid grid-cols-4 gap-6 print:grid-cols-4" style={{ breakInside: 'avoid' }}>
{items.map((item) => (
<div key={item.id} className="text-center break-inside-avoid p-2 border border-gray-200 rounded">
<QRCodeSVG value={item.value} size={pixels} level="M" includeMargin={false} className="mx-auto" />
<div className="mt-2 text-sm font-semibold">ID: {item.numericId}</div>
</div>
))}
</div>
</div>
)}
{/* Tabs:与 Labels/Reports 一致 */}
<div className="flex overflow-x-auto border-b border-gray-200 bg-gray-50 print:hidden">
{(['generate', 'report'] as const).map((tab) => (
<button
key={tab}
type="button"
onClick={() => setActiveTab(tab)}
className={cn(
'px-6 py-3 text-sm font-medium whitespace-nowrap cursor-pointer transition-colors',
activeTab === tab ? 'border-b-2' : 'text-gray-500 hover:text-gray-700'
)}
style={activeTab === tab ? { color: '#1b46c7', borderBottomColor: '#1b46c7' } : undefined}
>
{tab === 'generate' ? 'Generate & Export' : 'QR Report'}
</button>
))}
</div>
{/* Content Area:上下左右间距与 Products/Locations 一致(由 Layout main p-8 控制) */}
<div className="flex-1 overflow-auto bg-gray-50 print:hidden">
{activeTab === 'generate' && (
<div>
{/* 1. Quantity input and size selection — 圆角、细边框 */}
<div className="flex flex-wrap items-end gap-4 mt-4 mb-6 p-4 bg-white border border-gray-200 rounded-lg shadow-sm">
<div className="grid gap-2">
<Label>Quantity</Label>
<Input
type="number"
min={1}
max={500}
value={quantity}
onChange={(e) => setQuantity(Number(e.target.value) || 0)}
className="w-32 rounded-lg border border-black h-9"
/>
</div>
<div className="grid gap-2">
<Label>QR Code Size</Label>
<Select value={sizeOption} onValueChange={(v) => setSizeOption(v as QRSize)}>
<SelectTrigger className="w-[180px] h-9 rounded-lg border border-black">
<SelectValue />
</SelectTrigger>
<SelectContent>
{SIZE_OPTIONS.map((opt) => (
<SelectItem key={opt.value} value={opt.value}>
{opt.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<Button onClick={handleGenerate} className="h-9 rounded-lg bg-green-600 hover:bg-green-700 border-0">
<Plus className="w-4 h-4 mr-1" />
Generate
</Button>
</div>
{/* 2 & 3. Generated QRs; 4. Bulk export */}
<div className="flex flex-wrap items-center justify-between gap-4 mb-4">
<span className="text-sm text-gray-600">
{items.length} QR code(s) generated. Each has a numeric ID for manual entry when scanning is not available.
</span>
{items.length > 0 && (
<Button variant="outline" className="h-9 rounded-lg border border-black text-black gap-2 bg-white hover:bg-gray-50" onClick={handleBulkExportPrint}>
<Download className="w-4 h-4" /> Bulk Export for Print
</Button>
)}
</div>
{items.length > 0 && (
<div className="grid grid-cols-2 sm:grid-cols-4 md:grid-cols-6 gap-4">
{items.map((item) => (
<div
key={item.id}
className="border rounded-lg p-3 bg-white shadow-sm text-center"
>
<QRCodeSVG value={item.value} size={pixels} level="M" includeMargin={false} className="mx-auto" />
<div className="mt-2 text-sm font-semibold text-gray-700">ID: {item.numericId}</div>
<div className="text-xs text-gray-500">{item.usageStatus}</div>
</div>
))}
</div>
)}
</div>
)}
{activeTab === 'report' && (
<div>
<p className="text-sm text-gray-600 mb-4">
QR code generation and usage status. Use this report to see which codes have been used (e.g. after recharge or scan).
</p>
<div className="bg-white border border-gray-200 shadow-sm rounded-sm overflow-hidden">
<Table>
<TableHeader>
<TableRow className="bg-gray-100 hover:bg-gray-100">
<TableHead className="font-bold text-gray-900 border-r">QR ID</TableHead>
<TableHead className="font-bold text-gray-900 border-r">Generation Status</TableHead>
<TableHead className="font-bold text-gray-900 border-r">Usage Status</TableHead>
<TableHead className="font-bold text-gray-900 text-center">Date Generated</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{items.length === 0 ? (
<TableRow>
<TableCell colSpan={4} className="text-center text-gray-500 py-8">
No QR codes generated yet. Go to Generate & Export and enter a quantity, then click Generate.
</TableCell>
</TableRow>
) : (
items.map((row) => (
<TableRow key={row.id} className="hover:bg-gray-50">
<TableCell className="font-mono border-r">{row.numericId}</TableCell>
<TableCell className="border-r">Generated</TableCell>
<TableCell className="border-r">
<span className={row.usageStatus === 'Used' ? 'text-green-600' : 'text-gray-600'}>
{row.usageStatus}
</span>
<Button
variant="ghost"
size="sm"
className="ml-2 h-7 text-xs rounded-lg"
onClick={() => setUsageStatus(row.id, row.usageStatus === 'Used' ? 'Unused' : 'Used')}
>
{row.usageStatus === 'Used' ? 'Mark Unused' : 'Mark Used'}
</Button>
</TableCell>
<TableCell>{row.createdAt}</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>
</div>
</div>
)}
</div>
</div>
);
}