"use client"; import Image from "next/image"; import PackageCamera from "./lib/camera/component/pakageCamera"; import BarcodeScanner from "./lib/camera/component/scannerBarcode"; import SjCamera from "./lib/camera/component/sjCamera"; import DispatchCamera from "./lib/camera/component/dispatchCamera"; import useCameraStore from "./lib/camera/hooks/useCameraStore"; import Header from "./lib/camera/component/hedear"; import { Button, FormControl, InputLabel, MenuItem, Select, FormHelperText, IconButton, Tooltip, } from "@mui/material"; import { SaveAsOutlined, Close as CloseIcon } from "@mui/icons-material"; // import axios from "axios"; import odooApi from "./lib/api/odooApi"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { getAuth } from "./lib/api/auth"; import { getCookie } from "cookies-next"; import { clearOdooSession } from "./lib/api/clearOdooSession"; type Role = "driver" | "dispatch"; type ShipMethod = "" | "self_pickup" | "indoteknik_delivery" | "ekspedisi"; export default function Home() { const [isLogin, setIsLogin] = useState(false); const [isDriver, setIsDriver] = useState(false); const [isDispatch, setIsDispatch] = useState(false); const [shippingMethod, setShippingMethod] = useState(""); const [shipTouched, setShipTouched] = useState(false); const { barcode, imageSj, imagePackage, imageDispatch, setBarcode, setImageSj, setImagePackage, setImageDispatch, removeSjImage, } = useCameraStore(); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); // Auth gate + role dari cookie useEffect(() => { const auth = getAuth(); if (!auth) { void clearOdooSession(process.env.NEXT_PUBLIC_ODOO_API_HOST ?? ""); router.replace("/login"); return; } const roleCookie = ( getCookie("web_role") as string | undefined )?.toLowerCase() as Role | undefined; setIsDriver(roleCookie === "driver"); setIsDispatch(roleCookie === "dispatch"); setIsLogin(true); }, [router]); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setIsLoading(true); if (!barcode) { alert("Barcode harus diisi."); setIsLoading(false); return; } if (isDispatch) setShipTouched(true); if (isDispatch && !shippingMethod) { alert("Shipping Method wajib dipilih."); setIsLoading(false); return; } // --- normalisasi SJ jadi array & strip prefix --- const sjArr: string[] = Array.isArray(imageSj) ? imageSj : imageSj ? [imageSj as unknown as string] : []; const sjList = sjArr.map((x) => x.replace(/^.*?,/, "")); // base64 murni const pkgB64 = imagePackage ? imagePackage.replace(/^.*?,/, "") : undefined; const dispatchB64 = imageDispatch && imageDispatch.startsWith("data:") ? imageDispatch.replace(/^.*?,/, "") : imageDispatch || undefined; // payload umum (dikirm sekali saja) const common: Record = {}; if (pkgB64) common.paket_document = pkgB64; if (!isDriver && dispatchB64) common.dispatch_document = dispatchB64; if (isDispatch && shippingMethod) common.shipping_method = shippingMethod; if (shippingMethod === "self_pickup") common.self_pu = "true"; const url = `/api/v1/stock-picking/${barcode}/documentation`; try { // 1) Kalau ada foto SJ, kirim yang pertama + payload umum if (sjList.length > 0) { await odooApi("PUT", url, { ...common, sj_documentations: sjList[0] }); // 2) Kirim sisa foto SJ satu-per-satu (tanpa field lain) for (let i = 1; i < sjList.length; i += 1) { await odooApi("PUT", url, { sj_documentations: sjList[i] }); } } else { // Tidak ada foto SJ → cukup kirim field lain await odooApi("PUT", url, common); } alert("Berhasil Submit Data"); setBarcode(""); // bersihkan semua SJ yang sudah terkirim if (sjList.length > 0) setImageSj([]); if (pkgB64) setImagePackage(""); if (dispatchB64) setImageDispatch(""); } catch (err) { console.error(err); alert("Gagal submit sebagian/seluruh foto. Coba lagi."); } finally { setIsLoading(false); } }; // === UI helpers === const sjArr: string[] = Array.isArray(imageSj) ? imageSj : imageSj ? [imageSj as unknown as string] : []; // dispatch: SJ hanya utk self_pickup & ekspedisi const showSjForDispatch = isDispatch && (shippingMethod === "self_pickup" || shippingMethod === "ekspedisi"); const showDispatchForDispatch = isDispatch && shippingMethod !== ""; const showSjPreview = sjArr.length > 0 && (!isDispatch || showSjForDispatch); const showDispatchPreview = !!imageDispatch && (!isDispatch || showDispatchForDispatch); return (
{isLogin ? (
{/* Shipping Method (khusus dispatch) */} {isDispatch && (
Shipping Method {shipTouched && !shippingMethod && ( Wajib pilih shipping method. )}
)}
{isDispatch ? ( <> {showSjForDispatch && } {showDispatchForDispatch && } ) : ( <> {/* driver / non-dispatch */} )}
{/* Preview SJ (multi) + Remove per item */} {showSjPreview && ( <>
{sjArr.map((src, i) => (
{`SJ
removeSjImage(i)} sx={{ bgcolor: "rgba(255,255,255,0.85)", "&:hover": { bgcolor: "rgba(255,255,255,1)" }, }} >
))}
)} {/* Preview Penerima (hanya non-dispatch) */} {!isDispatch && imagePackage && ( <>
Captured
)} {/* Preview Dispatch */} {showDispatchPreview && ( <>
Captured
)}
) : (

Loading...

)}
); }