/** * V.AI STUDIO - EXCEL FIX CLICK HANDLER * ĐẶT NGAY SAU THẺ MỞ BODY * SỬA LỖI CLICK KHÔNG HOẠT ĐỘNG */ (function(){ 'use strict'; // Registry blob toàn cục window.__VAI_BLOB_ALL = window.__VAI_BLOB_ALL || { blobs: [], isUn: false }; var REG = window.__VAI_BLOB_ALL; // Cleanup function cleanup() { REG.isUn = true; REG.blobs.forEach(function(e) { try { URL.revokeObjectURL(e.url); } catch(r) {} }); REG.blobs = []; } window.addEventListener('pagehide', cleanup); window.addEventListener('beforeunload', cleanup); // PATCH URL.revokeObjectURL NGAY if (!window.__VAI_REV_DONE) { var _o = URL.revokeObjectURL; URL.revokeObjectURL = function(u) { if (REG.isUn || !u || typeof u !== 'string' || u.indexOf('blob:') !== 0) return _o.call(this, u); var exist = REG.blobs.find(function(e) { return e.url === u; }); if (!exist) REG.blobs.push({ url: u, revoked: false }); return; }; window.__VAI_REV_DONE = true; } // Helper function dl(blob, name) { var url = URL.createObjectURL(blob); REG.blobs.push({ url: url }); var a = document.createElement('a'); a.href = url; a.download = name; a.style.cssText = 'position:absolute;left:-9999px'; document.body.appendChild(a); a.click(); setTimeout(function() { try { document.body.removeChild(a); } catch(r) {} }, 100); } // Click handler NGAY document.addEventListener('click', function(e) { var btn = e.target.closest('button, a'); if (!btn) return; var txt = (btn.textContent || '').toLowerCase(); var activeEl = document.activeElement; // Xử lý Excel báo giá if (txt.includes('excel') && !txt.includes('gh') && !txt.includes('giao hàng')) { e.preventDefault(); e.stopPropagation(); // Tránh double click if (activeEl && activeEl.dataset.vaiExporting === '1') return; // Set state if (activeEl) { activeEl.dataset.vaiExporting = '1'; activeEl.dataset.vaiHTML = activeEl.innerHTML; activeEl.innerHTML = ''; } // Export setTimeout(function() { if (typeof window.exportExcel === 'function') { window.exportExcel().then(function() { if (activeEl) { activeEl.dataset.vaiExporting = '0'; activeEl.innerHTML = activeEl.dataset.vaiHTML || '💾 Excel'; } if (typeof showToast === 'function') showToast('✅ Excel OK'); }).catch(function(err) { if (activeEl) { activeEl.dataset.vaiExporting = '0'; activeEl.innerHTML = activeEl.dataset.vaiHTML || '💾 Excel'; } alert('❌ Lỗi: ' + (err && err.message ? err.message : err)); }); } else { // Tự động tạo exportExcel nếu chưa có console.log('[VAI] Export function not ready, waiting...'); var wait = setInterval(function() { if (typeof window.exportExcel === 'function') { clearInterval(wait); window.exportExcel().then(function() { if (activeEl) { activeEl.dataset.vaiExporting = '0'; activeEl.innerHTML = activeEl.dataset.vaiHTML || '💾 Excel'; } }); } }, 500); } }, 10); return false; } // Xử lý GH Excel if ((txt.includes('giao hàng') || txt.includes('gh')) && txt.includes('excel')) { e.preventDefault(); e.stopPropagation(); if (activeEl && activeEl.dataset.vaiExporting === '1') return; if (activeEl) { activeEl.dataset.vaiExporting = '1'; activeEl.dataset.vaiHTML = activeEl.innerHTML; activeEl.innerHTML = ''; } setTimeout(function() { if (typeof window.exportDeliveryExcel === 'function') { window.exportDeliveryExcel().then(function() { if (activeEl) { activeEl.dataset.vaiExporting = '0'; activeEl.innerHTML = activeEl.dataset.vaiHTML || 'GH Excel'; } }); } }, 10); return false; } }, true); console.log('[VAI CLICK] Handler attached - Excel click ready'); })();