';return}
var chNames={wa_nsk:'\u041d\u0421\u041a',wa_alm:'\u0410\u041b\u041c',max_ru:'Max'};
el.innerHTML=_prompts.map(function(p){
var chs=(p.channels||[]).map(function(c){return chNames[c]||c}).join(', ');
return '
';
}).join('');
}
function aNewPrompt(){_editPrId=null;document.getElementById('aPrName').value='';document.getElementById('aPrText').value='';document.getElementById('aPrTech').value='';document.getElementById('aPrModel').value='gpt-4.1-mini';document.querySelectorAll('#aPrChans input').forEach(function(cb){cb.checked=false});document.getElementById('aPromptEditor').style.display='block'}
function aEditPrompt(id){
var p=_prompts.find(function(x){return x.id===id});if(!p)return;_editPrId=id;
document.getElementById('aPrName').value=p.name||'';
document.getElementById('aPrText').value=p.text||'';
document.getElementById('aPrTech').value=p.tech_prompt||'';
document.getElementById('aPrModel').value=p.model||'gpt-4.1-mini';
// Set channel checkboxes
var chs=p.channels||[];
document.querySelectorAll('#aPrChans input').forEach(function(cb){cb.checked=chs.indexOf(cb.value)>=0});
document.getElementById('aPromptEditor').style.display='block';
}
function aDelPrompt(id){api('/api/cc/prompts/'+id,{method:'DELETE'}).then(function(){loadPrompts()})}
function aSavePrompt(){
var name=document.getElementById('aPrName').value.trim();
var text=document.getElementById('aPrText').value.trim();
var tech=document.getElementById('aPrTech').value.trim();
var model=document.getElementById('aPrModel').value;
var chs=[];document.querySelectorAll('#aPrChans input:checked').forEach(function(cb){chs.push(cb.value)});
if(!name)return;
var list=_prompts.slice();
if(_editPrId){list=list.map(function(p){return p.id===_editPrId?Object.assign({},p,{name:name,text:text,tech_prompt:tech,model:model,channels:chs}):p})}
else{list.push({id:'pr_'+Date.now(),name:name,text:text,tech_prompt:tech,model:model,channels:chs})}
api('/api/cc/prompts',{method:'POST',body:JSON.stringify(list)}).then(function(){document.getElementById('aPromptEditor').style.display='none';loadPrompts()});
}
// --- Bot Log ---
function loadBotLog(){
document.getElementById('aBotLog').innerHTML='';
api('/api/cc/bot_log').then(function(d){
var el=document.getElementById('aBotLog');
var logs=Array.isArray(d)?d:d.logs||[];
if(!logs.length){el.innerHTML='
'});
}
// --- Rules (full CRUD) ---
var _rules2=[];
function loadAdminRules2(){
api('/api/cc/rules').then(function(d){_rules2=Array.isArray(d)?d:[];renderRules2()}).catch(function(){});
api('/api/cc/variables').then(function(d){_ruleVars=Array.isArray(d)?d:[]}).catch(function(){});
}
function renderRules2(){
var el=document.getElementById('aRulesList2');
if(!_rules2.length){el.innerHTML='
'});
}
// --- History ---
function loadAdminHistory(){
document.getElementById('aHistoryList').innerHTML='';
api('/api/cc/notification_log?limit=100').then(function(d){
var arr=Array.isArray(d)?d:[];
if(!arr.length){document.getElementById('aHistoryList').innerHTML='
'});
}
// --- Team (employees + QR) ---
var _aEmp=[];
function loadAdminEmp(){api('/api/chat/employees').then(function(d){_aEmp=Array.isArray(d)?d:[];renderAEmp()}).catch(function(){})}
function renderAEmp(){
var el=document.getElementById('aEmpList');
if(!_aEmp.length){el.innerHTML='
\u041d\u0435\u0442
';return}
el.innerHTML=_aEmp.map(function(e,i){
var b=e.role==='admin'?'\u0430\u0434\u043c\u0438\u043d':'\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440';
return '
';
}).join('');
}
function aDelEmp(i){_aEmp.splice(i,1);api('/api/chat/employees',{method:'POST',body:JSON.stringify(_aEmp)}).then(function(){renderAEmp()})}
function aAddEmp(){
var n=document.getElementById('aEmpName').value.trim(),p=document.getElementById('aEmpPin').value.trim();
if(!n||p.length!==4)return;var chs=[];document.querySelectorAll('#aEmpChans input:checked').forEach(function(c){chs.push(c.value)});if(!chs.length)return;
_aEmp.push({id:'emp_'+Date.now(),name:n,pin:p,channels:chs,role:document.getElementById('aEmpRole').value});
api('/api/chat/employees',{method:'POST',body:JSON.stringify(_aEmp)}).then(function(){renderAEmp()});
document.getElementById('aEmpName').value='';document.getElementById('aEmpPin').value='';document.querySelectorAll('#aEmpChans input').forEach(function(c){c.checked=false});
}
var _aQR=[];
function loadAdminQR(){api('/api/cc/quick_replies').then(function(d){_aQR=Array.isArray(d)?d:[];renderAQR()}).catch(function(){})}
function renderAQR(){
var el=document.getElementById('aQRList');
if(!_aQR.length){el.innerHTML='
'}).join('');
}
function aDelQR(i){_aQR.splice(i,1);api('/api/cc/quick_replies',{method:'POST',body:JSON.stringify(_aQR)}).then(function(){renderAQR();QR=_aQR;renderQRChips()})}
function aAddQR(){
var t=document.getElementById('aQRTitle').value.trim(),x=document.getElementById('aQRText').value.trim();if(!t||!x)return;
_aQR.push({id:'qr_'+Date.now(),title:t,text:x});
api('/api/cc/quick_replies',{method:'POST',body:JSON.stringify(_aQR)}).then(function(){renderAQR();QR=_aQR;renderQRChips()});
document.getElementById('aQRTitle').value='';document.getElementById('aQRText').value='';
}
function resolveChat(){
if(!ACTIVE_CHAT)return;
var btn=document.getElementById('resolveBtn');
var currentState=CHATS.find(function(c){return c.chat_id===ACTIVE_CHAT.id&&c.channel===ACTIVE_CHAT.channel});
var isResolved=currentState&¤tState.chat_state==='resolved';
var action=isResolved?'reopen':'resolve';
api('/api/cc/chat_state',{method:'POST',body:JSON.stringify({chat_id:ACTIVE_CHAT.id,channel:ACTIVE_CHAT.channel,action:action})}).then(function(){
if(action==='resolve'){btn.textContent='\u21bb \u041e\u0442\u043a\u0440\u044b\u0442\u044c';btn.style.borderColor='rgba(255,255,255,.5)'}
else{btn.textContent='\u2713 \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c';btn.style.borderColor='rgba(255,255,255,.3)'}
loadChats();
});
}
var _ruleVars=[];
function renderVarChips(){
var el=document.getElementById('aVarChips');if(!el)return;
el.innerHTML=_ruleVars.map(function(v){
var vn=typeof v==='object'?(v['var']||'').replace(/[{}]/g,''):v;
var desc=typeof v==='object'?(v.desc||vn):v;
return '{'+vn+'}';
}).join('');
}
function aInsertVar(vn){
var ta=document.getElementById('aRuleTemplate');if(!ta)return;
var s=ta.selectionStart,v=ta.value;
ta.value=v.substring(0,s)+'{'+vn+'}'+v.substring(ta.selectionEnd);
ta.focus();ta.selectionStart=ta.selectionEnd=s+vn.length+2;
}
function aTestRule(){
var tpl=document.getElementById('aRuleTemplate').value;
api('/api/cc/rules/test',{method:'POST',body:JSON.stringify({template:tpl})}).then(function(r){
document.getElementById('aRulePreview').innerHTML='
'+escH(r.rendered||r.preview||tpl)+'
';
}).catch(function(){});
}
function aAiGenRule(){
var input=document.getElementById('aAiRuleInput').value.trim();if(!input)return;
var btn=document.getElementById('aAiRuleBtn');btn.disabled=true;
document.getElementById('aAiRuleStatus').textContent='Generating...';
api('/api/cc/ai_generate',{method:'POST',body:JSON.stringify({prompt:input,type:'rule'})}).then(function(r){
if(r.name)document.getElementById('aRuleName').value=r.name;
if(r.doc_type)document.getElementById('aRuleDocType').value=r.doc_type;
if(r.channel)document.getElementById('aRuleChannel').value=r.channel;
if(r.message_template)document.getElementById('aRuleTemplate').value=r.message_template;
if(r.conditions){document.getElementById('aRuleConds').innerHTML='';r.conditions.forEach(function(c){aRenderCond(c)})}
document.getElementById('aAiRuleStatus').textContent='Done!';
}).catch(function(e){document.getElementById('aAiRuleStatus').textContent='Error'}).then(function(){btn.disabled=false});
}