"use client"; import { NEXT_PUBLIC_API_URL } from "@/config/api"; import { clientAuthFetch } from "@/app/utils/clientAuthFetch"; export interface JobOrderListItem { id: number; code: string | null; planStart: string | null; itemCode: string | null; itemName: string | null; reqQty: number | null; stockInLineId: number | null; itemId: number | null; lotNo: string | null; bagPrintedQty?: number; labelPrintedQty?: number; laserPrintedQty?: number; } export interface LaserBag2Settings { host: string; port: number; /** Comma-separated item codes; empty string = show all packaging job orders */ itemCodes: string; } export interface LaserBag2SendRequest { itemId: number | null; stockInLineId: number | null; itemCode: string | null; itemName: string | null; printerIp?: string; printerPort?: number; } export interface LaserBag2SendResponse { success: boolean; message: string; payloadSent?: string | null; } /** * Uses server LASER_PRINT.itemCodes filter. Calls public GET /py/laser-job-orders (same as Python Bag2 /py/job-orders), * so it works without relying on authenticated /plastic routes. */ export async function fetchLaserJobOrders(planStart: string): Promise { const base = (NEXT_PUBLIC_API_URL ?? "").replace(/\/$/, ""); if (!base) { throw new Error("NEXT_PUBLIC_API_URL is not set; cannot reach API."); } const url = `${base}/py/laser-job-orders?planStart=${encodeURIComponent(planStart)}`; let res: Response; try { res = await clientAuthFetch(url, { method: "GET" }); } catch (e) { const msg = e instanceof Error ? e.message : String(e); throw new Error( `無法連線 API(${url}):${msg}。請確認後端已啟動且 NEXT_PUBLIC_API_URL 指向正確(例如 http://localhost:8090/api)。`, ); } if (!res.ok) { const body = await res.text().catch(() => ""); throw new Error( `載入工單失敗(${res.status})${body ? `:${body.slice(0, 200)}` : ""}`, ); } return res.json() as Promise; } export async function fetchLaserBag2Settings(): Promise { const base = (NEXT_PUBLIC_API_URL ?? "").replace(/\/$/, ""); if (!base) { throw new Error("NEXT_PUBLIC_API_URL is not set."); } const url = `${base}/plastic/laser-bag2-settings`; let res: Response; try { res = await clientAuthFetch(url, { method: "GET" }); } catch (e) { const msg = e instanceof Error ? e.message : String(e); throw new Error(`無法連線至 ${url}:${msg}`); } if (!res.ok) { const body = await res.text().catch(() => ""); throw new Error(`載入設定失敗(${res.status})${body ? body.slice(0, 200) : ""}`); } return res.json() as Promise; } export async function sendLaserBag2Job(body: LaserBag2SendRequest): Promise { const url = `${NEXT_PUBLIC_API_URL}/plastic/print-laser-bag2`; const res = await clientAuthFetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); const data = (await res.json()) as LaserBag2SendResponse; if (!res.ok) { return data; } return data; } export interface PrinterStatusRequest { printerType: "laser"; printerIp?: string; printerPort?: number; } export interface PrinterStatusResponse { connected: boolean; message: string; } export async function checkPrinterStatus(request: PrinterStatusRequest): Promise { const url = `${NEXT_PUBLIC_API_URL}/plastic/check-printer`; const res = await clientAuthFetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(request), }); const data = (await res.json()) as PrinterStatusResponse; return data; } export async function patchSetting(name: string, value: string): Promise { const url = `${NEXT_PUBLIC_API_URL}/settings/${encodeURIComponent(name)}`; const res = await clientAuthFetch(url, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ value }), }); if (!res.ok) { const t = await res.text().catch(() => ""); throw new Error(t || `Failed to save setting: ${res.status}`); } }