import React, { useState } from "react";
import SingleView from "@/Components/SingleView";
import { quotationSingleViewConfig } from "./quotationSingleViewConfig";
import QuotationPrintPage from "./QuotationPrintPage";

function QuotationSingleView({ auth, quotation, userApprovalAssignment }) {
    const [showPrintView, setShowPrintView] = useState(false);

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

    // Add print button to header
    const configWithPrint = {
        ...quotationSingleViewConfig,
        printConfig: {
            label: "Print",
            icon: "pi pi-print",
            onPrint: handlePrint,
            className: "p-button-info p-button-sm",
        },
    };

    return (
        <>
            {showPrintView && (
                <div className="print-view print:block hidden">
                    <QuotationPrintPage quotationInfo={quotation} />
                </div>
            )}
            <SingleView
                auth={auth}
                data={quotation}
                userApprovalAssignment={userApprovalAssignment}
                config={configWithPrint}
            />
        </>
    );
}

export default QuotationSingleView;
