{% extends "service_monitor/base.html" %}
{% block title %}监测目标{% endblock %}
{% block content %}
<div class="section-head">
    <div class="section-title">📊 监测目标</div>
    {% if is_admin %}
    <a href="/service-monitor/targets" class="btn-add">+ 目标管理</a>
    {% endif %}
</div>

{% if targets %}
<div class="target-grid">
    {% for t in targets %}
    <div class="target-card">
        <div class="target-card-head">
            <div class="target-icon">{{ '🖥️' if t.type == 'local' else '🌐' }}</div>
            <div class="target-name">{{ t.name }}</div>
            {% if t.built_in %}<span class="target-badge">内置</span>{% endif %}
        </div>
        <div class="target-meta">
            {{ '本机容器' if t.type == 'local' else (t.host ~ ':' ~ t.port ~ '  ' ~ t.username) }}
        </div>
        {% if is_admin %}
        <div class="target-actions">
            <a href="/service-monitor/run/{{ t.id }}?suite=quick" class="btn-suite btn-quick" onclick="return checkBeforeRun('/service-monitor/run/{{ t.id }}?suite=quick', '{{ t.id }}')">⚡ 快速巡检</a>
            <a href="/service-monitor/run/{{ t.id }}?suite=full" class="btn-suite btn-full" onclick="return checkBeforeRun('/service-monitor/run/{{ t.id }}?suite=full', '{{ t.id }}')">🔍 全量巡检</a>
        </div>
        {% else %}
        <div class="target-actions">
            <span class="btn-suite btn-quick" style="cursor:default; opacity:.5;">⚡ 快速巡检</span>
            <span class="btn-suite btn-full" style="cursor:default; opacity:.5;">🔍 全量巡检</span>
        </div>
        {% endif %}
    </div>
    {% endfor %}
</div>
{% else %}
<div class="empty">📭 暂无监测目标</div>
{% endif %}
{% endblock %}

{% block extra_css %}
<style>
    /* 目标卡片网格 */
    .target-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 36px; }
    .target-card {
        background: #fff; border-radius: 12px; padding: 20px; box-shadow: 0 2px 8px rgba(0,0,0,.06);
        border: 1px solid var(--gray-200);
    }
    .target-card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
    .target-icon { font-size: 28px; }
    .target-name { font-size: 16px; font-weight: 700; color: var(--gray-900); }
    .target-meta { font-size: 13px; color: var(--gray-500); margin-bottom: 14px; }
    .target-actions { display: flex; gap: 8px; }
    .btn-suite {
        flex: 1; padding: 8px 0; border-radius: 8px; font-size: 13px; font-weight: 600;
        text-align: center; text-decoration: none; cursor: pointer; border: none;
    }
    .btn-quick { background: var(--gray-100); color: var(--gray-700); }
    .btn-quick:hover { background: var(--gray-200); }
    .btn-full { background: #eef2ff; color: var(--primary); }
    .btn-full:hover { background: #e0e7ff; }
    .target-badge {
        display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 4px;
        background: var(--gray-100); color: var(--gray-500); margin-left: auto;
    }

    @media screen and (max-width: 1024px) { .target-grid { grid-template-columns: repeat(2, 1fr); } }
    @media screen and (max-width: 768px) {
        .target-grid { grid-template-columns: 1fr; gap: 12px; }
        .target-card { padding: 16px; }
        .target-card-head { margin-bottom: 10px; }
        .target-icon { font-size: 24px; }
        .target-name { font-size: 15px; }
        .target-meta { font-size: 12px; margin-bottom: 12px; }
        .target-actions { flex-direction: column; gap: 8px; }
        .btn-suite { padding: 10px 0; font-size: 13px; }
        .section-head { margin-bottom: 12px; }
        .section-title { font-size: 16px; }
        .btn-add { padding: 8px 14px; font-size: 13px; }
    }
    @media screen and (max-width: 480px) {
        .target-grid { gap: 10px; }
        .target-card { padding: 14px; }
        .target-icon { font-size: 20px; }
        .target-name { font-size: 14px; }
        .target-meta { font-size: 11px; }
        .btn-suite { padding: 9px 0; font-size: 12px; }
        .section-head { flex-direction: column; align-items: flex-start; gap: 8px; }
        .btn-add { width: 100%; text-align: center; font-size: 12px; padding: 7px 12px; }
    }
</style>
{% endblock %}

{% block extra_js %}
<script>
async function checkBeforeRun(href, targetId) {
    try {
        const resp = await fetch(`/api/service-monitor/run/status/${encodeURIComponent(targetId)}`, {credentials:'include'});
        if (resp.ok) {
            const status = await resp.json();
            if (status.running) {
                alert(`该目标已有巡检正在运行（${status.done}/${status.total}），请等待完成或取消后再试`);
                return false;
            }
        }
    } catch(err) {}
    window.location.href = href;
    return false;
}
</script>
{% endblock %}