{% extends "page.html" %}
{% block page_content %}
{% if latest_shs %}
<div class="card-grid">
    <div class="card"><div class="card-label">SHS Score</div><div class="card-value value-accent">{{ "%.2f"|format(latest_shs.shs_score) }}</div><div class="card-label"><span class="badge badge-{{ latest_shs.shs_status|lower }}">{{ latest_shs.shs_status }}</span></div></div>
    <div class="card"><div class="card-label">MRR</div><div class="card-value value-green">{{ "%.2f"|format(mrr) }} ₽</div></div>
    <div class="card"><div class="card-label">SLA Compliance</div><div class="card-value {% if sla_compliance >= 90 %}value-green{% elif sla_compliance >= 70 %}value-yellow{% else %}value-red{% endif %}">{{ sla_compliance }}%</div></div>
    <div class="card"><div class="card-label">Активные объекты</div><div class="card-value value-info">{{ active_objects }}</div></div>
    <div class="card"><div class="card-label">Сотрудники</div><div class="card-value">{{ active_engineers }}</div></div>
    <div class="card"><div class="card-label">Активные SLA</div><div class="card-value">{{ active_contracts }}</div></div>
    <div class="card"><div class="card-label">Инциденты (30д)</div><div class="card-value {% if incidents_30 > 5 %}value-red{% else %}value-green{% endif %}">{{ incidents_30 }}</div></div>
    <div class="card"><div class="card-label">Avg Engineer Score</div><div class="card-value value-accent">{{ avg_es }}</div></div>
</div>
{% endif %}

<div class="card">
    <div class="card-header"><h2>История SHS</h2>
        <div class="actions"><form method="post" action="/service/ceo/calc" style="display:inline"><button class="btn btn-primary btn-sm">Рассчитать SHS</button></form>
        <form method="post" action="/service/ceo/kpi" style="display:inline"><button class="btn btn-secondary btn-sm">Рассчитать KPI</button></form></div>
    </div>
    <div class="table-wrap"><table><thead><tr><th>Период</th><th>SHS</th><th>Статус</th><th>MRR</th><th>Retention</th><th>Сотрудники</th></tr></thead>
        <tbody>{% for s in shs_history %}<tr>
            <td><small>{{ s.period_start.strftime('%d.%m.%Y') if s.period_start else '' }} — {{ s.period_end.strftime('%d.%m.%Y') if s.period_end else '' }}</small></td>
            <td><strong>{{ "%.2f"|format(s.shs_score) }}</strong></td>
            <td><span class="badge badge-{{ s.shs_status|lower }}">{{ s.shs_status }}</span></td>
            <td>{{ "%.2f"|format(s.mrr or 0) }} ₽</td>
            <td>{{ "%.1f"|format(s.retention_rate or 0) }}%</td>
            <td>{{ s.employee_count or 0 }}</td>
        </tr>{% endfor %}</tbody></table>
    </div>
</div>

<div class="card">
    <div class="card-header"><h2>Последние KPI инженеров</h2></div>
    <div class="table-wrap"><table><thead><tr><th>Инженер</th><th>Период</th><th>SLA Compliance</th><th>Engineer Score</th><th>Grade</th></tr></thead>
        <tbody>{% for kpi, name in kpi_data %}<tr>
            <td>{{ name }}</td>
            <td><small>{{ kpi.period_start.strftime('%d.%m.%Y') if kpi.period_start else '' }} — {{ kpi.period_end.strftime('%d.%m.%Y') if kpi.period_end else '' }}</small></td>
            <td>{{ "%.1f"|format(kpi.sla_compliance or 0) }}%</td>
            <td><strong>{{ "%.2f"|format(kpi.engineer_score or 0) }}</strong></td>
            <td><span class="badge badge-{{ kpi.grade|lower if kpi.grade else 'default' }}">{{ kpi.grade or '—' }}</span></td>
        </tr>{% endfor %}</tbody></table>
    </div>
</div>
{% endblock %}
