import { Button } from "@/components/ui/button";
import { useSessionStore } from "@/lib/store/sessionStore";
//import {useSettingStore} from "@/lib/store/useSettings";
import { LotType } from "@/types/lots";
import { Tag } from "lucide-react";
import { toast } from "sonner";
import { manualPrintLabels } from "./ManualPrintLabel";
import { useState } from "react";

export default function ManualPrint({ lot }: { lot: LotType }) {
    const { user } = useSessionStore();
    const [printing, setPrinting] = useState(false);
    //const {settings} = useSettingStore();
    //const server = settings.filter((n) => n.name === "server")[0]?.value;
    //const serverPort = settings.filter((n) => n.name === "serverPort")[0]?.value;
    //const serverUrl = `http://${server}:${serverPort}`;

    const handlePrintLabel = async (lot: LotType) => {
        const labels: any = await manualPrintLabels(lot, user);

        if (labels.success) {
            toast.success(labels.message);
            setTimeout(() => {
                setPrinting(false);
            }, 5 * 1000);
            setPrinting(true);
        } else {
            toast.error(labels.message);
            setTimeout(() => {
                setPrinting(false);
            }, 5 * 1000);
        }
    };
    return (
        <Button
            variant="outline"
            size="icon"
            onClick={() => handlePrintLabel(lot)}
            disabled={printing}
        >
            <Tag className="h-[16px] w-[16px]" />
        </Button>
    );
}
