@lodashventure/medusa-parcel-shipping 0.3.12 → 0.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2063 @@
1
+ "use strict";
2
+ const jsxRuntime = require("react/jsx-runtime");
3
+ const adminSdk = require("@medusajs/admin-sdk");
4
+ const ui = require("@medusajs/ui");
5
+ const react = require("react");
6
+ const lucideReact = require("lucide-react");
7
+ const icons = require("@medusajs/icons");
8
+ require("@medusajs/admin-shared");
9
+ const OrderShippingQuoteWidget = ({ data }) => {
10
+ const [quote, setQuote] = react.useState(null);
11
+ const [loading, setLoading] = react.useState(false);
12
+ const [error, setError] = react.useState(null);
13
+ const order = data;
14
+ const fetchShippingQuote = async () => {
15
+ setLoading(true);
16
+ setError(null);
17
+ try {
18
+ const items = order.items.map((item) => {
19
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
20
+ return {
21
+ sku: ((_a = item.variant) == null ? void 0 : _a.sku) || item.title,
22
+ width: ((_b = item.variant) == null ? void 0 : _b.width) || ((_d = (_c = item.variant) == null ? void 0 : _c.product) == null ? void 0 : _d.width) || 10,
23
+ length: ((_e = item.variant) == null ? void 0 : _e.length) || ((_g = (_f = item.variant) == null ? void 0 : _f.product) == null ? void 0 : _g.length) || 10,
24
+ height: ((_h = item.variant) == null ? void 0 : _h.height) || ((_j = (_i = item.variant) == null ? void 0 : _i.product) == null ? void 0 : _j.height) || 10,
25
+ weight: ((_k = item.variant) == null ? void 0 : _k.weight) || ((_m = (_l = item.variant) == null ? void 0 : _l.product) == null ? void 0 : _m.weight) || 0.1,
26
+ quantity: item.quantity,
27
+ attributes: {
28
+ fragile: ((_o = (_n = item.variant) == null ? void 0 : _n.metadata) == null ? void 0 : _o.fragile) || false,
29
+ noStack: ((_q = (_p = item.variant) == null ? void 0 : _p.metadata) == null ? void 0 : _q.noStack) || false,
30
+ stackable: (_s = (_r = item.variant) == null ? void 0 : _r.metadata) == null ? void 0 : _s.stackable
31
+ }
32
+ };
33
+ });
34
+ const shippingAddress = order.shipping_address;
35
+ const payload = {
36
+ items,
37
+ shipping_address: {
38
+ address_line_1: (shippingAddress == null ? void 0 : shippingAddress.address_1) || "",
39
+ address_line_2: (shippingAddress == null ? void 0 : shippingAddress.address_2) || "",
40
+ city: (shippingAddress == null ? void 0 : shippingAddress.city) || "",
41
+ province: (shippingAddress == null ? void 0 : shippingAddress.province) || "",
42
+ postcode: (shippingAddress == null ? void 0 : shippingAddress.postal_code) || "",
43
+ country: (shippingAddress == null ? void 0 : shippingAddress.country_code) || "TH"
44
+ },
45
+ allow_split_boxes: true
46
+ };
47
+ const response = await fetch("/store/quote", {
48
+ method: "POST",
49
+ headers: {
50
+ "Content-Type": "application/json",
51
+ "x-publishable-api-key": "pk_99d0d47048eaf52697f42d149a7f58c1661652292b9e1ea1445f519cd16dc071"
52
+ },
53
+ body: JSON.stringify(payload)
54
+ });
55
+ if (!response.ok) {
56
+ const errorData = await response.json();
57
+ throw new Error(errorData.message || "Failed to fetch quote");
58
+ }
59
+ const data2 = await response.json();
60
+ setQuote(data2);
61
+ } catch (err) {
62
+ setError(err.message);
63
+ } finally {
64
+ setLoading(false);
65
+ }
66
+ };
67
+ const getServiceName = (carrierType, serviceCode) => {
68
+ const serviceNames = {
69
+ COMPANY_FLEET_MESSENGER_3H: "ส่งด่วน 3 ชั่วโมง",
70
+ COMPANY_TRUCK_SAME_DAY: "ส่งด่วนภายในวัน",
71
+ PRIVATE_CARRIER_STANDARD_3_5D: "EMS ไปรษณีย์ (3-5 วัน)",
72
+ PRIVATE_CARRIER_EXPRESS_1_2D: "Express (1-2 วัน)"
73
+ };
74
+ return serviceNames[`${carrierType}_${serviceCode}`] || `${carrierType} - ${serviceCode}`;
75
+ };
76
+ const formatETA = (option) => {
77
+ if (option.eta_hours_min && option.eta_hours_max) {
78
+ return `${option.eta_hours_min}-${option.eta_hours_max} ชั่วโมง`;
79
+ }
80
+ if (option.eta_days_min && option.eta_days_max) {
81
+ return `${option.eta_days_min}-${option.eta_days_max} วัน`;
82
+ }
83
+ return "N/A";
84
+ };
85
+ return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "divide-y p-0", children: [
86
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between px-6 py-4", children: [
87
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
88
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Package, { className: "text-ui-fg-subtle" }),
89
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: "ตัวเลือกการจัดส่ง" })
90
+ ] }),
91
+ /* @__PURE__ */ jsxRuntime.jsx(
92
+ ui.Button,
93
+ {
94
+ size: "small",
95
+ onClick: fetchShippingQuote,
96
+ isLoading: loading,
97
+ disabled: !order.shipping_address,
98
+ children: "คำนวณค่าจัดส่ง"
99
+ }
100
+ )
101
+ ] }),
102
+ !order.shipping_address && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2 text-ui-fg-subtle", children: [
103
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.AlertCircle, {}),
104
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", children: "กรุณาเพิ่มที่อยู่จัดส่งก่อนคำนวณค่าจัดส่ง" })
105
+ ] }) }),
106
+ error && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2 text-red-500", children: [
107
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.AlertCircle, {}),
108
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", children: error })
109
+ ] }) }),
110
+ quote && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-6 py-4 space-y-4", children: [
111
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "rounded-lg border p-4", children: [
112
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2 mb-2", children: [
113
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Package, { className: "text-ui-fg-subtle", size: 20 }),
114
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { weight: "plus", size: "small", children: [
115
+ "กล่องที่แนะนำ: ",
116
+ quote.best_box.name
117
+ ] })
118
+ ] }),
119
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-2 text-sm text-ui-fg-subtle", children: [
120
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
121
+ "ขนาด: ",
122
+ quote.best_box.width_cm,
123
+ " × ",
124
+ quote.best_box.length_cm,
125
+ " ×",
126
+ " ",
127
+ quote.best_box.height_cm,
128
+ " cm"
129
+ ] }),
130
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
131
+ "น้ำหนักรวม: ",
132
+ quote.packing.totalWeightKg.toFixed(2),
133
+ " kg"
134
+ ] }),
135
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
136
+ "ความสูงที่ใช้: ",
137
+ quote.packing.usedHeight,
138
+ " cm"
139
+ ] }),
140
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
141
+ "ว่างเหลือ: ",
142
+ quote.packing.headroom,
143
+ " cm"
144
+ ] })
145
+ ] })
146
+ ] }),
147
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-3", children: [
148
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { weight: "plus", size: "small", children: "ตัวเลือกการจัดส่ง" }),
149
+ quote.shipping_options.map((option, index) => /* @__PURE__ */ jsxRuntime.jsxs(
150
+ "div",
151
+ {
152
+ className: `rounded-lg border p-4 ${!option.available ? "opacity-50 bg-ui-bg-subtle" : "bg-ui-bg-base"}`,
153
+ children: [
154
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-start justify-between mb-2", children: [
155
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
156
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Truck, { className: "text-ui-fg-subtle", size: 20 }),
157
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { weight: "plus", children: getServiceName(option.carrier_type, option.service_code) }),
158
+ option.available ? /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: "green", size: "small", children: "พร้อมใช้งาน" }) : /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: "red", size: "small", children: "ไม่พร้อมใช้งาน" })
159
+ ] }),
160
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { weight: "plus", size: "large", children: [
161
+ "฿",
162
+ option.price.toFixed(2)
163
+ ] })
164
+ ] }),
165
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "grid grid-cols-2 gap-2 text-sm text-ui-fg-subtle mb-2", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1", children: [
166
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Clock, { size: 16 }),
167
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
168
+ "เวลาจัดส่ง: ",
169
+ formatETA(option)
170
+ ] })
171
+ ] }) }),
172
+ !option.available && option.reason && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1 text-sm text-red-500 mb-2", children: [
173
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.AlertCircle, { size: 16 }),
174
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: option.reason })
175
+ ] }),
176
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mt-3 pt-3 border-t space-y-1 text-sm", children: [
177
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between text-ui-fg-subtle", children: [
178
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: "ค่ากล่อง:" }),
179
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
180
+ "฿",
181
+ option.price_breakdown.box_cost.toFixed(2)
182
+ ] })
183
+ ] }),
184
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between text-ui-fg-subtle", children: [
185
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: "ค่าวัสดุห่อหุ้ม:" }),
186
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
187
+ "฿",
188
+ option.price_breakdown.packaging_materials_cost.toFixed(
189
+ 2
190
+ )
191
+ ] })
192
+ ] }),
193
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between text-ui-fg-subtle", children: [
194
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: "ค่าจัดส่ง:" }),
195
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
196
+ "฿",
197
+ option.price_breakdown.shipping_cost.toFixed(2)
198
+ ] })
199
+ ] }),
200
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between font-semibold pt-1 border-t", children: [
201
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: "รวมทั้งหมด:" }),
202
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
203
+ "฿",
204
+ option.price_breakdown.total.toFixed(2)
205
+ ] })
206
+ ] })
207
+ ] })
208
+ ]
209
+ },
210
+ index
211
+ ))
212
+ ] })
213
+ ] })
214
+ ] });
215
+ };
216
+ adminSdk.defineWidgetConfig({
217
+ zone: "order.details.after"
218
+ });
219
+ const TH_PROVINCES = [
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
+ "เพชรบูรณ์",
260
+ "แพร่",
261
+ "ภูเก็ต",
262
+ "มหาสารคาม",
263
+ "มุกดาหาร",
264
+ "แม่ฮ่องสอน",
265
+ "ยโสธร",
266
+ "ยะลา",
267
+ "ร้อยเอ็ด",
268
+ "ระนอง",
269
+ "ระยอง",
270
+ "ราชบุรี",
271
+ "ลพบุรี",
272
+ "ลำปาง",
273
+ "ลำพูน",
274
+ "เลย",
275
+ "ศรีสะเกษ",
276
+ "สกลนคร",
277
+ "สงขลา",
278
+ "สตูล",
279
+ "สมุทรปราการ",
280
+ "สมุทรสงคราม",
281
+ "สมุทรสาคร",
282
+ "สระแก้ว",
283
+ "สระบุรี",
284
+ "สิงห์บุรี",
285
+ "สุโขทัย",
286
+ "สุพรรณบุรี",
287
+ "สุราษฎร์ธานี",
288
+ "สุรินทร์",
289
+ "หนองคาย",
290
+ "หนองบัวลำภู",
291
+ "อ่างทอง",
292
+ "อำนาจเจริญ",
293
+ "อุดรธานี",
294
+ "อุตรดิตถ์",
295
+ "อุทัยธานี",
296
+ "อุบลราชธานี"
297
+ ];
298
+ const ServiceAreaModal = ({ area, onClose }) => {
299
+ const [formData, setFormData] = react.useState({
300
+ kind: "PROVINCE",
301
+ value: "",
302
+ active: true
303
+ });
304
+ const [isSaving, setIsSaving] = react.useState(false);
305
+ react.useEffect(() => {
306
+ if (area) {
307
+ setFormData({
308
+ kind: area.kind,
309
+ value: area.value,
310
+ active: area.active
311
+ });
312
+ }
313
+ }, [area]);
314
+ const handleSubmit = async (e) => {
315
+ e.preventDefault();
316
+ const errors = [];
317
+ const trimmedValue = formData.value.trim();
318
+ if (!trimmedValue) {
319
+ errors.push("กรุณากรอกค่า");
320
+ }
321
+ if (formData.kind === "PROVINCE") {
322
+ if (!TH_PROVINCES.includes(trimmedValue)) {
323
+ errors.push("จังหวัดไม่ถูกต้อง กรุณาตรวจสอบการสะกดชื่อจังหวัด");
324
+ }
325
+ } else if (formData.kind === "POSTCODE_PREFIX") {
326
+ if (!/^\d{1,5}$/.test(trimmedValue)) {
327
+ errors.push("รหัสไปรษณีย์ต้องเป็นตัวเลข 1-5 หลัก");
328
+ }
329
+ }
330
+ if (errors.length > 0) {
331
+ ui.toast.error("ข้อมูลไม่ถูกต้อง", {
332
+ description: errors.join(", ")
333
+ });
334
+ return;
335
+ }
336
+ setIsSaving(true);
337
+ try {
338
+ const payload = {
339
+ kind: formData.kind,
340
+ value: trimmedValue,
341
+ active: formData.active
342
+ };
343
+ const url = area ? `/admin/service-areas/${area.id}` : "/admin/service-areas";
344
+ const method = area ? "PUT" : "POST";
345
+ const response = await fetch(url, {
346
+ method,
347
+ headers: { "Content-Type": "application/json" },
348
+ credentials: "include",
349
+ body: JSON.stringify(payload)
350
+ });
351
+ if (response.ok) {
352
+ ui.toast.success("สำเร็จ", {
353
+ description: area ? "แก้ไขพื้นที่บริการแล้ว" : "สร้างพื้นที่บริการใหม่แล้ว"
354
+ });
355
+ onClose();
356
+ } else {
357
+ const error = await response.json();
358
+ throw new Error(error.message || "Failed to save");
359
+ }
360
+ } catch (error) {
361
+ ui.toast.error("ข้อผิดพลาด", {
362
+ description: error instanceof Error ? error.message : "ไม่สามารถบันทึกข้อมูลได้"
363
+ });
364
+ } finally {
365
+ setIsSaving(false);
366
+ }
367
+ };
368
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Content, { children: [
369
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Title, { children: area ? "แก้ไขพื้นที่บริการ" : "สร้างพื้นที่บริการใหม่" }) }),
370
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, className: "flex flex-col gap-y-4", children: [
371
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
372
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "kind", children: "ประเภท *" }),
373
+ /* @__PURE__ */ jsxRuntime.jsxs(
374
+ ui.Select,
375
+ {
376
+ value: formData.kind,
377
+ onValueChange: (value) => setFormData({ ...formData, kind: value, value: "" }),
378
+ required: true,
379
+ children: [
380
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "เลือกประเภท" }) }),
381
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Select.Content, { children: [
382
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "PROVINCE", children: "จังหวัด" }),
383
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "POSTCODE_PREFIX", children: "รหัสไปรษณีย์" })
384
+ ] })
385
+ ]
386
+ }
387
+ )
388
+ ] }),
389
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
390
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "value", children: formData.kind === "PROVINCE" ? "ชื่อจังหวัด *" : "รหัสไปรษณีย์ (1-5 หลัก) *" }),
391
+ formData.kind === "PROVINCE" ? /* @__PURE__ */ jsxRuntime.jsxs(
392
+ ui.Select,
393
+ {
394
+ value: formData.value,
395
+ onValueChange: (value) => setFormData({ ...formData, value }),
396
+ required: true,
397
+ children: [
398
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "เลือกจังหวัด" }) }),
399
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: TH_PROVINCES.map((province) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: province, children: province }, province)) })
400
+ ]
401
+ }
402
+ ) : /* @__PURE__ */ jsxRuntime.jsx(
403
+ ui.Input,
404
+ {
405
+ id: "value",
406
+ type: "text",
407
+ value: formData.value,
408
+ onChange: (e) => setFormData({ ...formData, value: e.target.value }),
409
+ placeholder: "เช่น 10, 102, 10200",
410
+ maxLength: 5,
411
+ pattern: "\\d{1,5}",
412
+ required: true
413
+ }
414
+ ),
415
+ formData.kind === "POSTCODE_PREFIX" && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs text-ui-fg-subtle mt-1", children: 'ใส่รหัส 1-5 หลักเพื่อครอบคลุมหลายพื้นที่ เช่น "10" = กรุงเทพฯ ทั้งหมด' })
416
+ ] }),
417
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
418
+ /* @__PURE__ */ jsxRuntime.jsx(
419
+ ui.Switch,
420
+ {
421
+ id: "active",
422
+ checked: formData.active,
423
+ onCheckedChange: (checked) => setFormData({ ...formData, active: checked })
424
+ }
425
+ ),
426
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "active", children: "เปิดใช้งาน" })
427
+ ] }),
428
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Footer, { children: [
429
+ /* @__PURE__ */ jsxRuntime.jsx(
430
+ ui.Button,
431
+ {
432
+ type: "button",
433
+ variant: "secondary",
434
+ onClick: onClose,
435
+ disabled: isSaving,
436
+ children: "ยกเลิก"
437
+ }
438
+ ),
439
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", disabled: isSaving, children: isSaving ? "กำลังบันทึก..." : "บันทึก" })
440
+ ] })
441
+ ] }) })
442
+ ] }) });
443
+ };
444
+ const AREA_KINDS = [
445
+ { value: "ALL", label: "ทั้งหมด" },
446
+ { value: "PROVINCE", label: "จังหวัด" },
447
+ { value: "POSTCODE_PREFIX", label: "รหัสไปรษณีย์" }
448
+ ];
449
+ const ServiceAreasTable = () => {
450
+ const [areas, setAreas] = react.useState([]);
451
+ const [filteredAreas, setFilteredAreas] = react.useState([]);
452
+ const [searchTerm, setSearchTerm] = react.useState("");
453
+ const [kindFilter, setKindFilter] = react.useState("ALL");
454
+ const [isModalOpen, setIsModalOpen] = react.useState(false);
455
+ const [editingArea, setEditingArea] = react.useState(null);
456
+ const [isLoading, setIsLoading] = react.useState(false);
457
+ const [deleteAreaId, setDeleteAreaId] = react.useState(null);
458
+ const [deleteAreaValue, setDeleteAreaValue] = react.useState("");
459
+ react.useEffect(() => {
460
+ fetchAreas();
461
+ }, []);
462
+ react.useEffect(() => {
463
+ let filtered = areas;
464
+ if (kindFilter !== "ALL") {
465
+ filtered = filtered.filter((area) => area.kind === kindFilter);
466
+ }
467
+ if (searchTerm) {
468
+ filtered = filtered.filter(
469
+ (area) => area.value.toLowerCase().includes(searchTerm.toLowerCase())
470
+ );
471
+ }
472
+ setFilteredAreas(filtered);
473
+ }, [searchTerm, kindFilter, areas]);
474
+ const fetchAreas = async () => {
475
+ setIsLoading(true);
476
+ try {
477
+ const response = await fetch("/admin/service-areas", {
478
+ credentials: "include"
479
+ });
480
+ const data = await response.json();
481
+ console.log("Fetched service areas:", data);
482
+ setAreas(data.service_areas || []);
483
+ } catch (error) {
484
+ ui.toast.error("ข้อผิดพลาด", {
485
+ description: "ไม่สามารถโหลดข้อมูลพื้นที่บริการได้"
486
+ });
487
+ } finally {
488
+ setIsLoading(false);
489
+ }
490
+ };
491
+ const handleToggleActive = async (area) => {
492
+ try {
493
+ const response = await fetch(`/admin/service-areas/${area.id}`, {
494
+ method: "PUT",
495
+ headers: { "Content-Type": "application/json" },
496
+ credentials: "include",
497
+ body: JSON.stringify({ ...area, active: !area.active })
498
+ });
499
+ if (response.ok) {
500
+ ui.toast.success("สำเร็จ", {
501
+ description: `${area.active ? "ปิดใช้งาน" : "เปิดใช้งาน"}พื้นที่ ${area.value} แล้ว`
502
+ });
503
+ fetchAreas();
504
+ } else {
505
+ throw new Error("Failed to update");
506
+ }
507
+ } catch (error) {
508
+ ui.toast.error("ข้อผิดพลาด", {
509
+ description: "ไม่สามารถอัพเดทสถานะได้"
510
+ });
511
+ }
512
+ };
513
+ const handleDelete = async () => {
514
+ if (!deleteAreaId) return;
515
+ try {
516
+ const response = await fetch(`/admin/service-areas/${deleteAreaId}`, {
517
+ method: "DELETE",
518
+ credentials: "include"
519
+ });
520
+ if (response.ok) {
521
+ ui.toast.success("สำเร็จ", {
522
+ description: `ลบพื้นที่ ${deleteAreaValue} แล้ว`
523
+ });
524
+ fetchAreas();
525
+ } else {
526
+ throw new Error("Failed to delete");
527
+ }
528
+ } catch (error) {
529
+ ui.toast.error("ข้อผิดพลาด", {
530
+ description: "ไม่สามารถลบพื้นที่ได้"
531
+ });
532
+ } finally {
533
+ setDeleteAreaId(null);
534
+ setDeleteAreaValue("");
535
+ }
536
+ };
537
+ const handleEdit = (area) => {
538
+ setEditingArea(area);
539
+ setIsModalOpen(true);
540
+ };
541
+ const handleCreateNew = () => {
542
+ setEditingArea(null);
543
+ setIsModalOpen(true);
544
+ };
545
+ const handleModalClose = () => {
546
+ setIsModalOpen(false);
547
+ setEditingArea(null);
548
+ fetchAreas();
549
+ };
550
+ const openDeletePrompt = (id, value) => {
551
+ setDeleteAreaId(id);
552
+ setDeleteAreaValue(value);
553
+ };
554
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
555
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
556
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between gap-x-4", children: [
557
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-x-4 flex-1", children: [
558
+ /* @__PURE__ */ jsxRuntime.jsx(
559
+ ui.Input,
560
+ {
561
+ type: "search",
562
+ placeholder: "ค้นหาจังหวัดหรือรหัสไปรษณีย์...",
563
+ value: searchTerm,
564
+ onChange: (e) => setSearchTerm(e.target.value),
565
+ className: "max-w-md"
566
+ }
567
+ ),
568
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Select, { value: kindFilter, onValueChange: setKindFilter, children: [
569
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "ประเภท" }) }),
570
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: AREA_KINDS.map((kind) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: kind.value, children: kind.label }, kind.value)) })
571
+ ] })
572
+ ] }),
573
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Button, { onClick: handleCreateNew, children: [
574
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Plus, {}),
575
+ "สร้างพื้นที่ใหม่"
576
+ ] })
577
+ ] }),
578
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Table, { children: [
579
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Header, { children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
580
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ประเภท" }),
581
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ค่า" }),
582
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "สถานะ" }),
583
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { className: "text-right", children: "การจัดการ" })
584
+ ] }) }),
585
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Body, { children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "กำลังโหลด..." }) }) : filteredAreas.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "ไม่พบข้อมูลพื้นที่บริการ" }) }) : filteredAreas.map((area) => /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
586
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: area.kind === "PROVINCE" ? "blue" : "purple", children: area.kind === "PROVINCE" ? "จังหวัด" : "รหัสไปรษณีย์" }) }),
587
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: area.value }),
588
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
589
+ ui.Badge,
590
+ {
591
+ color: area.active ? "green" : "grey",
592
+ className: "cursor-pointer",
593
+ onClick: () => handleToggleActive(area),
594
+ children: area.active ? "เปิดใช้งาน" : "ปิดใช้งาน"
595
+ }
596
+ ) }),
597
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
598
+ /* @__PURE__ */ jsxRuntime.jsx(
599
+ ui.Button,
600
+ {
601
+ variant: "transparent",
602
+ size: "small",
603
+ onClick: () => handleEdit(area),
604
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Edit, {})
605
+ }
606
+ ),
607
+ /* @__PURE__ */ jsxRuntime.jsx(
608
+ ui.Button,
609
+ {
610
+ variant: "transparent",
611
+ size: "small",
612
+ onClick: () => openDeletePrompt(area.id, area.value),
613
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Trash, {})
614
+ }
615
+ )
616
+ ] }) })
617
+ ] }, area.id)) })
618
+ ] })
619
+ ] }),
620
+ isModalOpen && /* @__PURE__ */ jsxRuntime.jsx(ServiceAreaModal, { area: editingArea, onClose: handleModalClose }),
621
+ /* @__PURE__ */ jsxRuntime.jsx(
622
+ ui.Prompt,
623
+ {
624
+ variant: "confirmation",
625
+ open: !!deleteAreaId,
626
+ onOpenChange: () => {
627
+ setDeleteAreaId(null);
628
+ setDeleteAreaValue("");
629
+ },
630
+ children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Content, { children: [
631
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Header, { children: [
632
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Title, { children: "ลบพื้นที่บริการ" }),
633
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Description, { children: [
634
+ 'คุณต้องการลบพื้นที่ "',
635
+ deleteAreaValue,
636
+ '" ใช่หรือไม่? การดำเนินการนี้ไม่สามารถย้อนกลับได้'
637
+ ] })
638
+ ] }),
639
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Footer, { children: [
640
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Cancel, { children: "ยกเลิก" }),
641
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Action, { onClick: handleDelete, children: "ลบ" })
642
+ ] })
643
+ ] })
644
+ }
645
+ )
646
+ ] });
647
+ };
648
+ const AreasPage = () => {
649
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
650
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h1", children: "พื้นที่บริการ" }) }),
651
+ /* @__PURE__ */ jsxRuntime.jsx(ServiceAreasTable, {})
652
+ ] }) });
653
+ };
654
+ const config$3 = adminSdk.defineRouteConfig({
655
+ icon: icons.MapPin,
656
+ label: "พื้นที่บริการ"
657
+ });
658
+ const ParcelBoxModal = ({ box, onClose }) => {
659
+ const [formData, setFormData] = react.useState({
660
+ name: "",
661
+ width_cm: "",
662
+ length_cm: "",
663
+ height_cm: "",
664
+ max_weight_kg: "",
665
+ price_thb: "",
666
+ active: true
667
+ });
668
+ const [isSaving, setIsSaving] = react.useState(false);
669
+ react.useEffect(() => {
670
+ if (box) {
671
+ setFormData({
672
+ name: box.name,
673
+ width_cm: box.width_cm.toString(),
674
+ length_cm: box.length_cm.toString(),
675
+ height_cm: box.height_cm.toString(),
676
+ max_weight_kg: box.max_weight_kg.toString(),
677
+ price_thb: box.price_thb.toString(),
678
+ active: box.active
679
+ });
680
+ }
681
+ }, [box]);
682
+ const handleSubmit = async (e) => {
683
+ e.preventDefault();
684
+ const errors = [];
685
+ if (!formData.name.trim()) {
686
+ errors.push("กรุณากรอกชื่อกล่อง");
687
+ }
688
+ const width = parseFloat(formData.width_cm);
689
+ const length = parseFloat(formData.length_cm);
690
+ const height = parseFloat(formData.height_cm);
691
+ const maxWeight = parseFloat(formData.max_weight_kg);
692
+ const price = parseFloat(formData.price_thb);
693
+ if (isNaN(width) || width <= 0) {
694
+ errors.push("ความกว้างต้องเป็นตัวเลขมากกว่า 0");
695
+ }
696
+ if (isNaN(length) || length <= 0) {
697
+ errors.push("ความยาวต้องเป็นตัวเลขมากกว่า 0");
698
+ }
699
+ if (isNaN(height) || height <= 0) {
700
+ errors.push("ความสูงต้องเป็นตัวเลขมากกว่า 0");
701
+ }
702
+ if (isNaN(maxWeight) || maxWeight <= 0) {
703
+ errors.push("น้ำหนักสูงสุดต้องเป็นตัวเลขมากกว่า 0");
704
+ }
705
+ if (isNaN(price) || price < 0) {
706
+ errors.push("ราคาต้องเป็นตัวเลขไม่ติดลบ");
707
+ }
708
+ if (errors.length > 0) {
709
+ ui.toast.error("ข้อมูลไม่ถูกต้อง", {
710
+ description: errors.join(", ")
711
+ });
712
+ return;
713
+ }
714
+ setIsSaving(true);
715
+ try {
716
+ const payload = {
717
+ name: formData.name.trim(),
718
+ width_cm: width,
719
+ length_cm: length,
720
+ height_cm: height,
721
+ max_weight_kg: maxWeight,
722
+ price_thb: price,
723
+ active: formData.active
724
+ };
725
+ const url = box ? `/admin/boxes/${box.id}` : "/admin/boxes";
726
+ const method = box ? "PUT" : "POST";
727
+ const response = await fetch(url, {
728
+ method,
729
+ headers: { "Content-Type": "application/json" },
730
+ credentials: "include",
731
+ body: JSON.stringify(payload)
732
+ });
733
+ if (response.ok) {
734
+ ui.toast.success("สำเร็จ", {
735
+ description: box ? "แก้ไขกล่องพัสดุแล้ว" : "สร้างกล่องพัสดุใหม่แล้ว"
736
+ });
737
+ onClose();
738
+ } else {
739
+ const error = await response.json();
740
+ throw new Error(error.message || "Failed to save");
741
+ }
742
+ } catch (error) {
743
+ ui.toast.error("ข้อผิดพลาด", {
744
+ description: error instanceof Error ? error.message : "ไม่สามารถบันทึกข้อมูลได้"
745
+ });
746
+ } finally {
747
+ setIsSaving(false);
748
+ }
749
+ };
750
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Content, { children: [
751
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Title, { children: box ? "แก้ไขกล่องพัสดุ" : "สร้างกล่องพัสดุใหม่" }) }),
752
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, className: "flex flex-col gap-y-4", children: [
753
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
754
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "name", children: "ชื่อกล่อง *" }),
755
+ /* @__PURE__ */ jsxRuntime.jsx(
756
+ ui.Input,
757
+ {
758
+ id: "name",
759
+ value: formData.name,
760
+ onChange: (e) => setFormData({ ...formData, name: e.target.value }),
761
+ placeholder: "เช่น กล่อง S, กล่อง M",
762
+ required: true
763
+ }
764
+ )
765
+ ] }),
766
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-3 gap-x-4", children: [
767
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
768
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "width", children: "ความกว้าง (cm) *" }),
769
+ /* @__PURE__ */ jsxRuntime.jsx(
770
+ ui.Input,
771
+ {
772
+ id: "width",
773
+ type: "number",
774
+ step: "0.1",
775
+ value: formData.width_cm,
776
+ onChange: (e) => setFormData({ ...formData, width_cm: e.target.value }),
777
+ placeholder: "20",
778
+ required: true
779
+ }
780
+ )
781
+ ] }),
782
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
783
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "length", children: "ความยาว (cm) *" }),
784
+ /* @__PURE__ */ jsxRuntime.jsx(
785
+ ui.Input,
786
+ {
787
+ id: "length",
788
+ type: "number",
789
+ step: "0.1",
790
+ value: formData.length_cm,
791
+ onChange: (e) => setFormData({ ...formData, length_cm: e.target.value }),
792
+ placeholder: "30",
793
+ required: true
794
+ }
795
+ )
796
+ ] }),
797
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
798
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "height", children: "ความสูง (cm) *" }),
799
+ /* @__PURE__ */ jsxRuntime.jsx(
800
+ ui.Input,
801
+ {
802
+ id: "height",
803
+ type: "number",
804
+ step: "0.1",
805
+ value: formData.height_cm,
806
+ onChange: (e) => setFormData({ ...formData, height_cm: e.target.value }),
807
+ placeholder: "15",
808
+ required: true
809
+ }
810
+ )
811
+ ] })
812
+ ] }),
813
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
814
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "maxWeight", children: "น้ำหนักสูงสุด (kg) *" }),
815
+ /* @__PURE__ */ jsxRuntime.jsx(
816
+ ui.Input,
817
+ {
818
+ id: "maxWeight",
819
+ type: "number",
820
+ step: "0.1",
821
+ value: formData.max_weight_kg,
822
+ onChange: (e) => setFormData({ ...formData, max_weight_kg: e.target.value }),
823
+ placeholder: "0.5",
824
+ required: true
825
+ }
826
+ )
827
+ ] }),
828
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
829
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "price", children: "ราคา (THB) *" }),
830
+ /* @__PURE__ */ jsxRuntime.jsx(
831
+ ui.Input,
832
+ {
833
+ id: "price",
834
+ type: "number",
835
+ step: "0.01",
836
+ value: formData.price_thb,
837
+ onChange: (e) => setFormData({ ...formData, price_thb: e.target.value }),
838
+ placeholder: "10.00",
839
+ required: true
840
+ }
841
+ )
842
+ ] }),
843
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
844
+ /* @__PURE__ */ jsxRuntime.jsx(
845
+ ui.Switch,
846
+ {
847
+ id: "active",
848
+ checked: formData.active,
849
+ onCheckedChange: (checked) => setFormData({ ...formData, active: checked })
850
+ }
851
+ ),
852
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "active", children: "เปิดใช้งาน" })
853
+ ] }),
854
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Footer, { children: [
855
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "button", variant: "secondary", onClick: onClose, disabled: isSaving, children: "ยกเลิก" }),
856
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", disabled: isSaving, children: isSaving ? "กำลังบันทึก..." : "บันทึก" })
857
+ ] })
858
+ ] }) })
859
+ ] }) });
860
+ };
861
+ const ParcelBoxesTable = () => {
862
+ const [boxes, setBoxes] = react.useState([]);
863
+ const [filteredBoxes, setFilteredBoxes] = react.useState([]);
864
+ const [searchTerm, setSearchTerm] = react.useState("");
865
+ const [isModalOpen, setIsModalOpen] = react.useState(false);
866
+ const [editingBox, setEditingBox] = react.useState(null);
867
+ const [isLoading, setIsLoading] = react.useState(false);
868
+ const [deleteBoxId, setDeleteBoxId] = react.useState(null);
869
+ const [deleteBoxName, setDeleteBoxName] = react.useState("");
870
+ react.useEffect(() => {
871
+ fetchBoxes();
872
+ }, []);
873
+ react.useEffect(() => {
874
+ const filtered = boxes.filter(
875
+ (box) => box.name.toLowerCase().includes(searchTerm.toLowerCase())
876
+ );
877
+ setFilteredBoxes(filtered);
878
+ }, [searchTerm, boxes]);
879
+ const fetchBoxes = async () => {
880
+ setIsLoading(true);
881
+ try {
882
+ const response = await fetch("/admin/boxes", {
883
+ credentials: "include"
884
+ });
885
+ const data = await response.json();
886
+ setBoxes(data.boxes || []);
887
+ } catch (error) {
888
+ ui.toast.error("ข้อผิดพลาด", {
889
+ description: "ไม่สามารถโหลดข้อมูลกล่องพัสดุได้"
890
+ });
891
+ } finally {
892
+ setIsLoading(false);
893
+ }
894
+ };
895
+ const handleToggleActive = async (box) => {
896
+ try {
897
+ const response = await fetch(`/admin/boxes/${box.id}`, {
898
+ method: "PUT",
899
+ headers: { "Content-Type": "application/json" },
900
+ credentials: "include",
901
+ body: JSON.stringify({ ...box, active: !box.active })
902
+ });
903
+ if (response.ok) {
904
+ ui.toast.success("สำเร็จ", {
905
+ description: `${box.active ? "ปิดใช้งาน" : "เปิดใช้งาน"}กล่อง ${box.name} แล้ว`
906
+ });
907
+ fetchBoxes();
908
+ } else {
909
+ throw new Error("Failed to update");
910
+ }
911
+ } catch (error) {
912
+ ui.toast.error("ข้อผิดพลาด", {
913
+ description: "ไม่สามารถอัพเดทสถานะได้"
914
+ });
915
+ }
916
+ };
917
+ const handleDelete = async () => {
918
+ if (!deleteBoxId) return;
919
+ try {
920
+ const response = await fetch(`/admin/boxes/${deleteBoxId}`, {
921
+ method: "DELETE",
922
+ credentials: "include"
923
+ });
924
+ if (response.ok) {
925
+ ui.toast.success("สำเร็จ", {
926
+ description: `ลบกล่อง ${deleteBoxName} แล้ว`
927
+ });
928
+ fetchBoxes();
929
+ } else {
930
+ throw new Error("Failed to delete");
931
+ }
932
+ } catch (error) {
933
+ ui.toast.error("ข้อผิดพลาด", {
934
+ description: "ไม่สามารถลบกล่องได้"
935
+ });
936
+ } finally {
937
+ setDeleteBoxId(null);
938
+ setDeleteBoxName("");
939
+ }
940
+ };
941
+ const handleEdit = (box) => {
942
+ setEditingBox(box);
943
+ setIsModalOpen(true);
944
+ };
945
+ const handleCreateNew = () => {
946
+ setEditingBox(null);
947
+ setIsModalOpen(true);
948
+ };
949
+ const handleModalClose = () => {
950
+ setIsModalOpen(false);
951
+ setEditingBox(null);
952
+ fetchBoxes();
953
+ };
954
+ const openDeletePrompt = (id, name) => {
955
+ setDeleteBoxId(id);
956
+ setDeleteBoxName(name);
957
+ };
958
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
959
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
960
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between gap-x-4", children: [
961
+ /* @__PURE__ */ jsxRuntime.jsx(
962
+ ui.Input,
963
+ {
964
+ type: "search",
965
+ placeholder: "ค้นหาชื่อกล่อง...",
966
+ value: searchTerm,
967
+ onChange: (e) => setSearchTerm(e.target.value),
968
+ className: "max-w-md"
969
+ }
970
+ ),
971
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Button, { onClick: handleCreateNew, children: [
972
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Plus, {}),
973
+ "สร้างกล่องใหม่"
974
+ ] })
975
+ ] }),
976
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Table, { children: [
977
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Header, { children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
978
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ชื่อ" }),
979
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ขนาด (กว้าง×ยาว×สูง cm)" }),
980
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "น้ำหนักสูงสุด (kg)" }),
981
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ราคา (THB)" }),
982
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "สถานะ" }),
983
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { className: "text-right", children: "การจัดการ" })
984
+ ] }) }),
985
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Body, { children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "กำลังโหลด..." }) }) : filteredBoxes.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "ไม่พบข้อมูลกล่องพัสดุ" }) }) : filteredBoxes.map((box) => /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
986
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: box.name }),
987
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Cell, { children: [
988
+ box.width_cm,
989
+ " × ",
990
+ box.length_cm,
991
+ " × ",
992
+ box.height_cm
993
+ ] }),
994
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: box.max_weight_kg }),
995
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: box.price_thb.toFixed(2) }),
996
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
997
+ ui.Badge,
998
+ {
999
+ color: box.active ? "green" : "grey",
1000
+ className: "cursor-pointer",
1001
+ onClick: () => handleToggleActive(box),
1002
+ children: box.active ? "เปิดใช้งาน" : "ปิดใช้งาน"
1003
+ }
1004
+ ) }),
1005
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
1006
+ /* @__PURE__ */ jsxRuntime.jsx(
1007
+ ui.Button,
1008
+ {
1009
+ variant: "transparent",
1010
+ size: "small",
1011
+ onClick: () => handleEdit(box),
1012
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Edit, {})
1013
+ }
1014
+ ),
1015
+ /* @__PURE__ */ jsxRuntime.jsx(
1016
+ ui.Button,
1017
+ {
1018
+ variant: "transparent",
1019
+ size: "small",
1020
+ onClick: () => openDeletePrompt(box.id, box.name),
1021
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Trash, {})
1022
+ }
1023
+ )
1024
+ ] }) })
1025
+ ] }, box.id)) })
1026
+ ] })
1027
+ ] }),
1028
+ isModalOpen && /* @__PURE__ */ jsxRuntime.jsx(ParcelBoxModal, { box: editingBox, onClose: handleModalClose }),
1029
+ /* @__PURE__ */ jsxRuntime.jsx(
1030
+ ui.Prompt,
1031
+ {
1032
+ variant: "confirmation",
1033
+ open: !!deleteBoxId,
1034
+ onOpenChange: () => {
1035
+ setDeleteBoxId(null);
1036
+ setDeleteBoxName("");
1037
+ },
1038
+ children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Content, { children: [
1039
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Header, { children: [
1040
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Title, { children: "ลบกล่องพัสดุ" }),
1041
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Description, { children: [
1042
+ 'คุณต้องการลบกล่อง "',
1043
+ deleteBoxName,
1044
+ '" ใช่หรือไม่? การดำเนินการนี้ไม่สามารถย้อนกลับได้'
1045
+ ] })
1046
+ ] }),
1047
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Footer, { children: [
1048
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Cancel, { children: "ยกเลิก" }),
1049
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Action, { onClick: handleDelete, children: "ลบ" })
1050
+ ] })
1051
+ ] })
1052
+ }
1053
+ )
1054
+ ] });
1055
+ };
1056
+ const BoxesPage = () => {
1057
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
1058
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h1", children: "กล่องพัสดุ" }) }),
1059
+ /* @__PURE__ */ jsxRuntime.jsx(ParcelBoxesTable, {})
1060
+ ] }) });
1061
+ };
1062
+ const config$2 = adminSdk.defineRouteConfig({
1063
+ icon: icons.ArchiveBox,
1064
+ label: "กล่องพัสดุ"
1065
+ });
1066
+ const CATEGORIES$1 = [
1067
+ { value: "PACKAGING", label: "วัสดุหีบห่อ" },
1068
+ { value: "PROTECTION", label: "วัสดุป้องกัน" },
1069
+ { value: "FILLING", label: "วัสดุเติมช่องว่าง" },
1070
+ { value: "TAPE", label: "เทป/กาว" },
1071
+ { value: "LABEL", label: "ฉลาก/สติกเกอร์" },
1072
+ { value: "OTHER", label: "อื่นๆ" }
1073
+ ];
1074
+ const MaterialCostModal = ({
1075
+ materialCost,
1076
+ onClose
1077
+ }) => {
1078
+ const [formData, setFormData] = react.useState({
1079
+ name: "",
1080
+ unit: "",
1081
+ cost_per_unit: "",
1082
+ currency: "THB",
1083
+ category: "",
1084
+ description: "",
1085
+ active: true
1086
+ });
1087
+ const [isSaving, setIsSaving] = react.useState(false);
1088
+ react.useEffect(() => {
1089
+ if (materialCost) {
1090
+ setFormData({
1091
+ name: materialCost.name,
1092
+ unit: materialCost.unit,
1093
+ cost_per_unit: materialCost.cost_per_unit.toString(),
1094
+ currency: materialCost.currency,
1095
+ category: materialCost.category || "",
1096
+ description: materialCost.description || "",
1097
+ active: materialCost.active
1098
+ });
1099
+ }
1100
+ }, [materialCost]);
1101
+ const handleSubmit = async (e) => {
1102
+ e.preventDefault();
1103
+ const errors = [];
1104
+ if (!formData.name.trim()) {
1105
+ errors.push("กรุณากรอกชื่อวัสดุ");
1106
+ }
1107
+ if (!formData.unit.trim()) {
1108
+ errors.push("กรุณากรอกหน่วย");
1109
+ }
1110
+ const costPerUnit = parseFloat(formData.cost_per_unit);
1111
+ if (isNaN(costPerUnit) || costPerUnit < 0) {
1112
+ errors.push("ต้นทุนต่อหน่วยต้องเป็นตัวเลขไม่ติดลบ");
1113
+ }
1114
+ if (errors.length > 0) {
1115
+ ui.toast.error("ข้อมูลไม่ถูกต้อง", {
1116
+ description: errors.join(", ")
1117
+ });
1118
+ return;
1119
+ }
1120
+ setIsSaving(true);
1121
+ try {
1122
+ const payload = {
1123
+ name: formData.name.trim(),
1124
+ unit: formData.unit.trim(),
1125
+ cost_per_unit: costPerUnit,
1126
+ currency: formData.currency,
1127
+ category: formData.category || void 0,
1128
+ description: formData.description.trim() || void 0,
1129
+ active: formData.active
1130
+ };
1131
+ const url = materialCost ? `/admin/material-costs/${materialCost.id}` : "/admin/material-costs";
1132
+ const method = materialCost ? "PUT" : "POST";
1133
+ const response = await fetch(url, {
1134
+ method,
1135
+ headers: { "Content-Type": "application/json" },
1136
+ credentials: "include",
1137
+ body: JSON.stringify(payload)
1138
+ });
1139
+ if (response.ok) {
1140
+ ui.toast.success("สำเร็จ", {
1141
+ description: materialCost ? "แก้ไขรายการต้นทุนวัสดุแล้ว" : "สร้างรายการต้นทุนวัสดุใหม่แล้ว"
1142
+ });
1143
+ onClose();
1144
+ } else {
1145
+ const error = await response.json();
1146
+ throw new Error(error.message || "Failed to save");
1147
+ }
1148
+ } catch (error) {
1149
+ ui.toast.error("ข้อผิดพลาด", {
1150
+ description: error instanceof Error ? error.message : "ไม่สามารถบันทึกข้อมูลได้"
1151
+ });
1152
+ } finally {
1153
+ setIsSaving(false);
1154
+ }
1155
+ };
1156
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Content, { children: [
1157
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Title, { children: materialCost ? "แก้ไขรายการต้นทุนวัสดุ" : "สร้างรายการต้นทุนวัสดุใหม่" }) }),
1158
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, className: "flex flex-col gap-y-4", children: [
1159
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1160
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "name", children: "ชื่อวัสดุ *" }),
1161
+ /* @__PURE__ */ jsxRuntime.jsx(
1162
+ ui.Input,
1163
+ {
1164
+ id: "name",
1165
+ value: formData.name,
1166
+ onChange: (e) => setFormData({ ...formData, name: e.target.value }),
1167
+ placeholder: "เช่น พลาสติกกันกระแทก, กล่องกระดาษ",
1168
+ required: true
1169
+ }
1170
+ )
1171
+ ] }),
1172
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1173
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "category", children: "หมวดหมู่ (ไม่บังคับ)" }),
1174
+ /* @__PURE__ */ jsxRuntime.jsxs(
1175
+ ui.Select,
1176
+ {
1177
+ value: formData.category || void 0,
1178
+ onValueChange: (value) => setFormData({ ...formData, category: value }),
1179
+ children: [
1180
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "เลือกหมวดหมู่ (ไม่บังคับ)" }) }),
1181
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: CATEGORIES$1.map((category) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: category.value, children: category.label }, category.value)) })
1182
+ ]
1183
+ }
1184
+ )
1185
+ ] }),
1186
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-4", children: [
1187
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1188
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "unit", children: "หน่วย *" }),
1189
+ /* @__PURE__ */ jsxRuntime.jsx(
1190
+ ui.Input,
1191
+ {
1192
+ id: "unit",
1193
+ value: formData.unit,
1194
+ onChange: (e) => setFormData({ ...formData, unit: e.target.value }),
1195
+ placeholder: "เช่น ชิ้น, เมตร, กิโลกรัม",
1196
+ required: true
1197
+ }
1198
+ )
1199
+ ] }),
1200
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1201
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "costPerUnit", children: "ต้นทุนต่อหน่วย *" }),
1202
+ /* @__PURE__ */ jsxRuntime.jsx(
1203
+ ui.Input,
1204
+ {
1205
+ id: "costPerUnit",
1206
+ type: "number",
1207
+ step: "0.01",
1208
+ value: formData.cost_per_unit,
1209
+ onChange: (e) => setFormData({ ...formData, cost_per_unit: e.target.value }),
1210
+ placeholder: "0.00",
1211
+ required: true
1212
+ }
1213
+ )
1214
+ ] })
1215
+ ] }),
1216
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1217
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "currency", children: "สกุลเงิน" }),
1218
+ /* @__PURE__ */ jsxRuntime.jsx(
1219
+ ui.Input,
1220
+ {
1221
+ id: "currency",
1222
+ value: formData.currency,
1223
+ onChange: (e) => setFormData({ ...formData, currency: e.target.value }),
1224
+ placeholder: "THB"
1225
+ }
1226
+ )
1227
+ ] }),
1228
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1229
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "description", children: "คำอธิบาย" }),
1230
+ /* @__PURE__ */ jsxRuntime.jsx(
1231
+ ui.Textarea,
1232
+ {
1233
+ id: "description",
1234
+ value: formData.description,
1235
+ onChange: (e) => setFormData({ ...formData, description: e.target.value }),
1236
+ placeholder: "คำอธิบายเพิ่มเติมเกี่ยวกับวัสดุนี้...",
1237
+ rows: 3
1238
+ }
1239
+ )
1240
+ ] }),
1241
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
1242
+ /* @__PURE__ */ jsxRuntime.jsx(
1243
+ ui.Switch,
1244
+ {
1245
+ id: "active",
1246
+ checked: formData.active,
1247
+ onCheckedChange: (checked) => setFormData({ ...formData, active: checked })
1248
+ }
1249
+ ),
1250
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "active", children: "เปิดใช้งาน" })
1251
+ ] }),
1252
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Footer, { children: [
1253
+ /* @__PURE__ */ jsxRuntime.jsx(
1254
+ ui.Button,
1255
+ {
1256
+ type: "button",
1257
+ variant: "secondary",
1258
+ onClick: onClose,
1259
+ disabled: isSaving,
1260
+ children: "ยกเลิก"
1261
+ }
1262
+ ),
1263
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", disabled: isSaving, children: isSaving ? "กำลังบันทึก..." : "บันทึก" })
1264
+ ] })
1265
+ ] }) })
1266
+ ] }) });
1267
+ };
1268
+ const CATEGORIES = [
1269
+ { value: "ALL", label: "ทั้งหมด" },
1270
+ { value: "PACKAGING", label: "วัสดุหีบห่อ" },
1271
+ { value: "PROTECTION", label: "วัสดุป้องกัน" },
1272
+ { value: "FILLING", label: "วัสดุเติมช่องว่าง" },
1273
+ { value: "TAPE", label: "เทป/กาว" },
1274
+ { value: "LABEL", label: "ฉลาก/สติกเกอร์" },
1275
+ { value: "OTHER", label: "อื่นๆ" }
1276
+ ];
1277
+ const MaterialCostsTable = () => {
1278
+ const [materialCosts, setMaterialCosts] = react.useState([]);
1279
+ const [filteredMaterialCosts, setFilteredMaterialCosts] = react.useState([]);
1280
+ const [searchTerm, setSearchTerm] = react.useState("");
1281
+ const [categoryFilter, setCategoryFilter] = react.useState("ALL");
1282
+ const [isModalOpen, setIsModalOpen] = react.useState(false);
1283
+ const [editingMaterialCost, setEditingMaterialCost] = react.useState(null);
1284
+ const [isLoading, setIsLoading] = react.useState(false);
1285
+ const [deleteMaterialCostId, setDeleteMaterialCostId] = react.useState(null);
1286
+ const [deleteMaterialCostName, setDeleteMaterialCostName] = react.useState("");
1287
+ react.useEffect(() => {
1288
+ fetchMaterialCosts();
1289
+ }, []);
1290
+ react.useEffect(() => {
1291
+ let filtered = materialCosts;
1292
+ if (categoryFilter !== "ALL") {
1293
+ filtered = filtered.filter((mc) => mc.category === categoryFilter);
1294
+ }
1295
+ if (searchTerm) {
1296
+ filtered = filtered.filter(
1297
+ (mc) => mc.name.toLowerCase().includes(searchTerm.toLowerCase())
1298
+ );
1299
+ }
1300
+ setFilteredMaterialCosts(filtered);
1301
+ }, [searchTerm, categoryFilter, materialCosts]);
1302
+ const fetchMaterialCosts = async () => {
1303
+ setIsLoading(true);
1304
+ try {
1305
+ const response = await fetch("/admin/material-costs", {
1306
+ credentials: "include"
1307
+ });
1308
+ const data = await response.json();
1309
+ setMaterialCosts(data.material_costs || []);
1310
+ } catch (error) {
1311
+ ui.toast.error("ข้อผิดพลาด", {
1312
+ description: "ไม่สามารถโหลดข้อมูลต้นทุนวัสดุได้"
1313
+ });
1314
+ } finally {
1315
+ setIsLoading(false);
1316
+ }
1317
+ };
1318
+ const handleToggleActive = async (materialCost) => {
1319
+ try {
1320
+ const response = await fetch(`/admin/material-costs/${materialCost.id}`, {
1321
+ method: "PUT",
1322
+ headers: { "Content-Type": "application/json" },
1323
+ credentials: "include",
1324
+ body: JSON.stringify({ ...materialCost, active: !materialCost.active })
1325
+ });
1326
+ if (response.ok) {
1327
+ ui.toast.success("สำเร็จ", {
1328
+ description: `${materialCost.active ? "ปิดใช้งาน" : "เปิดใช้งาน"}วัสดุ ${materialCost.name} แล้ว`
1329
+ });
1330
+ fetchMaterialCosts();
1331
+ } else {
1332
+ throw new Error("Failed to update");
1333
+ }
1334
+ } catch (error) {
1335
+ ui.toast.error("ข้อผิดพลาด", {
1336
+ description: "ไม่สามารถอัพเดทสถานะได้"
1337
+ });
1338
+ }
1339
+ };
1340
+ const handleDelete = async () => {
1341
+ if (!deleteMaterialCostId) return;
1342
+ try {
1343
+ const response = await fetch(
1344
+ `/admin/material-costs/${deleteMaterialCostId}`,
1345
+ {
1346
+ method: "DELETE",
1347
+ credentials: "include"
1348
+ }
1349
+ );
1350
+ if (response.ok) {
1351
+ ui.toast.success("สำเร็จ", {
1352
+ description: `ลบวัสดุ ${deleteMaterialCostName} แล้ว`
1353
+ });
1354
+ fetchMaterialCosts();
1355
+ } else {
1356
+ throw new Error("Failed to delete");
1357
+ }
1358
+ } catch (error) {
1359
+ ui.toast.error("ข้อผิดพลาด", {
1360
+ description: "ไม่สามารถลบวัสดุได้"
1361
+ });
1362
+ } finally {
1363
+ setDeleteMaterialCostId(null);
1364
+ setDeleteMaterialCostName("");
1365
+ }
1366
+ };
1367
+ const handleEdit = (materialCost) => {
1368
+ setEditingMaterialCost(materialCost);
1369
+ setIsModalOpen(true);
1370
+ };
1371
+ const handleCreateNew = () => {
1372
+ setEditingMaterialCost(null);
1373
+ setIsModalOpen(true);
1374
+ };
1375
+ const handleModalClose = () => {
1376
+ setIsModalOpen(false);
1377
+ setEditingMaterialCost(null);
1378
+ fetchMaterialCosts();
1379
+ };
1380
+ const openDeletePrompt = (id, name) => {
1381
+ setDeleteMaterialCostId(id);
1382
+ setDeleteMaterialCostName(name);
1383
+ };
1384
+ const getCategoryLabel = (category) => {
1385
+ var _a;
1386
+ if (!category) return "-";
1387
+ return ((_a = CATEGORIES.find((c) => c.value === category)) == null ? void 0 : _a.label) || category;
1388
+ };
1389
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1390
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
1391
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between gap-x-4", children: [
1392
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-x-4 flex-1", children: [
1393
+ /* @__PURE__ */ jsxRuntime.jsx(
1394
+ ui.Input,
1395
+ {
1396
+ type: "search",
1397
+ placeholder: "ค้นหาชื่อวัสดุ...",
1398
+ value: searchTerm,
1399
+ onChange: (e) => setSearchTerm(e.target.value),
1400
+ className: "max-w-md"
1401
+ }
1402
+ ),
1403
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Select, { value: categoryFilter, onValueChange: setCategoryFilter, children: [
1404
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "หมวดหมู่" }) }),
1405
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: CATEGORIES.map((category) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: category.value, children: category.label }, category.value)) })
1406
+ ] })
1407
+ ] }),
1408
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Button, { onClick: handleCreateNew, children: [
1409
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Plus, {}),
1410
+ "สร้างรายการใหม่"
1411
+ ] })
1412
+ ] }),
1413
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Table, { children: [
1414
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Header, { children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
1415
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ชื่อ" }),
1416
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "หมวดหมู่" }),
1417
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "หน่วย" }),
1418
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ต้นทุนต่อหน่วย" }),
1419
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "คำอธิบาย" }),
1420
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "สถานะ" }),
1421
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { className: "text-right", children: "การจัดการ" })
1422
+ ] }) }),
1423
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Body, { children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "กำลังโหลด..." }) }) : filteredMaterialCosts.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "ไม่พบข้อมูลต้นทุนวัสดุ" }) }) : filteredMaterialCosts.map((materialCost) => /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
1424
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: materialCost.name }),
1425
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: "blue", children: getCategoryLabel(materialCost.category) }) }),
1426
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: materialCost.unit }),
1427
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Cell, { children: [
1428
+ materialCost.cost_per_unit.toFixed(2),
1429
+ " ",
1430
+ materialCost.currency
1431
+ ] }),
1432
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "max-w-xs truncate", children: materialCost.description || "-" }),
1433
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
1434
+ ui.Badge,
1435
+ {
1436
+ color: materialCost.active ? "green" : "grey",
1437
+ className: "cursor-pointer",
1438
+ onClick: () => handleToggleActive(materialCost),
1439
+ children: materialCost.active ? "เปิดใช้งาน" : "ปิดใช้งาน"
1440
+ }
1441
+ ) }),
1442
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
1443
+ /* @__PURE__ */ jsxRuntime.jsx(
1444
+ ui.Button,
1445
+ {
1446
+ variant: "transparent",
1447
+ size: "small",
1448
+ onClick: () => handleEdit(materialCost),
1449
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Edit, {})
1450
+ }
1451
+ ),
1452
+ /* @__PURE__ */ jsxRuntime.jsx(
1453
+ ui.Button,
1454
+ {
1455
+ variant: "transparent",
1456
+ size: "small",
1457
+ onClick: () => openDeletePrompt(materialCost.id, materialCost.name),
1458
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Trash, {})
1459
+ }
1460
+ )
1461
+ ] }) })
1462
+ ] }, materialCost.id)) })
1463
+ ] })
1464
+ ] }),
1465
+ isModalOpen && /* @__PURE__ */ jsxRuntime.jsx(
1466
+ MaterialCostModal,
1467
+ {
1468
+ materialCost: editingMaterialCost,
1469
+ onClose: handleModalClose
1470
+ }
1471
+ ),
1472
+ /* @__PURE__ */ jsxRuntime.jsx(
1473
+ ui.Prompt,
1474
+ {
1475
+ variant: "confirmation",
1476
+ open: !!deleteMaterialCostId,
1477
+ onOpenChange: () => {
1478
+ setDeleteMaterialCostId(null);
1479
+ setDeleteMaterialCostName("");
1480
+ },
1481
+ children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Content, { children: [
1482
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Header, { children: [
1483
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Title, { children: "ลบรายการต้นทุนวัสดุ" }),
1484
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Description, { children: [
1485
+ 'คุณต้องการลบวัสดุ "',
1486
+ deleteMaterialCostName,
1487
+ '" ใช่หรือไม่? การดำเนินการนี้ไม่สามารถย้อนกลับได้'
1488
+ ] })
1489
+ ] }),
1490
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Footer, { children: [
1491
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Cancel, { children: "ยกเลิก" }),
1492
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Action, { onClick: handleDelete, children: "ลบ" })
1493
+ ] })
1494
+ ] })
1495
+ }
1496
+ )
1497
+ ] });
1498
+ };
1499
+ const MaterialCostsPage = () => {
1500
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
1501
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h1", children: "ต้นทุนวัสดุ" }) }),
1502
+ /* @__PURE__ */ jsxRuntime.jsx(MaterialCostsTable, {})
1503
+ ] }) });
1504
+ };
1505
+ const config$1 = adminSdk.defineRouteConfig({
1506
+ icon: icons.CurrencyDollarSolid,
1507
+ label: "ต้นทุนวัสดุ"
1508
+ });
1509
+ const CARRIER_TYPES$1 = [
1510
+ { value: "COMPANY_FLEET", label: "บริษัทรถส่งของ (Messenger)" },
1511
+ { value: "COMPANY_TRUCK", label: "รถบริษัท (Same Day)" },
1512
+ { value: "PRIVATE_CARRIER", label: "ขนส่งเอกชน (EMS/Express)" }
1513
+ ];
1514
+ const SERVICE_CODES$1 = [
1515
+ { value: "MESSENGER_3H", label: "ส่งด่วน 3 ชม.", usesHours: true },
1516
+ { value: "SAME_DAY", label: "ส่งด่วนภายในวัน", usesHours: true },
1517
+ { value: "STANDARD_3_5D", label: "EMS 3-5 วัน", usesHours: false },
1518
+ { value: "EXPRESS_1_2D", label: "Express 1-2 วัน", usesHours: false }
1519
+ ];
1520
+ const ShippingRateModal = ({ rate, onClose }) => {
1521
+ const [formData, setFormData] = react.useState({
1522
+ carrier_type: "COMPANY_FLEET",
1523
+ service_code: "MESSENGER_3H",
1524
+ max_weight_kg: "",
1525
+ price: "",
1526
+ currency: "THB",
1527
+ priority: "0",
1528
+ eta_hours_min: "",
1529
+ eta_hours_max: "",
1530
+ eta_days_min: "",
1531
+ eta_days_max: "",
1532
+ active: true
1533
+ });
1534
+ const [isSaving, setIsSaving] = react.useState(false);
1535
+ const selectedService = SERVICE_CODES$1.find((s) => s.value === formData.service_code);
1536
+ const usesHours = (selectedService == null ? void 0 : selectedService.usesHours) ?? false;
1537
+ react.useEffect(() => {
1538
+ var _a, _b, _c, _d;
1539
+ if (rate) {
1540
+ setFormData({
1541
+ carrier_type: rate.carrier_type,
1542
+ service_code: rate.service_code,
1543
+ max_weight_kg: rate.max_weight_kg.toString(),
1544
+ price: rate.price.toString(),
1545
+ currency: rate.currency,
1546
+ priority: (rate.priority ?? 0).toString(),
1547
+ eta_hours_min: ((_a = rate.eta_hours_min) == null ? void 0 : _a.toString()) || "",
1548
+ eta_hours_max: ((_b = rate.eta_hours_max) == null ? void 0 : _b.toString()) || "",
1549
+ eta_days_min: ((_c = rate.eta_days_min) == null ? void 0 : _c.toString()) || "",
1550
+ eta_days_max: ((_d = rate.eta_days_max) == null ? void 0 : _d.toString()) || "",
1551
+ active: rate.active
1552
+ });
1553
+ }
1554
+ }, [rate]);
1555
+ const handleSubmit = async (e) => {
1556
+ e.preventDefault();
1557
+ const errors = [];
1558
+ const maxWeight = parseFloat(formData.max_weight_kg);
1559
+ const price = parseFloat(formData.price);
1560
+ const priority = parseInt(formData.priority);
1561
+ if (isNaN(maxWeight) || maxWeight <= 0) {
1562
+ errors.push("น้ำหนักสูงสุดต้องเป็นตัวเลขมากกว่า 0");
1563
+ }
1564
+ if (isNaN(price) || price < 0) {
1565
+ errors.push("ราคาต้องเป็นตัวเลขไม่ติดลบ");
1566
+ }
1567
+ if (isNaN(priority)) {
1568
+ errors.push("ลำดับความสำคัญต้องเป็นตัวเลข");
1569
+ }
1570
+ let etaHoursMin;
1571
+ let etaHoursMax;
1572
+ let etaDaysMin;
1573
+ let etaDaysMax;
1574
+ if (usesHours) {
1575
+ etaHoursMin = formData.eta_hours_min ? parseFloat(formData.eta_hours_min) : void 0;
1576
+ etaHoursMax = formData.eta_hours_max ? parseFloat(formData.eta_hours_max) : void 0;
1577
+ if (etaHoursMin === void 0 || etaHoursMax === void 0) {
1578
+ errors.push("กรุณากรอก ETA เป็นชั่วโมงให้ครบ");
1579
+ } else if (etaHoursMin > etaHoursMax) {
1580
+ errors.push("ETA ชั่วโมงต่ำสุดต้องน้อยกว่าหรือเท่ากับสูงสุด");
1581
+ }
1582
+ } else {
1583
+ etaDaysMin = formData.eta_days_min ? parseFloat(formData.eta_days_min) : void 0;
1584
+ etaDaysMax = formData.eta_days_max ? parseFloat(formData.eta_days_max) : void 0;
1585
+ if (etaDaysMin === void 0 || etaDaysMax === void 0) {
1586
+ errors.push("กรุณากรอก ETA เป็นวันให้ครบ");
1587
+ } else if (etaDaysMin > etaDaysMax) {
1588
+ errors.push("ETA วันต่ำสุดต้องน้อยกว่าหรือเท่ากับสูงสุด");
1589
+ }
1590
+ }
1591
+ if (errors.length > 0) {
1592
+ ui.toast.error("ข้อมูลไม่ถูกต้อง", {
1593
+ description: errors.join(", ")
1594
+ });
1595
+ return;
1596
+ }
1597
+ setIsSaving(true);
1598
+ try {
1599
+ const payload = {
1600
+ carrier_type: formData.carrier_type,
1601
+ service_code: formData.service_code,
1602
+ max_weight_kg: maxWeight,
1603
+ price,
1604
+ currency: formData.currency,
1605
+ priority,
1606
+ active: formData.active
1607
+ };
1608
+ if (usesHours) {
1609
+ payload.eta_hours_min = etaHoursMin;
1610
+ payload.eta_hours_max = etaHoursMax;
1611
+ } else {
1612
+ payload.eta_days_min = etaDaysMin;
1613
+ payload.eta_days_max = etaDaysMax;
1614
+ }
1615
+ const url = rate ? `/admin/shipping-rates/${rate.id}` : "/admin/shipping-rates";
1616
+ const method = rate ? "PUT" : "POST";
1617
+ const response = await fetch(url, {
1618
+ method,
1619
+ headers: { "Content-Type": "application/json" },
1620
+ credentials: "include",
1621
+ body: JSON.stringify(payload)
1622
+ });
1623
+ if (response.ok) {
1624
+ ui.toast.success("สำเร็จ", {
1625
+ description: rate ? "แก้ไขอัตราค่าขนส่งแล้ว" : "สร้างอัตราค่าขนส่งใหม่แล้ว"
1626
+ });
1627
+ onClose();
1628
+ } else {
1629
+ const error = await response.json();
1630
+ throw new Error(error.message || "Failed to save");
1631
+ }
1632
+ } catch (error) {
1633
+ ui.toast.error("ข้อผิดพลาด", {
1634
+ description: error instanceof Error ? error.message : "ไม่สามารถบันทึกข้อมูลได้"
1635
+ });
1636
+ } finally {
1637
+ setIsSaving(false);
1638
+ }
1639
+ };
1640
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Content, { children: [
1641
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Title, { children: rate ? "แก้ไขอัตราค่าขนส่ง" : "สร้างอัตราค่าขนส่งใหม่" }) }),
1642
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Drawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, className: "flex flex-col gap-y-4", children: [
1643
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1644
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "carrier", children: "ประเภทขนส่ง *" }),
1645
+ /* @__PURE__ */ jsxRuntime.jsxs(
1646
+ ui.Select,
1647
+ {
1648
+ value: formData.carrier_type,
1649
+ onValueChange: (value) => setFormData({ ...formData, carrier_type: value }),
1650
+ children: [
1651
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "เลือกประเภทขนส่ง" }) }),
1652
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: CARRIER_TYPES$1.map((carrier) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: carrier.value, children: carrier.label }, carrier.value)) })
1653
+ ]
1654
+ }
1655
+ )
1656
+ ] }),
1657
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1658
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "service", children: "บริการจัดส่ง *" }),
1659
+ /* @__PURE__ */ jsxRuntime.jsxs(
1660
+ ui.Select,
1661
+ {
1662
+ value: formData.service_code,
1663
+ onValueChange: (value) => setFormData({
1664
+ ...formData,
1665
+ service_code: value,
1666
+ eta_hours_min: "",
1667
+ eta_hours_max: "",
1668
+ eta_days_min: "",
1669
+ eta_days_max: ""
1670
+ }),
1671
+ children: [
1672
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "เลือกบริการ" }) }),
1673
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: SERVICE_CODES$1.map((service) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: service.value, children: service.label }, service.value)) })
1674
+ ]
1675
+ }
1676
+ )
1677
+ ] }),
1678
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-4", children: [
1679
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1680
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "maxWeight", children: "น้ำหนักสูงสุด (kg) *" }),
1681
+ /* @__PURE__ */ jsxRuntime.jsx(
1682
+ ui.Input,
1683
+ {
1684
+ id: "maxWeight",
1685
+ type: "number",
1686
+ step: "0.1",
1687
+ min: "0",
1688
+ value: formData.max_weight_kg,
1689
+ onChange: (e) => setFormData({ ...formData, max_weight_kg: e.target.value }),
1690
+ placeholder: "เช่น 3",
1691
+ required: true
1692
+ }
1693
+ )
1694
+ ] }),
1695
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1696
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "price", children: "ราคา (THB) *" }),
1697
+ /* @__PURE__ */ jsxRuntime.jsx(
1698
+ ui.Input,
1699
+ {
1700
+ id: "price",
1701
+ type: "number",
1702
+ step: "0.01",
1703
+ min: "0",
1704
+ value: formData.price,
1705
+ onChange: (e) => setFormData({ ...formData, price: e.target.value }),
1706
+ placeholder: "เช่น 80",
1707
+ required: true
1708
+ }
1709
+ )
1710
+ ] })
1711
+ ] }),
1712
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1713
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "priority", children: "ลำดับความสำคัญ" }),
1714
+ /* @__PURE__ */ jsxRuntime.jsx(
1715
+ ui.Input,
1716
+ {
1717
+ id: "priority",
1718
+ type: "number",
1719
+ value: formData.priority,
1720
+ onChange: (e) => setFormData({ ...formData, priority: e.target.value }),
1721
+ placeholder: "0"
1722
+ }
1723
+ ),
1724
+ /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs text-ui-fg-subtle mt-1", children: "ตัวเลขสูงกว่าจะถูกเสนอให้เลือกก่อน (ค่าเริ่มต้น 0)" })
1725
+ ] }),
1726
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1727
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Label, { children: [
1728
+ usesHours ? "ETA (ชั่วโมง)" : "ETA (วัน)",
1729
+ " *"
1730
+ ] }),
1731
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "grid grid-cols-2 gap-x-4 mt-2", children: usesHours ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1732
+ /* @__PURE__ */ jsxRuntime.jsx(
1733
+ ui.Input,
1734
+ {
1735
+ type: "number",
1736
+ placeholder: "ต่ำสุด",
1737
+ value: formData.eta_hours_min,
1738
+ onChange: (e) => setFormData({ ...formData, eta_hours_min: e.target.value }),
1739
+ required: true
1740
+ }
1741
+ ),
1742
+ /* @__PURE__ */ jsxRuntime.jsx(
1743
+ ui.Input,
1744
+ {
1745
+ type: "number",
1746
+ placeholder: "สูงสุด",
1747
+ value: formData.eta_hours_max,
1748
+ onChange: (e) => setFormData({ ...formData, eta_hours_max: e.target.value }),
1749
+ required: true
1750
+ }
1751
+ )
1752
+ ] }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1753
+ /* @__PURE__ */ jsxRuntime.jsx(
1754
+ ui.Input,
1755
+ {
1756
+ type: "number",
1757
+ placeholder: "ต่ำสุด",
1758
+ value: formData.eta_days_min,
1759
+ onChange: (e) => setFormData({ ...formData, eta_days_min: e.target.value }),
1760
+ required: true
1761
+ }
1762
+ ),
1763
+ /* @__PURE__ */ jsxRuntime.jsx(
1764
+ ui.Input,
1765
+ {
1766
+ type: "number",
1767
+ placeholder: "สูงสุด",
1768
+ value: formData.eta_days_max,
1769
+ onChange: (e) => setFormData({ ...formData, eta_days_max: e.target.value }),
1770
+ required: true
1771
+ }
1772
+ )
1773
+ ] }) })
1774
+ ] }),
1775
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
1776
+ /* @__PURE__ */ jsxRuntime.jsx(
1777
+ ui.Switch,
1778
+ {
1779
+ id: "active",
1780
+ checked: formData.active,
1781
+ onCheckedChange: (checked) => setFormData({ ...formData, active: checked })
1782
+ }
1783
+ ),
1784
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "active", children: "เปิดใช้งาน" })
1785
+ ] }),
1786
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Drawer.Footer, { children: [
1787
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "button", variant: "secondary", onClick: onClose, disabled: isSaving, children: "ยกเลิก" }),
1788
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", disabled: isSaving, children: isSaving ? "กำลังบันทึก..." : "บันทึก" })
1789
+ ] })
1790
+ ] }) })
1791
+ ] }) });
1792
+ };
1793
+ const CARRIER_TYPES = [
1794
+ { value: "ALL", label: "ทั้งหมด" },
1795
+ { value: "COMPANY_FLEET", label: "บริษัทรถส่งของ (Messenger)" },
1796
+ { value: "COMPANY_TRUCK", label: "รถบริษัท (Same Day)" },
1797
+ { value: "PRIVATE_CARRIER", label: "ขนส่งเอกชน (EMS/Express)" }
1798
+ ];
1799
+ const SERVICE_CODES = [
1800
+ { value: "ALL", label: "ทั้งหมด" },
1801
+ { value: "MESSENGER_3H", label: "ส่งด่วน 3 ชม." },
1802
+ { value: "SAME_DAY", label: "ส่งด่วนภายในวัน" },
1803
+ { value: "STANDARD_3_5D", label: "EMS 3-5 วัน" },
1804
+ { value: "EXPRESS_1_2D", label: "Express 1-2 วัน" }
1805
+ ];
1806
+ const ShippingRatesTable = () => {
1807
+ const [rates, setRates] = react.useState([]);
1808
+ const [filteredRates, setFilteredRates] = react.useState([]);
1809
+ const [carrierFilter, setCarrierFilter] = react.useState("ALL");
1810
+ const [serviceFilter, setServiceFilter] = react.useState("ALL");
1811
+ const [isModalOpen, setIsModalOpen] = react.useState(false);
1812
+ const [editingRate, setEditingRate] = react.useState(null);
1813
+ const [isLoading, setIsLoading] = react.useState(false);
1814
+ const [deleteRateId, setDeleteRateId] = react.useState(null);
1815
+ react.useEffect(() => {
1816
+ fetchRates();
1817
+ }, []);
1818
+ react.useEffect(() => {
1819
+ let filtered = rates;
1820
+ if (carrierFilter !== "ALL") {
1821
+ filtered = filtered.filter((rate) => rate.carrier_type === carrierFilter);
1822
+ }
1823
+ if (serviceFilter !== "ALL") {
1824
+ filtered = filtered.filter((rate) => rate.service_code === serviceFilter);
1825
+ }
1826
+ setFilteredRates(filtered);
1827
+ }, [carrierFilter, serviceFilter, rates]);
1828
+ const fetchRates = async () => {
1829
+ setIsLoading(true);
1830
+ try {
1831
+ const response = await fetch("/admin/shipping-rates", {
1832
+ credentials: "include"
1833
+ });
1834
+ const data = await response.json();
1835
+ setRates(data.rates || []);
1836
+ } catch (error) {
1837
+ ui.toast.error("ข้อผิดพลาด", {
1838
+ description: "ไม่สามารถโหลดข้อมูลอัตราค่าขนส่งได้"
1839
+ });
1840
+ } finally {
1841
+ setIsLoading(false);
1842
+ }
1843
+ };
1844
+ const handleDelete = async () => {
1845
+ if (!deleteRateId) return;
1846
+ try {
1847
+ const response = await fetch(`/admin/shipping-rates/${deleteRateId}`, {
1848
+ method: "DELETE",
1849
+ credentials: "include"
1850
+ });
1851
+ if (response.ok) {
1852
+ ui.toast.success("สำเร็จ", {
1853
+ description: "ลบอัตราค่าขนส่งแล้ว"
1854
+ });
1855
+ fetchRates();
1856
+ } else {
1857
+ throw new Error("Failed to delete");
1858
+ }
1859
+ } catch (error) {
1860
+ ui.toast.error("ข้อผิดพลาด", {
1861
+ description: "ไม่สามารถลบอัตราค่าขนส่งได้"
1862
+ });
1863
+ } finally {
1864
+ setDeleteRateId(null);
1865
+ }
1866
+ };
1867
+ const handleEdit = (rate) => {
1868
+ setEditingRate(rate);
1869
+ setIsModalOpen(true);
1870
+ };
1871
+ const handleCreateNew = () => {
1872
+ setEditingRate(null);
1873
+ setIsModalOpen(true);
1874
+ };
1875
+ const handleModalClose = () => {
1876
+ setIsModalOpen(false);
1877
+ setEditingRate(null);
1878
+ fetchRates();
1879
+ };
1880
+ const getCarrierLabel = (type) => {
1881
+ var _a;
1882
+ return ((_a = CARRIER_TYPES.find((c) => c.value === type)) == null ? void 0 : _a.label) || type;
1883
+ };
1884
+ const getServiceLabel = (code) => {
1885
+ var _a;
1886
+ return ((_a = SERVICE_CODES.find((s) => s.value === code)) == null ? void 0 : _a.label) || code;
1887
+ };
1888
+ const formatETA = (rate) => {
1889
+ if (rate.eta_hours_min !== null && rate.eta_hours_min !== void 0 && rate.eta_hours_max !== null && rate.eta_hours_max !== void 0) {
1890
+ return `${rate.eta_hours_min}-${rate.eta_hours_max} ชม.`;
1891
+ }
1892
+ if (rate.eta_days_min !== null && rate.eta_days_min !== void 0 && rate.eta_days_max !== null && rate.eta_days_max !== void 0) {
1893
+ return `${rate.eta_days_min}-${rate.eta_days_max} วัน`;
1894
+ }
1895
+ return "-";
1896
+ };
1897
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1898
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
1899
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between gap-x-4", children: [
1900
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-x-4 items-end", children: [
1901
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-1", children: [
1902
+ /* @__PURE__ */ jsxRuntime.jsx("label", { className: "text-sm font-medium text-ui-fg-subtle", children: "ประเภทขนส่ง" }),
1903
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Select, { value: carrierFilter, onValueChange: setCarrierFilter, children: [
1904
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "ทั้งหมด" }) }),
1905
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: CARRIER_TYPES.map((type) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: type.value, children: type.label }, type.value)) })
1906
+ ] })
1907
+ ] }),
1908
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-1", children: [
1909
+ /* @__PURE__ */ jsxRuntime.jsx("label", { className: "text-sm font-medium text-ui-fg-subtle", children: "บริการจัดส่ง" }),
1910
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Select, { value: serviceFilter, onValueChange: setServiceFilter, children: [
1911
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "ทั้งหมด" }) }),
1912
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: SERVICE_CODES.map((service) => /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: service.value, children: service.label }, service.value)) })
1913
+ ] })
1914
+ ] })
1915
+ ] }),
1916
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Button, { onClick: handleCreateNew, children: [
1917
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Plus, {}),
1918
+ "สร้างอัตราใหม่"
1919
+ ] })
1920
+ ] }),
1921
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Table, { children: [
1922
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Header, { children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
1923
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ประเภทขนส่ง" }),
1924
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "บริการ" }),
1925
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "น้ำหนัก ≤ (kg)" }),
1926
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ราคา (THB)" }),
1927
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "ETA" }),
1928
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "สถานะ" }),
1929
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { className: "text-right", children: "การจัดการ" })
1930
+ ] }) }),
1931
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Body, { children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "กำลังโหลด..." }) }) : filteredRates.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Row, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-center", children: "ไม่พบข้อมูลอัตราค่าขนส่ง" }) }) : filteredRates.map((rate) => /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
1932
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: getCarrierLabel(rate.carrier_type) }),
1933
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: getServiceLabel(rate.service_code) }),
1934
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Cell, { children: [
1935
+ "≤ ",
1936
+ rate.max_weight_kg,
1937
+ " kg"
1938
+ ] }),
1939
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: rate.price.toFixed(2) }),
1940
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: "blue", children: formatETA(rate) }) }),
1941
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: rate.active ? "green" : "grey", children: rate.active ? "เปิดใช้งาน" : "ปิดใช้งาน" }) }),
1942
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
1943
+ /* @__PURE__ */ jsxRuntime.jsx(
1944
+ ui.Button,
1945
+ {
1946
+ variant: "transparent",
1947
+ size: "small",
1948
+ onClick: () => handleEdit(rate),
1949
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Edit, {})
1950
+ }
1951
+ ),
1952
+ /* @__PURE__ */ jsxRuntime.jsx(
1953
+ ui.Button,
1954
+ {
1955
+ variant: "transparent",
1956
+ size: "small",
1957
+ onClick: () => setDeleteRateId(rate.id),
1958
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Trash, {})
1959
+ }
1960
+ )
1961
+ ] }) })
1962
+ ] }, rate.id)) })
1963
+ ] })
1964
+ ] }),
1965
+ isModalOpen && /* @__PURE__ */ jsxRuntime.jsx(ShippingRateModal, { rate: editingRate, onClose: handleModalClose }),
1966
+ /* @__PURE__ */ jsxRuntime.jsx(
1967
+ ui.Prompt,
1968
+ {
1969
+ variant: "confirmation",
1970
+ open: !!deleteRateId,
1971
+ onOpenChange: () => setDeleteRateId(null),
1972
+ children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Content, { children: [
1973
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Header, { children: [
1974
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Title, { children: "ลบอัตราค่าขนส่ง" }),
1975
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Description, { children: "คุณต้องการลบอัตราค่าขนส่งนี้ใช่หรือไม่? การดำเนินการนี้ไม่สามารถย้อนกลับได้" })
1976
+ ] }),
1977
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Prompt.Footer, { children: [
1978
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Cancel, { children: "ยกเลิก" }),
1979
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Prompt.Action, { onClick: handleDelete, children: "ลบ" })
1980
+ ] })
1981
+ ] })
1982
+ }
1983
+ )
1984
+ ] });
1985
+ };
1986
+ const RatesPage = () => {
1987
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
1988
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h1", children: "อัตราค่าขนส่ง" }) }),
1989
+ /* @__PURE__ */ jsxRuntime.jsx(ShippingRatesTable, {})
1990
+ ] }) });
1991
+ };
1992
+ const config = adminSdk.defineRouteConfig({
1993
+ icon: icons.HandTruck,
1994
+ label: "อัตราค่าขนส่ง"
1995
+ });
1996
+ const widgetModule = { widgets: [
1997
+ {
1998
+ Component: OrderShippingQuoteWidget,
1999
+ zone: ["order.details.after"]
2000
+ }
2001
+ ] };
2002
+ const routeModule = {
2003
+ routes: [
2004
+ {
2005
+ Component: AreasPage,
2006
+ path: "/areas"
2007
+ },
2008
+ {
2009
+ Component: BoxesPage,
2010
+ path: "/boxes"
2011
+ },
2012
+ {
2013
+ Component: MaterialCostsPage,
2014
+ path: "/material-costs"
2015
+ },
2016
+ {
2017
+ Component: RatesPage,
2018
+ path: "/rates"
2019
+ }
2020
+ ]
2021
+ };
2022
+ const menuItemModule = {
2023
+ menuItems: [
2024
+ {
2025
+ label: config$3.label,
2026
+ icon: config$3.icon,
2027
+ path: "/areas",
2028
+ nested: void 0
2029
+ },
2030
+ {
2031
+ label: config$2.label,
2032
+ icon: config$2.icon,
2033
+ path: "/boxes",
2034
+ nested: void 0
2035
+ },
2036
+ {
2037
+ label: config$1.label,
2038
+ icon: config$1.icon,
2039
+ path: "/material-costs",
2040
+ nested: void 0
2041
+ },
2042
+ {
2043
+ label: config.label,
2044
+ icon: config.icon,
2045
+ path: "/rates",
2046
+ nested: void 0
2047
+ }
2048
+ ]
2049
+ };
2050
+ const formModule = { customFields: {} };
2051
+ const displayModule = {
2052
+ displays: {}
2053
+ };
2054
+ const i18nModule = { resources: {} };
2055
+ const plugin = {
2056
+ widgetModule,
2057
+ routeModule,
2058
+ menuItemModule,
2059
+ formModule,
2060
+ displayModule,
2061
+ i18nModule
2062
+ };
2063
+ module.exports = plugin;