import React, { useState } from "react";
import SingleView from "@/Components/SingleView";
import { requisitionSingleViewConfig } from "./requisitionSingleViewConfig";
import PrintRequisition from "./PrintRequisition";
import PrintOutOfStock from "./PrintOutOfStock";

function RequisitionSingleView({ auth, requisition, userApprovalAssignment }) {
    const [showPrintView, setShowPrintView] = useState(false);
    const [printType, setPrintType] = useState("all");

    const handlePrint = (type = "all") => {
        setPrintType(type);
        setShowPrintView(true);
        setTimeout(() => {
            window.print();
            setTimeout(() => {
                setShowPrintView(false);
            }, 500);
        }, 300);
    };

    // Add print buttons to header
    const configWithPrint = {
        ...requisitionSingleViewConfig,
        printConfig: {
            buttons: [
                {
                    label: "Print",
                    icon: "pi pi-print",
                    onClick: () => handlePrint("all"),
                    className: "p-button-info p-button-sm",
                },
                {
                    label: "Print Out of Stock",
                    icon: "pi pi-exclamation-triangle",
                    onClick: () => handlePrint("outOfStock"),
                    className: "p-button-warning p-button-sm",
                },
            ],
        },
    };

    return (
        <>
            {showPrintView && (
                <div className="print-view">
                    {printType === "all" ? (
                        <PrintRequisition requisitionData={requisition} auth={auth} />
                    ) : (
                        <PrintOutOfStock requisition={requisition} auth={auth} />
                    )}
                </div>
            )}
            <SingleView
                auth={auth}
                data={requisition}
                userApprovalAssignment={userApprovalAssignment}
                config={configWithPrint}
            />
        </>
    );
}

export default RequisitionSingleView;
