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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* V.AI STUDIO — Quote UI v7
|
| 3 |
+
*
|
| 4 |
+
* - Nút "💾 Lưu" LUÔN HIỆN (không cần mã truy cập) — lưu đơn cho mọi user
|
| 5 |
+
* - Nút "📋 Đơn hàng", "📦 GH Excel", "📦 GH PDF" ẨN cho đến khi nhập V.AISTUDIO
|
| 6 |
+
* - Khi Lưu → tự động lưu vào danh sách đơn hàng (VAI_ORDERS)
|
| 7 |
+
*/
|
| 8 |
+
(function(){
|
| 9 |
+
'use strict';
|
| 10 |
+
|
| 11 |
+
var ACCESS_CODE='V.AISTUDIO';
|
| 12 |
+
|
| 13 |
+
function isUnlocked(){
|
| 14 |
+
return document.body.classList.contains('vas-unlocked');
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
function unlock(){
|
| 18 |
+
document.body.classList.add('vas-unlocked');
|
| 19 |
+
console.log('[Quote UI v7] ✅ Unlocked');
|
| 20 |
+
// Auto-apply discount/fees/notes from prompt text
|
| 21 |
+
if(typeof window.applyAiDiscount === 'function'){
|
| 22 |
+
setTimeout(function(){ window.applyAiDiscount(); }, 300);
|
| 23 |
+
} else {
|
| 24 |
+
// Fallback: trigger input on qcAiPrompt
|
| 25 |
+
var inp = document.getElementById('qcAiPrompt');
|
| 26 |
+
if(inp) { inp.dispatchEvent(new Event('input', {bubbles: true})); }
|
| 27 |
+
}
|
| 28 |
+
setTimeout(addProtectedButtons, 200);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function lock(){
|
| 32 |
+
document.body.classList.remove('vas-unlocked');
|
| 33 |
+
removeProtectedButtons();
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// === NÚT LƯU (LUÔN HIỆN) ===
|
| 37 |
+
function hasSaveButton(){
|
| 38 |
+
return !!document.querySelector('[data-vai-save-btn]');
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
function addSaveButton(){
|
| 42 |
+
if(hasSaveButton())return;
|
| 43 |
+
var actionsArea=document.querySelector('.quote-actions');
|
| 44 |
+
if(!actionsArea)return;
|
| 45 |
+
|
| 46 |
+
var btn=document.createElement('button');
|
| 47 |
+
btn.setAttribute('data-vai-save-btn','1');
|
| 48 |
+
btn.className='quote-btn'; // KHÔNG có checkout-done-btn → luôn hiện
|
| 49 |
+
btn.innerHTML='💾 Lưu';
|
| 50 |
+
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';
|
| 51 |
+
btn.onmouseenter=function(){this.style.background='#6d28d9';};
|
| 52 |
+
btn.onmouseleave=function(){this.style.background='#7c3aed';};
|
| 53 |
+
btn.onclick=function(){
|
| 54 |
+
if(window.VAI_ORDERS&&window.VAI_ORDERS.save){
|
| 55 |
+
window.VAI_ORDERS.save();
|
| 56 |
+
}else{
|
| 57 |
+
alert('⏳ Đang tải module đơn hàng...');
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
// Insert as first button in actions
|
| 61 |
+
actionsArea.insertBefore(btn, actionsArea.firstChild);
|
| 62 |
+
console.log('[Quote UI v7] 💾 Lưu button added (always visible)');
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
// === CÁC NÚT BẢO VỆ (ẨN CHO ĐẾN KHI UNLOCK) ===
|
| 66 |
+
function hasProtectedButtons(){
|
| 67 |
+
return !!document.querySelector('[data-vai-protected-btn]');
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
function removeProtectedButtons(){
|
| 71 |
+
document.querySelectorAll('[data-vai-protected-btn]').forEach(function(b){b.remove();});
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
function addProtectedButtons(){
|
| 75 |
+
if(!isUnlocked())return;
|
| 76 |
+
if(hasProtectedButtons())return;
|
| 77 |
+
var actionsArea=document.querySelector('.quote-actions');
|
| 78 |
+
if(!actionsArea)return;
|
| 79 |
+
|
| 80 |
+
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';
|
| 81 |
+
|
| 82 |
+
var buttons=[
|
| 83 |
+
{html:'📋 Đơn hàng',bg:'#2563eb',hover:'#1d4ed8',fn:function(){if(window.VAI_ORDERS)window.VAI_ORDERS.openSearch();else alert('⏳ Đang tải...');}},
|
| 84 |
+
{html:'📦 GH Excel',bg:'#059669',hover:'#047857',fn:function(){if(typeof exportDeliveryExcel==='function')exportDeliveryExcel();}},
|
| 85 |
+
{html:'📦 GH PDF',bg:'#0d9488',hover:'#0f766e',fn:function(){if(typeof exportDeliveryPDF==='function')exportDeliveryPDF();}}
|
| 86 |
+
];
|
| 87 |
+
|
| 88 |
+
buttons.forEach(function(cfg){
|
| 89 |
+
var btn=document.createElement('button');
|
| 90 |
+
btn.setAttribute('data-vai-protected-btn','1');
|
| 91 |
+
btn.className='quote-btn checkout-done-btn'; // ẨN cho đến khi vas-unlocked
|
| 92 |
+
btn.innerHTML=cfg.html;
|
| 93 |
+
btn.style.cssText=btnStyle+';background:'+cfg.bg+';color:#fff';
|
| 94 |
+
btn.onmouseenter=function(){this.style.background=cfg.hover;};
|
| 95 |
+
btn.onmouseleave=function(){this.style.background=cfg.bg;};
|
| 96 |
+
btn.onclick=cfg.fn;
|
| 97 |
+
actionsArea.appendChild(btn);
|
| 98 |
+
});
|
| 99 |
+
console.log('[Quote UI v7] Protected buttons injected (Đơn hàng, GH)');
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
function isQuoteOpen(){
|
| 103 |
+
var ov=document.querySelector('.quote-overlay');
|
| 104 |
+
return ov&&(ov.classList.contains('open')||ov.style.display==='flex'||ov.style.display==='block');
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// === BIND mã truy cập input (#qcAccessCode) ===
|
| 108 |
+
function bindAccessCodeInput(){
|
| 109 |
+
var accessInput=document.getElementById('qcAccessCode');
|
| 110 |
+
if(!accessInput)return false;
|
| 111 |
+
if(accessInput.getAttribute('data-vai-access'))return true;
|
| 112 |
+
accessInput.setAttribute('data-vai-access','1');
|
| 113 |
+
|
| 114 |
+
function checkCode(){
|
| 115 |
+
var val=(accessInput.value||'').trim();
|
| 116 |
+
if(val===ACCESS_CODE||val.toUpperCase()===ACCESS_CODE){
|
| 117 |
+
if(!isUnlocked())unlock();
|
| 118 |
+
else if(typeof window.applyAiDiscount === 'function'){ setTimeout(function(){ window.applyAiDiscount(); }, 300); }
|
| 119 |
+
}else{
|
| 120 |
+
if(isUnlocked())lock();
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
accessInput.addEventListener('input',checkCode);
|
| 125 |
+
accessInput.addEventListener('change',checkCode);
|
| 126 |
+
accessInput.addEventListener('blur',checkCode);
|
| 127 |
+
accessInput.addEventListener('keypress',function(e){if(e.key==='Enter')checkCode();});
|
| 128 |
+
return true;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// === OBSERVER ===
|
| 132 |
+
var observer=new MutationObserver(function(){
|
| 133 |
+
bindAccessCodeInput();
|
| 134 |
+
if(isQuoteOpen()){
|
| 135 |
+
if(!hasSaveButton())setTimeout(addSaveButton,200);
|
| 136 |
+
if(isUnlocked()&&!hasProtectedButtons())setTimeout(addProtectedButtons,300);
|
| 137 |
+
}
|
| 138 |
+
if(!isQuoteOpen()){
|
| 139 |
+
if(hasSaveButton())document.querySelectorAll('[data-vai-save-btn]').forEach(function(b){b.remove();});
|
| 140 |
+
if(hasProtectedButtons())removeProtectedButtons();
|
| 141 |
+
}
|
| 142 |
+
if(!isUnlocked()&&hasProtectedButtons())removeProtectedButtons();
|
| 143 |
+
});
|
| 144 |
+
observer.observe(document.body,{childList:true,subtree:true,attributes:true,attributeFilter:['class','style']});
|
| 145 |
+
|
| 146 |
+
setInterval(function(){
|
| 147 |
+
bindAccessCodeInput();
|
| 148 |
+
if(isQuoteOpen()){
|
| 149 |
+
if(!hasSaveButton())addSaveButton();
|
| 150 |
+
if(isUnlocked()&&!hasProtectedButtons())addProtectedButtons();
|
| 151 |
+
}
|
| 152 |
+
if(!isQuoteOpen()){
|
| 153 |
+
if(hasSaveButton())document.querySelectorAll('[data-vai-save-btn]').forEach(function(b){b.remove();});
|
| 154 |
+
if(hasProtectedButtons())removeProtectedButtons();
|
| 155 |
+
}
|
| 156 |
+
if(!isUnlocked()&&hasProtectedButtons())removeProtectedButtons();
|
| 157 |
+
},2000);
|
| 158 |
+
|
| 159 |
+
setTimeout(bindAccessCodeInput,1000);
|
| 160 |
+
setTimeout(bindAccessCodeInput,3000);
|
| 161 |
+
|
| 162 |
+
console.log('[Quote UI v7] loaded — Lưu always visible, protected buttons need V.AISTUDIO');
|
| 163 |
+
})();
|