<aside class="sidebar">
    <div class="sidebar-header">
        <div class="logo sidebar-logo-text">Aegis<span>One</span></div>
        <div style="font-size:12px;color:var(--text-muted);letter-spacing:0.05em;">Service Portal</div>
    </div>
    <nav class="sidebar-nav">
        <div class="sidebar-section">Панель</div>
        <a href="/service/dashboard" class="{% if active_page == 'dashboard' %}active{% endif %}">Дашборд</a>
        {% if role_permissions.get('charts', True) != False %}<a href="/service/charts" class="{% if active_page == 'charts' %}active{% endif %}">Графики</a>{% endif %}
        {% if role_permissions.get('ceo', True) != False %}<a href="/service/ceo" class="{% if active_page == 'ceo' %}active{% endif %}">CEO дашборд</a>{% endif %}

        <div class="sidebar-section">Управление</div>
        {% if role_permissions.get('customers', True) != False %}<a href="/service/customers" class="{% if active_page == 'customers' %}active{% endif %}">Клиенты</a>{% endif %}
        {% if role_permissions.get('objects', True) != False %}<a href="/service/objects" class="{% if active_page == 'objects' %}active{% endif %}">Объекты</a>{% endif %}
        {% if role_permissions.get('sla', True) != False %}<a href="/service/sla" class="{% if active_page == 'sla' %}active{% endif %}">SLA контракты</a>{% endif %}
        {% if role_permissions.get('questionnaire', True) != False %}<a href="/service/questionnaire" class="{% if active_page == 'questionnaire' %}active{% endif %}">Опросник</a>{% endif %}
        {% if role_permissions.get('passports', True) != False %}<a href="/service/passports" class="{% if active_page == 'passports' %}active{% endif %}">Паспорта объектов</a>{% endif %}

        <div class="sidebar-section">Работа</div>
        {% if role_permissions.get('users', True) != False %}<a href="/service/users" class="{% if active_page == 'users' %}active{% endif %}">Сотрудники</a>{% endif %}
        {% if role_permissions.get('assignments', True) != False %}<a href="/service/assignments" class="{% if active_page == 'assignments' %}active{% endif %}">Назначение сотрудников</a>{% endif %}
        {% if role_permissions.get('docs', True) != False %}<a href="/service/documents/" class="{% if active_page == 'docs' %}active{% endif %}">Документация</a>{% endif %}
        {% if role_permissions.get('tasks', True) != False %}<a href="/service/tasks" class="{% if active_page == 'tasks' %}active{% endif %}">Задачи</a>{% endif %}
        {% if role_permissions.get('reports', True) != False %}<a href="/service/reports" class="{% if active_page == 'reports' %}active{% endif %}">Отчёты</a>{% endif %}
        {% if role_permissions.get('incidents', True) != False %}<a href="/service/incidents" class="{% if active_page == 'incidents' %}active{% endif %}">Инциденты</a>{% endif %}
        {% if role_permissions.get('checklist', True) != False %}<a href="/service/checklist" class="{% if active_page == 'checklist' %}active{% endif %}">Чек-лист</a>{% endif %}
        {% if role_permissions.get('tech_access', True) != False %}<a href="/service/documents/tech-access" class="{% if active_page == 'tech-access' %}active{% endif %}">Доступ техников</a>{% endif %}

        <div class="sidebar-section">Чат-бот MAX</div>
        {% if role_permissions.get('bot_consent', True) != False %}<a href="/service/bot-consent" class="{% if active_page == 'bot-consent' %}active{% endif %}">Согласия ФЗ-152</a>{% endif %}
        {% if role_permissions.get('bot_tickets', True) != False %}<a href="/service/bot-tickets" class="{% if active_page == 'bot-tickets' %}active{% endif %}">Заявки от бота</a>{% endif %}
        {% if role_permissions.get('bot_kb', True) != False %}<a href="/service/bot-kb" class="{% if active_page == 'bot-kb' %}active{% endif %}">База знаний</a>{% endif %}
        {% if role_permissions.get('bot_unknown', True) != False %}<a href="/service/bot-unknown" class="{% if active_page == 'bot-unknown' %}active{% endif %}">Неизвестные вопросы</a>{% endif %}
        {% if role_permissions.get('bot_dialogues', True) != False %}<a href="/service/bot-dialogues" class="{% if active_page == 'bot-dialogues' %}active{% endif %}">Диалоги</a>{% endif %}

        <div class="sidebar-section">Контент</div>
        {% if role_permissions.get('blog', True) != False %}<a href="/service/blog" class="{% if active_page == 'blog' %}active{% endif %}">Управление блогом</a>{% endif %}
        {% if role_permissions.get('cases', True) != False %}<a href="/service/cases" class="{% if active_page == 'cases' %}active{% endif %}">Примеры из практики</a>{% endif %}

        <div class="sidebar-section">Внешние системы</div>
        <a href="https://git.aegisone.ru" target="_blank" rel="noopener">Gitea</a>
        <a href="{{ PORTAINER_URL }}" target="_blank" rel="noopener">Portainer</a>

        <div class="sidebar-section">Настройки</div>
        <a href="/service/questionnaire-config" class="{% if active_page == 'questionnaire-config' %}active{% endif %}">Настройка опросника</a>
        <a href="/service/formulas" class="{% if active_page == 'formulas' %}active{% endif %}">Формулы</a>
        <a href="/service/portal-settings" class="{% if active_page == 'portal-settings' %}active{% endif %}">Настройка портала</a>
        <a href="/service/role-settings" class="{% if active_page == 'role-settings' %}active{% endif %}">Настройка Ролей</a>
        {% if role_permissions.get('bot_settings', True) != False %}<a href="/service/bot-settings" class="{% if active_page == 'bot-settings' %}active{% endif %}">Настройки чат-бота</a>{% endif %}
        <a href="/service/ideas" class="{% if active_page == 'ideas' %}active{% endif %}">Идеи</a>
    </nav>
    <div class="sidebar-footer">
        <div class="user-name">{{ user.full_name }}</div>
        <a href="/service/logout" style="display:inline-block;margin:6px 0;font-size:12px;">Выйти</a>
        <div class="version-pill" onclick="openChangelog()" title="История изменений">v{{ app_version }}</div>
    </div>
