{#
    EwoooC Frontend V2 shell.

    使用方式：
        {% include 'components/_ewoooc_shell.html' %}

    呼叫頁需提供 active_page；未提供時會以空字串處理。
#}

{% set _active_page = active_page|default('') %}
{% set _scheduler = scheduler_stats|default({}) %}
{% set _momo_runs = _scheduler.get('momo_task', []) if _scheduler is mapping else [] %}
{% set _latest_run = _momo_runs[0] if _momo_runs else {} %}
{% set _has_scheduler_data = _latest_run is mapping and _latest_run %}
{% set _last_run = _latest_run.get('last_run', '--') if _has_scheduler_data else '--' %}
{% set _scanned = _latest_run.get('scanned_count', _latest_run.get('total_products', '--')) if _latest_run is mapping else '--' %}
{% set _added = _latest_run.get('new_records', _latest_run.get('added', '--')) if _latest_run is mapping else '--' %}
{% set _run_status = _latest_run.get('status', '') if _has_scheduler_data else '' %}
{% set _status_label = '尚無紀錄' if not _has_scheduler_data else ('最近成功' if _run_status in ['Success', 'success', 'SUCCESS'] else (_run_status or '已有紀錄')) %}
{% set _next_run = next_run|default(None) %}
{% set _session_username = session.get('username') if session is defined else None %}
{% set _session_role = session.get('role') if session is defined else None %}
{% set _is_logged_in = session.get('logged_in') if session is defined else false %}

<aside class="momo-sidebar" aria-label="主選單">
    <a class="momo-sidebar-logo" href="/">
        <span class="momo-logo-mark" aria-hidden="true">
            <span></span><span></span><span></span>
            <span></span><span></span><span></span>
            <span></span><span></span><span></span>
        </span>
        <span class="momo-brand-word">
            <span class="momo-brand-name momo-display">EwoooC</span>
            <span class="momo-brand-subtitle momo-label">價格監控 V2</span>
        </span>
    </a>

    <nav class="momo-nav momo-scroll">
        <div class="momo-nav-group">
            <div class="momo-nav-group-title momo-label">監控</div>
            <a class="momo-nav-link {% if _active_page == 'dashboard' %}is-active{% endif %}" href="/">
                <span class="momo-nav-icon"><i class="fas fa-border-all"></i></span>
                <span class="momo-nav-label">商品看板</span>
                <span class="momo-nav-code momo-mono">01</span>
            </a>
            <a class="momo-nav-link {% if _active_page in ['edm', 'campaigns'] %}is-active{% endif %}" href="/edm">
                <span class="momo-nav-icon"><i class="fas fa-bullhorn"></i></span>
                <span class="momo-nav-label">活動看板</span>
                <span class="momo-nav-code momo-mono">02</span>
            </a>
            <a class="momo-nav-link {% if _active_page in ['sales', 'daily_sales', 'monthly', 'growth'] %}is-active{% endif %}" href="/sales_analysis">
                <span class="momo-nav-icon"><i class="fas fa-chart-line"></i></span>
                <span class="momo-nav-label">分析報表</span>
                <span class="momo-nav-code momo-mono">03</span>
            </a>
        </div>

        <div class="momo-nav-group">
            <div class="momo-nav-group-title momo-label">營運</div>
            <a class="momo-nav-link {% if _active_page == 'vendor_stockout' %}is-active{% endif %}" href="/vendor-stockout">
                <span class="momo-nav-icon"><i class="fas fa-box-open"></i></span>
                <span class="momo-nav-label">廠商缺貨</span>
                <span class="momo-nav-code momo-mono">04</span>
            </a>
            <a class="momo-nav-link {% if _active_page in ['ai_recommend', 'ai_history', 'ai_intelligence'] %}is-active{% endif %}" href="/ai_recommend">
                <span class="momo-nav-icon"><i class="fas fa-wand-magic-sparkles"></i></span>
                <span class="momo-nav-label">AI 助手</span>
                <span class="momo-nav-code momo-mono">05</span>
            </a>
            <a class="momo-nav-link {% if _active_page == 'auto_import' %}is-active{% endif %}" href="/auto_import">
                <span class="momo-nav-icon"><i class="fas fa-download"></i></span>
                <span class="momo-nav-label">雲端匯入</span>
                <span class="momo-nav-code momo-mono">06</span>
            </a>
        </div>

        <div class="momo-nav-group">
            <div class="momo-nav-group-title momo-label">系統</div>
            <a class="momo-nav-link {% if _active_page in ['settings', 'system_settings', 'logs', 'crawler', 'user_management', 'ai_automation_smoke'] %}is-active{% endif %}" href="/settings">
                <span class="momo-nav-icon"><i class="fas fa-gear"></i></span>
                <span class="momo-nav-label">系統管理</span>
                <span class="momo-nav-code momo-mono">07</span>
            </a>
        </div>
    </nav>

    <div class="momo-status-card">
        <div class="momo-status-title momo-label">爬蟲狀態</div>
        <div class="momo-status-active momo-mono">
            <span class="momo-live-dot"></span>
            <span>{{ _status_label }}</span>
        </div>
        <div class="momo-status-meta momo-mono">
            上次執行 {{ _last_run }}<br>
            掃描筆數 {{ _scanned }}<br>
            新增筆數 {% if _added == '--' %}--{% else %}+{{ _added }}{% endif %}
        </div>
    </div>
</aside>

<div class="momo-shell-backdrop" data-momo-sidebar-close></div>
