document.addEventListener('DOMContentLoaded', () => {
    const pasteId = document.body.dataset.pasteId;  // Obtener `pasteId` desde `data-*`
    const downloadUrl = document.body.dataset.downloadUrl; // `data-download-url`
    const qrCodeContainer = document.getElementById('qrcode');

    if (qrCodeContainer && downloadUrl) {
        new QRCode(qrCodeContainer, {
            text: downloadUrl,
            width: 200,
            height: 200,
            colorDark: "#000000",
            colorLight: "#ffffff",
            correctLevel: QRCode.CorrectLevel.H
        });

        const downloadQrButton = document.getElementById('download-qr');
        if (downloadQrButton) {
            downloadQrButton.addEventListener('click', () => {
                const canvas = qrCodeContainer.querySelector('canvas');
                if (canvas) {
                    const pngUrl = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
                    const downloadLink = document.createElement('a');
                    downloadLink.href = pngUrl;
                    downloadLink.download = `qr_code_paste_${pasteId}.png`;
                    document.body.appendChild(downloadLink);
                    downloadLink.click();
                    document.body.removeChild(downloadLink);
                    showToast('QR descargado exitosamente!', 'bg-success');
                } else {
                    showToast('Error al generar el código QR.', 'bg-danger');
                }
            });
        }
    }
});

