{% extends 'admin/admin_base.html' %}
{% block title %}Алерты{% endblock %}
{% block breadcrumb %}
{{ super() }}
<li class="breadcrumb-item active">Алерты</li>
{% endblock %}
{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <h1 class="mb-2">Управление алертами</h1>
            <p class="text-muted mb-0">Настройка правил и мониторинг уведомлений</p>
        </div>
    </div>
    
    <!-- Активные алерты -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Активные алерты</h5>
                    <button class="btn btn-sm btn-primary" onclick="loadAlerts()">
                        <i class="bi bi-arrow-clockwise"></i> Обновить
                    </button>
                </div>
                <div class="card-body">
                    <div id="activeAlertsContainer">
                        <p class="text-muted">Загрузка...</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Правила алертов -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Правила алертов</h5>
                    <button class="btn btn-sm btn-primary" onclick="openAddRuleModal()">
                        <i class="bi bi-plus-lg"></i> Добавить правило
                    </button>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Название</th>
                                    <th>Метрика</th>
                                    <th>Условие</th>
                                    <th>Порог</th>
                                    <th>Уровень</th>
                                    <th>Статус</th>
                                    <th>Действия</th>
                                </tr>
                            </thead>
                            <tbody id="alertsRulesTableBody">
                                <tr>
                                    <td colspan="7" class="text-center">Загрузка...</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- История алертов -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">История алертов</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Время</th>
                                    <th>Правило</th>
                                    <th>Метрика</th>
                                    <th>Значение</th>
                                    <th>Порог</th>
                                    <th>Уровень</th>
                                    <th>Статус</th>
                                </tr>
                            </thead>
                            <tbody id="alertsHistoryTableBody">
                                <tr>
                                    <td colspan="7" class="text-center">Загрузка...</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Модальное окно для добавления правила -->
<div class="modal fade" id="addRuleModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Добавить правило алерта</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="addRuleForm">
                    <div class="mb-3">
                        <label for="ruleName" class="form-label">Название правила</label>
                        <input type="text" class="form-control" id="ruleName" required>
                    </div>
                    <div class="mb-3">
                        <label for="ruleMetric" class="form-label">Метрика</label>
                        <select class="form-select" id="ruleMetric" required>
                            <option value="cpu_usage_percent">CPU Usage (%)</option>
                            <option value="memory_usage_percent">Memory Usage (%)</option>
                            <option value="request_throughput">Request Throughput (RPS)</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="ruleCondition" class="form-label">Условие</label>
                        <select class="form-select" id="ruleCondition" required>
                            <option value="gt">Больше (>)</option>
                            <option value="lt">Меньше (<)</option>
                            <option value="gte">Больше или равно (>=)</option>
                            <option value="lte">Меньше или равно (<=)</option>
                            <option value="eq">Равно (=)</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="ruleThreshold" class="form-label">Порог</label>
                        <input type="number" class="form-control" id="ruleThreshold" step="0.01" required>
                    </div>
                    <div class="mb-3">
                        <label for="ruleSeverity" class="form-label">Уровень серьезности</label>
                        <select class="form-select" id="ruleSeverity" required>
                            <option value="info">Информация</option>
                            <option value="warning">Предупреждение</option>
                            <option value="error">Ошибка</option>
                            <option value="critical">Критично</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="ruleDescription" class="form-label">Описание</label>
                        <textarea class="form-control" id="ruleDescription" rows="3"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Отмена</button>
                <button type="button" class="btn btn-primary" onclick="saveRule()">Сохранить</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block admin_scripts %}
<script src="{{ url_for('static', filename='js/alerts.js') }}"></script>
{% endblock %}
