⚙ Настройки

📨Авто
🔔Правила
📡Каналы
📋История
👥Команда
📨 Простой автоответ
Раб.время:
🌙 Нераб.время
👋 Приветствие
✅ Завершение
🤖 ИИ-бот
Модель:
Тест. номера:
Промпты
📚 Лог бота
Нажмите Обновить
Правила уведомлений
Каналы связи
История отправок
👥 Сотрудники
⚡ Быстрые ответы
// ============ ADMIN PANEL ============ var _adTab='auto'; function showAdmin(){show('screenAdmin');showAdTab('auto')} function showAdTab(tab){ _adTab=tab; document.querySelectorAll('.adtab').forEach(function(t,i){t.classList.toggle('active',['auto','rules','channels','history','team'][i]===tab)}); document.querySelectorAll('.adpane').forEach(function(p){p.classList.remove('active')}); var map={auto:'adAuto',rules:'adRules',channels:'adChannels',history:'adHistory',team:'adTeam'}; var el=document.getElementById(map[tab]);if(el)el.classList.add('active'); if(tab==='auto'){loadAdminSAR();loadAiBot();loadBotLog()} else if(tab==='rules')loadAdminRules2(); else if(tab==='channels')loadAdminChannels(); else if(tab==='history')loadAdminHistory(); else if(tab==='team'){loadAdminEmp();loadAdminQR()} } // --- Simple Autoresponder --- function loadAdminSAR(){ api('/api/cc/simple_autoresponder').then(function(d){ document.getElementById('aSarOn').checked=d.enabled||false; document.getElementById('aWF').value=d.work_from||'10:00';document.getElementById('aWT').value=d.work_to||'21:00'; var oh=d.off_hours||{};document.getElementById('aOffOn').checked=oh.enabled||false;document.getElementById('aOffMsg').value=oh.message||''; var gr=d.greeting||{};document.getElementById('aGreetOn').checked=gr.enabled||false;document.getElementById('aGreetMsg').value=gr.message||''; var cl=d.closing||{};document.getElementById('aCloseOn').checked=cl.enabled||false;document.getElementById('aCloseMsg').value=cl.message||''; }).catch(function(){}); } function saveAdminSAR(){ api('/api/cc/simple_autoresponder',{method:'POST',body:JSON.stringify({ enabled:document.getElementById('aSarOn').checked,work_from:document.getElementById('aWF').value,work_to:document.getElementById('aWT').value,timezone_offset:7, off_hours:{enabled:document.getElementById('aOffOn').checked,channel:'cascade_ru',message:document.getElementById('aOffMsg').value}, greeting:{enabled:document.getElementById('aGreetOn').checked,channel:'cascade_ru',message:document.getElementById('aGreetMsg').value}, closing:{enabled:document.getElementById('aCloseOn').checked,timeout_minutes:30,channel:'cascade_ru',message:document.getElementById('aCloseMsg').value} })}); } // --- AI Bot --- var _aiData={}; function loadAiBot(){ api('/api/cc/autoresponder').then(function(d){ _aiData=d; document.getElementById('aAiOn').checked=d.enabled||false; document.getElementById('aAiTest').checked=d.test_mode!==false; document.getElementById('aAiModel').value=d.model||'gpt-4.1-mini'; var ph=(d.test_phones||[]).map(function(p){return ''+escH(p)+' x'}).join(' '); document.getElementById('aAiPhones').innerHTML=ph; loadPrompts(); }).catch(function(){}); } function saveAiBot(){ var data=Object.assign({},_aiData,{ enabled:document.getElementById('aAiOn').checked, test_mode:document.getElementById('aAiTest').checked, model:document.getElementById('aAiModel').value }); api('/api/cc/autoresponder',{method:'POST',body:JSON.stringify(data)}); } function aAiAddPhone(){var p=prompt('\u041d\u043e\u043c\u0435\u0440 (\u0431\u0435\u0437 +):');if(!p)return;if(!_aiData.test_phones)_aiData.test_phones=[];_aiData.test_phones.push(p.trim());saveAiBot();loadAiBot()} function aAiDelPhone(p){if(!_aiData.test_phones)return;_aiData.test_phones=_aiData.test_phones.filter(function(x){return x!==p});saveAiBot();loadAiBot()} // --- Prompts --- var _prompts=[],_editPrId=null; function loadPrompts(){ api('/api/cc/prompts').then(function(d){_prompts=Array.isArray(d)?d:[];renderPrompts()}).catch(function(){}); } function renderPrompts(){ var el=document.getElementById('aPromptList'); if(!_prompts.length){el.innerHTML='
\u041d\u0435\u0442 \u043f\u0440\u043e\u043c\u043f\u0442\u043e\u0432
';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 '
'+escH(p.name||p.id)+''+escH(chs||'\u0432\u0441\u0435')+''+escH((p.text||'').substring(0,30))+'
'; }).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='
\u041f\u0443\u0441\u0442\u043e
';return} el.innerHTML=logs.slice(-20).reverse().map(function(l){ return '
'+escH(l.from||'?')+': '+escH((l.question||l.text||'').substring(0,80))+'
'+(l.answer?'
\u2192 '+escH(l.answer.substring(0,80))+'
':'')+'
'; }).join(''); }).catch(function(){document.getElementById('aBotLog').innerHTML='
\u041e\u0448\u0438\u0431\u043a\u0430
'}); } // --- 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='
\u041d\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b
';return} el.innerHTML=_rules2.map(function(r){ return '
'+escH(r.name||r.id)+''+(r.enabled?'\u0412\u041a\u041b':'\u0412\u042b\u041a\u041b')+'
'; }).join(''); } function aNewRule(){ document.getElementById('aRuleId').value='';document.getElementById('aRuleName').value=''; document.getElementById('aRuleDocType').value='customerorder';document.getElementById('aRuleTemplate').value=''; document.getElementById('aRuleDelay').value='immediate';document.getElementById('aRuleDelayVal').value='1'; document.getElementById('aRuleWorkH').checked=false;document.getElementById('aRuleEnabled').checked=true; document.getElementById('aRuleConds').innerHTML='';document.getElementById('aRuleEdTitle').textContent='\u041d\u043e\u0432\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e'; renderVarChips(); document.getElementById('aRuleEditor').style.display='block'; } function aEditRule(id){ var r=_rules2.find(function(x){return x.id===id});if(!r)return; document.getElementById('aRuleId').value=r.id;document.getElementById('aRuleName').value=r.name||''; document.getElementById('aRuleDocType').value=r.doc_type||'customerorder'; document.getElementById('aRuleTemplate').value=r.message_template||''; document.getElementById('aRuleDelay').value=r.delay_type||'immediate'; document.getElementById('aRuleDelayVal').value=r.delay_value||1; document.getElementById('aRuleWorkH').checked=r.work_hours_only||false; document.getElementById('aRuleEnabled').checked=r.enabled!==false; document.getElementById('aRuleEdTitle').textContent='\u0420\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435'; // Render conditions var el=document.getElementById('aRuleConds');el.innerHTML=''; (r.conditions||[]).forEach(function(c,i){aRenderCond(c)}); renderVarChips(); document.getElementById('aRuleEditor').style.display='block'; } function aAddCond(){aRenderCond({type:'state',value:''})} function aRenderCond(c){ var el=document.getElementById('aRuleConds'); var div=document.createElement('div');div.className='cond-row'; div.innerHTML=''; el.appendChild(div); } function aSaveRule(){ var id=document.getElementById('aRuleId').value||('rule_'+Date.now()); var conds=[];document.querySelectorAll('#aRuleConds .cond-row').forEach(function(row){ var t=row.querySelector('.cond-type').value,v=row.querySelector('input').value.trim(); if(v)conds.push({type:t,value:v}); }); var rule={ id:id,name:document.getElementById('aRuleName').value.trim(), doc_type:document.getElementById('aRuleDocType').value, conditions:conds,channel:document.getElementById('aRuleChannel').value, message_template:document.getElementById('aRuleTemplate').value, delay_type:document.getElementById('aRuleDelay').value, delay_value:parseInt(document.getElementById('aRuleDelayVal').value)||1, work_hours_only:document.getElementById('aRuleWorkH').checked, enabled:document.getElementById('aRuleEnabled').checked, work_from:'09:00',work_to:'21:00' }; api('/api/cc/rules',{method:'POST',body:JSON.stringify(rule)}).then(function(){ document.getElementById('aRuleEditor').style.display='none';loadAdminRules2(); }); } function aDeleteRule(id){if(!confirm('\u0423\u0434\u0430\u043b\u0438\u0442\u044c?'))return;api('/api/cc/rules/'+id,{method:'DELETE'}).then(function(){loadAdminRules2()})} // --- Channels --- function loadAdminChannels(){ document.getElementById('aChannelsList').innerHTML='
'; api('/api/cc/channels').then(function(d){ var arr=Array.isArray(d)?d:[]; var names={wa_nsk:'WA \u041d\u043e\u0432\u043e\u0441\u0438\u0431\u0438\u0440\u0441\u043a',wa_alm:'WA \u0410\u043b\u043c\u0430\u0442\u044b',max_ru:'Max \u0420\u043e\u0441\u0441\u0438\u044f'}; document.getElementById('aChannelsList').innerHTML=arr.map(function(c){ var ok=c.status==='authorized'; return '
'+(names[c.key]||c.name||c.key)+'
ID: '+escH(c.instance_id||'?')+' | '+(c.phone||'\u043d\u0435\u0442 \u0442\u0435\u043b.')+' | '+escH(c.status)+'
'; }).join('')||'
\u041d\u0435\u0442 \u043a\u0430\u043d\u0430\u043b\u043e\u0432
'; }).catch(function(){document.getElementById('aChannelsList').innerHTML='
\u041e\u0448\u0438\u0431\u043a\u0430
'}); } // --- 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='
\u041d\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043e\u043a
';return} document.getElementById('aHistoryList').innerHTML=arr.map(function(h){ var ok=h.status==='sent'; return '
'+escH(h.rule||'?')+''+escH((h.timestamp||'').substring(0,16))+'
'+escH(h.phone||'')+' | '+escH(h.status||'?')+'
'+escH((h.message||'').substring(0,60))+'
'; }).join(''); }).catch(function(){document.getElementById('aHistoryList').innerHTML='
\u041e\u0448\u0438\u0431\u043a\u0430
'}); } // --- 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 '
'+escH(e.name)+''+b+'PIN:'+escH(e.pin)+''+escH((e.channels||[]).join(', '))+'
'; }).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='
\u041f\u0443\u0441\u0442\u043e
';return} el.innerHTML=_aQR.map(function(q,i){return '
'+escH(q.title)+''+escH((q.text||'').substring(0,50))+'
'}).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}); }