{% extends "crowdsec_base.html" %}

{% block title %}대시보드 - RealNAS security{% endblock %}

{% block content %}
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8" />
    <title>RealNAS security</title>
    <style>
        body {
            font-family: 'Malgun Gothic', Arial, sans-serif;
            background: #f8f9fb;
            color: #222;
            margin: 0;
            padding: 0;
        }
        .navbar {
            background: #24262b;
            color: white;
            padding: 8px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            flex-wrap: wrap;
        }
        .navbar a {
            color: #aee1ff;
            text-decoration: none;
            margin-left: 12px;
            transition: color 0.3s ease;
        }
        .navbar a:hover,
        .navbar a.active {
            color: white;
            text-decoration: underline;
        }
        .container {
            max-width: 1400px;
            margin: 20px auto;
            padding: 0 20px;
        }
        h1, h2 {
            margin-bottom: 12px;
            font-weight: 700;
            color: #333;
        }
        section {
            background: white;
            padding: 20px 25px;
            margin-bottom: 25px;
            border-radius: 10px;
            box-shadow: 0 3px 8px rgba(0,0,0,0.08);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px 14px;
            text-align: left;
            vertical-align: middle;
        }
        th {
            background: #f0f2f7;
            font-weight: 600;
            color: #555;
        }
        button {
            background: #369;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.25s ease;
            font-size: 14px;
        }
        button:hover {
            background: #246;
        }
        input[type="text"], select {
            padding: 8px 10px;
            margin-right: 12px;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 14px;
            width: 250px;
            max-width: 100%;
            box-sizing: border-box;
        }
        .flex-row {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
            flex-wrap: wrap;
        }
        .flex-row label {
            margin-right: 8px;
            font-weight: 600;
            color: #444;
        }
        .flash-message {
            background: #fff5cc;
            border: 1px solid #ffd324;
            padding: 12px 15px;
            margin: 15px 0;
            border-radius: 6px;
            color: #665c00;
            font-size: 14px;
            box-shadow: 0 1px 4px rgba(255, 211, 36, 0.3);
        }
        .status-good {
            color: #2a9d8f;
            font-weight: 700;
        }
        .status-bad {
            color: #e76f51;
            font-weight: 700;
        }
        @media (max-width: 768px) {
            .navbar {
                font-size: 12px;
            }
            input[type="text"] {
                width: 100%;
                margin-bottom: 10px;
            }
            .flex-row {
                flex-direction: column;
                align-items: flex-start;
            }
            button {
                width: 100%;
            }
        }
    </style>
</head>

<div class="container">

    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <div class="flash-message">
          {% for message in messages %}
            <div>{{ message }}</div>
          {% endfor %}
        </div>
      {% endif %}
    {% endwith %}

    <section>
        <h1>대시보드</h1>
        <button onclick="location.href='/share'" style="margin-bottom:12px;">관리 페이지로 돌아가기</button>
        <p>전체 이벤트: <strong>{{ metrics.total_events or '0' }}</strong></p>
        <p>현재 활성 차단 IP: <strong>{{ metrics.active_blocks or '0' }}</strong></p>
        <p>총 시나리오 수: <strong>{{ metrics.scenario_count or '0' }}</strong></p>
        <p>최근 24시간 경고: <strong>{{ metrics.alerts_24h or '0' }}</strong></p>
    </section>

    <section>
        <h2>차단 IP 목록</h2>
        <form method="POST" action="{{ url_for('crowdsec_unban') }}">
            <table>
                <thead>
                    <tr>
                        <th>IP 주소</th>
                        <th>차단 이유</th>
                        <th>차단 시작 시간</th>
                        <th>차단 만료 시간</th>
                        <th>선택</th>
                    </tr>
                </thead>
                <tbody>
                    {% for block in blocks %}
                    <tr>
                        <td>{{ block.ip }}</td>
                        <td>{{ block.reason }}</td>
                        <td>{{ block.start_time }}</td>
                        <td>{{ block.end_time }}</td>
                        <td><input type="checkbox" name="ips_to_unban" value="{{ block.ip }}"></td>
                    </tr>
                    {% else %}
                    <tr><td colspan="5" style="text-align:center;">차단된 IP가 없습니다.</td></tr>
                    {% endfor %}
                </tbody>
            </table>
            <button type="submit">선택된 IP 차단 해제</button>
        </form>
    </section>

    <section>
        <h2>로그 검색</h2>
        <form method="GET" action="{{ url_for('crowdsec_logs') }}">
            <div class="flex-row">
                <label for="search_term">검색어:</label>
                <input type="text" id="search_term" name="q" value="{{ request.args.get('q', '') }}" placeholder="IP, 메시지 등">
                <button type="submit">검색</button>
            </div>
        </form>
        {% if logs %}
            <table>
                <thead>
                    <tr><th>시간</th><th>IP</th><th>이벤트</th></tr>
                </thead>
                <tbody>
                    {% for log in logs %}
                    <tr>
                        <td>{{ log.time }}</td>
                        <td>{{ log.ip }}</td>
                        <td>{{ log.message }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        {% else %}
            <p>검색 결과가 없습니다.</p>
        {% endif %}
    </section>


</div>

</body>
</html>
{% endblock %}