</aside>
<div class="modal-overlay" id="changelog-modal"><div class="modal">
    <button type="button" class="modal-close" onclick="closeChangelog()">✕</button>
    <h2>История изменений</h2>
    <div id="changelog-content" class="doc-content" style="padding:0;background:transparent;border:none;"></div>
</div></div>

<script>
function openChangelog(){
    var modal = document.getElementById('changelog-modal');
    modal.classList.add('open');
    var content = document.getElementById('changelog-content');
    if(!content.dataset.loaded){
        content.innerHTML = '<div class="loading"><div class="spinner"></div></div>';
        fetch('/service/api/changelog').then(function(r){ return r.json() }).then(function(data){
            content.innerHTML = data.html || '<p>Нет данных</p>';
            content.dataset.loaded = '1';
        });
    }
}
function closeChangelog(){
    document.getElementById('changelog-modal').classList.remove('open');
}
document.addEventListener('DOMContentLoaded', function(){
    var nav = document.querySelector('.navbar__menu');
    if(!nav) return;
    var sidebarLinks = document.querySelectorAll('.sidebar-nav a[href]');
    var saved = JSON.parse(localStorage.getItem('sidebar_favorites') || '[]');
    sidebarLinks.forEach(function(link){
        var star = document.createElement('span');
        star.className = 'sidebar-star';
        var url = link.getAttribute('href');
        star.textContent = saved.indexOf(url) !== -1 ? '\u2605' : '\u2606';
        star.style.cssText = 'cursor:pointer;margin-right:6px;font-size:14px;user-select:none;';
        star.addEventListener('click', function(e){
            e.preventDefault(); e.stopPropagation();
            var favs = JSON.parse(localStorage.getItem('sidebar_favorites') || '[]');
            var idx = favs.indexOf(url);
            if(idx !== -1){ favs.splice(idx,1); star.textContent = '\u2606'; }
            else { favs.push(url); star.textContent = '\u2605'; }
            localStorage.setItem('sidebar_favorites', JSON.stringify(favs));
            renderFavorites();
        });
        link.insertBefore(star, link.firstChild);
    });
    function renderFavorites(){
        var existing = nav.querySelectorAll('.nav-fav');
        existing.forEach(function(e){ e.remove(); });
        var favs = JSON.parse(localStorage.getItem('sidebar_favorites') || '[]');
        favs.forEach(function(url){
            var orig = document.querySelector('.sidebar-nav a[href="' + url + '"]');
            if(!orig) return;
            var li = document.createElement('li');
            li.className = 'nav-fav';
            var a = document.createElement('a');
            a.href = url; a.textContent = orig.textContent.replace(/^[☆★]/, '').trim();
            li.appendChild(a);
            nav.insertBefore(li, nav.lastElementChild);
        });
    }
    renderFavorites();
    var sidebarNav = document.querySelector('.sidebar-nav');
    var savedScroll = sessionStorage.getItem('sidebar_scroll');
    if (sidebarNav && savedScroll) sidebarNav.scrollTop = parseInt(savedScroll, 10);
    sidebarNav && sidebarNav.addEventListener('click', function(){
        sessionStorage.setItem('sidebar_scroll', this.scrollTop);
    });
});
</script>