document.addEventListener('DOMContentLoaded', function () {
    if (typeof QRCode === 'undefined') {
        console.error('QRCode library not loaded.');
        return;
    }

    const qrCanvases = document.querySelectorAll('canvas[data-url]');
    if (!qrCanvases.length) return;

    qrCanvases.forEach(function (qrCodeCanvas) {
        const {
            url,
            qrColor = '#000000',
            qrBgColor = '#ffffff',
            qrWidth = '150',
            qrMargin = '2',
            qrCorrectionLevel = 'L'
        } = qrCodeCanvas.dataset;

        if (!url) return;

        const qrCodeOptions = {
            color: {
                dark: qrColor,
                light: qrBgColor
            },
            width: parseInt(qrWidth),
            margin: parseInt(qrMargin),
            errorCorrectionLevel: qrCorrectionLevel
        };

        QRCode.toCanvas(qrCodeCanvas, url, qrCodeOptions, function (error) {
            if (error) console.error('QR Code generation failed:', error);
        });
    });
});