V.AISTUDIO / quote-ui.js
bep40's picture
Revert quote-ui.js to bb80fef31b
d4ab6aa verified
/**
* V.AI STUDIO — Quote UI v7
*
* - Nút "💾 Lưu" LUÔN HIỆN (không cần mã truy cập) — lưu đơn cho mọi user
* - Nút "📋 Đơn hàng", "📦 GH Excel", "📦 GH PDF" ẨN cho đến khi nhập V.AISTUDIO
* - Khi Lưu → tự động lưu vào danh sách đơn hàng (VAI_ORDERS)
*/
(function(){
'use strict';
var ACCESS_CODE='V.AISTUDIO';
function isUnlocked(){
return document.body.classList.contains('vas-unlocked');
}
function unlock(){
document.body.classList.add('vas-unlocked');
console.log('[Quote UI v7] ✅ Unlocked');
setTimeout(addProtectedButtons,200);
}
function lock(){
document.body.classList.remove('vas-unlocked');
removeProtectedButtons();
}
// === NÚT LƯU (LUÔN HIỆN) ===
function hasSaveButton(){
return !!document.querySelector('[data-vai-save-btn]');
}
function addSaveButton(){
if(hasSaveButton())return;
var actionsArea=document.querySelector('.quote-actions');
if(!actionsArea)return;
var btn=document.createElement('button');
btn.setAttribute('data-vai-save-btn','1');
btn.className='quote-btn'; // KHÔNG có checkout-done-btn → luôn hiện
btn.innerHTML='💾 Lưu';
btn.style.cssText='padding:12px 20px;border:none;border-radius:10px;font-weight:700;font-size:.82rem;cursor:pointer;display:flex;align-items:center;gap:6px;transition:all .25s ease;white-space:nowrap;background:#7c3aed;color:#fff';
btn.onmouseenter=function(){this.style.background='#6d28d9';};
btn.onmouseleave=function(){this.style.background='#7c3aed';};
btn.onclick=function(){
if(window.VAI_ORDERS&&window.VAI_ORDERS.save){
window.VAI_ORDERS.save();
}else{
alert('⏳ Đang tải module đơn hàng...');
}
};
// Insert as first button in actions
actionsArea.insertBefore(btn, actionsArea.firstChild);
console.log('[Quote UI v7] 💾 Lưu button added (always visible)');
}
// === CÁC NÚT BẢO VỆ (ẨN CHO ĐẾN KHI UNLOCK) ===
function hasProtectedButtons(){
return !!document.querySelector('[data-vai-protected-btn]');
}
function removeProtectedButtons(){
document.querySelectorAll('[data-vai-protected-btn]').forEach(function(b){b.remove();});
}
function addProtectedButtons(){
if(!isUnlocked())return;
if(hasProtectedButtons())return;
var actionsArea=document.querySelector('.quote-actions');
if(!actionsArea)return;
var btnStyle='padding:12px 20px;border:none;border-radius:10px;font-weight:700;font-size:.82rem;cursor:pointer;display:flex;align-items:center;gap:6px;transition:all .25s ease;white-space:nowrap';
var buttons=[
{html:'📋 Đơn hàng',bg:'#2563eb',hover:'#1d4ed8',fn:function(){if(window.VAI_ORDERS)window.VAI_ORDERS.openSearch();else alert('⏳ Đang tải...');}},
{html:'📦 GH Excel',bg:'#059669',hover:'#047857',fn:function(){if(typeof exportDeliveryExcel==='function')exportDeliveryExcel();}},
{html:'📦 GH PDF',bg:'#0d9488',hover:'#0f766e',fn:function(){if(typeof exportDeliveryPDF==='function')exportDeliveryPDF();}}
];
buttons.forEach(function(cfg){
var btn=document.createElement('button');
btn.setAttribute('data-vai-protected-btn','1');
btn.className='quote-btn checkout-done-btn'; // ẨN cho đến khi vas-unlocked
btn.innerHTML=cfg.html;
btn.style.cssText=btnStyle+';background:'+cfg.bg+';color:#fff';
btn.onmouseenter=function(){this.style.background=cfg.hover;};
btn.onmouseleave=function(){this.style.background=cfg.bg;};
btn.onclick=cfg.fn;
actionsArea.appendChild(btn);
});
console.log('[Quote UI v7] Protected buttons injected (Đơn hàng, GH)');
}
function isQuoteOpen(){
var ov=document.querySelector('.quote-overlay');
return ov&&(ov.classList.contains('open')||ov.style.display==='flex'||ov.style.display==='block');
}
// === BIND mã truy cập input (#qcAccessCode) ===
function bindAccessCodeInput(){
var accessInput=document.getElementById('qcAccessCode');
if(!accessInput)return false;
if(accessInput.getAttribute('data-vai-access'))return true;
accessInput.setAttribute('data-vai-access','1');
function checkCode(){
var val=(accessInput.value||'').trim();
if(val===ACCESS_CODE||val.toUpperCase()===ACCESS_CODE){
if(!isUnlocked())unlock();
}else{
if(isUnlocked())lock();
}
}
accessInput.addEventListener('input',checkCode);
accessInput.addEventListener('change',checkCode);
accessInput.addEventListener('blur',checkCode);
accessInput.addEventListener('keypress',function(e){if(e.key==='Enter')checkCode();});
return true;
}
// === OBSERVER ===
var observer=new MutationObserver(function(){
bindAccessCodeInput();
if(isQuoteOpen()){
if(!hasSaveButton())setTimeout(addSaveButton,200);
if(isUnlocked()&&!hasProtectedButtons())setTimeout(addProtectedButtons,300);
}
if(!isQuoteOpen()){
if(hasSaveButton())document.querySelectorAll('[data-vai-save-btn]').forEach(function(b){b.remove();});
if(hasProtectedButtons())removeProtectedButtons();
}
if(!isUnlocked()&&hasProtectedButtons())removeProtectedButtons();
});
observer.observe(document.body,{childList:true,subtree:true,attributes:true,attributeFilter:['class','style']});
setInterval(function(){
bindAccessCodeInput();
if(isQuoteOpen()){
if(!hasSaveButton())addSaveButton();
if(isUnlocked()&&!hasProtectedButtons())addProtectedButtons();
}
if(!isQuoteOpen()){
if(hasSaveButton())document.querySelectorAll('[data-vai-save-btn]').forEach(function(b){b.remove();});
if(hasProtectedButtons())removeProtectedButtons();
}
if(!isUnlocked()&&hasProtectedButtons())removeProtectedButtons();
},2000);
setTimeout(bindAccessCodeInput,1000);
setTimeout(bindAccessCodeInput,3000);
console.log('[Quote UI v7] loaded — Lưu always visible, protected buttons need V.AISTUDIO');
})();