File size: 6,574 Bytes
9ffee8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * 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');
	    // Auto-apply discount/fees/notes from prompt text
	    if(typeof window.applyAiDiscount === 'function'){
	        setTimeout(function(){ window.applyAiDiscount(); }, 300);
	    } else {
	        // Fallback: trigger input on qcAiPrompt
	        var inp = document.getElementById('qcAiPrompt');
	        if(inp) { inp.dispatchEvent(new Event('input', {bubbles: true})); }
	    }
	    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(typeof window.applyAiDiscount === 'function'){ setTimeout(function(){ window.applyAiDiscount(); }, 300); }
	        }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');
})();