<view class="container">
  <view class="hero fade-up">
    <view class="hero-badge">OPS DASHBOARD</view>
    <view class="hero-title">垃圾信息运营看板</view>
    <view class="hero-sub">覆盖发布、拦截、申诉、样本与模型状态，支持日常运营与风险监控。</view>
  </view>

  <view class="card fade-up fade-up-delay-1" wx:if="{{kpis.length}}">
    <view class="card-title">核心指标</view>
    <view class="grid-2">
      <view class="kpi" wx:for="{{kpis}}" wx:key="label">
        <view class="kpi-value">{{item.value}}</view>
        <view class="kpi-label">{{item.label}}</view>
      </view>
    </view>
  </view>

  <view class="card fade-up fade-up-delay-2" wx:if="{{stats && stats.threshold}}">
    <view class="card-title">检测阈值配置</view>
    <view class="row"><text class="label">当前阈值</text><text class="value">{{stats.threshold_text}}</text></view>
    <view class="row"><text class="label">更新时间</text><text class="value">{{stats.threshold.updated_at || '--'}}</text></view>
  </view>

  <view class="card fade-up fade-up-delay-2" wx:if="{{stats && stats.model_info}}">
    <view class="card-title">模型信息</view>
    <view class="row"><text class="label">模型版本</text><text class="value">{{stats.model_info.version || '未训练'}}</text></view>
    <view class="row"><text class="label">训练时间</text><text class="value">{{stats.model_info.trained_at || '--'}}</text></view>
    <view class="row"><text class="label">样本数量</text><text class="value">{{stats.model_info.sample_count || 0}}</text></view>
  </view>

  <view class="card fade-up fade-up-delay-3" wx:if="{{bars.length}}">
    <view class="card-title">近 7 天发布趋势</view>
    <view class="list-item" wx:for="{{bars}}" wx:key="date">
      <view class="row">
        <text class="label">{{item.label}}</text>
        <text class="value">{{item.value}} 条</text>
      </view>
      <view class="progress-track">
        <view class="progress-fill-safe" style="width: {{item.percent_text}};"></view>
      </view>
    </view>
  </view>

  <view class="card fade-up fade-up-delay-3" wx:if="{{sourceDist.length}}">
    <view class="card-title">训练样本来源</view>
    <view class="list-item" wx:for="{{sourceDist}}" wx:key="name">
      <view class="row"><text class="item-title">{{item.name}}</text><text class="value">{{item.value}}</text></view>
    </view>
  </view>

  <view class="card fade-up fade-up-delay-3" wx:if="{{topKeywords.length}}">
    <view class="card-title">高频风险词</view>
    <view class="chip-group">
      <text class="tag" wx:for="{{topKeywords}}" wx:key="token">{{item.token}} × {{item.count}}</text>
    </view>
  </view>

  <!-- 生成报告按钮 -->
  <view class="card fade-up fade-up-delay-3">
    <button class="btn btn-accent" loading="{{reportLoading}}" bindtap="generateReport">生成运营报告</button>
  </view>

  <!-- 报告展示区域 -->
  <view class="report-modal" wx:if="{{report}}">
    <view class="report-header">
      <view class="report-title">垃圾信息运营报告</view>
      <view class="report-period">{{report.period}}</view>
    </view>

    <view class="report-close" bindtap="closeReport">×</view>

    <view class="report-section">
      <view class="report-section-title">汇总统计</view>
      <view class="grid-3">
        <view class="report-kpi">
          <view class="report-kpi-value">{{report.summary.total_posts}}</view>
          <view class="report-kpi-label">总发布量</view>
        </view>
        <view class="report-kpi">
          <view class="report-kpi-value">{{report.summary.total_blocked}}</view>
          <view class="report-kpi-label">拦截量</view>
        </view>
        <view class="report-kpi">
          <view class="report-kpi-value">{{report.summary.total_published}}</view>
          <view class="report-kpi-label">正常发布</view>
        </view>
      </view>
      <view class="row">
        <text class="label">拦截率</text>
        <text class="value">{{report.summary.blocked_ratio * 100}}%</text>
      </view>
      <view class="row">
        <text class="label">平均误判率</text>
        <text class="value">{{report.summary.avg_misjudge_rate_text}}</text>
      </view>
    </view>

    <view class="report-section">
      <view class="report-section-title">垃圾信息数量变化（近14天）</view>
      <view class="report-trend-item" wx:for="{{report.spam_trend}}" wx:key="date">
        <view class="row">
          <text class="label">{{item.label}}</text>
          <text class="value">拦截 {{item.blocked}} / 发布 {{item.published}}</text>
        </view>
        <view class="progress-track">
          <view class="progress-fill" style="width: {{item.blocked_percent}};"></view>
        </view>
      </view>
    </view>

    <view class="report-section">
      <view class="report-section-title">高频风险词 Top10</view>
      <view class="chip-group">
        <text class="tag tag-danger" wx:for="{{report.topKeywords}}" wx:for-item="kw" wx:if="{{index < 10}}" wx:key="token">{{kw.token}} × {{kw.count}}</text>
      </view>
    </view>

    <view class="report-section">
      <view class="report-section-title">误判率趋势（近14天）</view>
      <view class="report-trend-item" wx:for="{{report.misjudge_trend}}" wx:key="date">
        <view class="row">
          <text class="label">{{item.label}}</text>
          <text class="value">{{item.misjudge_rate_text}}</text>
        </view>
        <view class="progress-track">
          <view class="progress-fill-safe" style="width: {{item.rate_percent}};"></view>
        </view>
      </view>
    </view>

    <view class="btn-row">
      <button class="btn btn-primary" bindtap="copyReportText">复制报告文本</button>
      <button class="btn btn-ghost" bindtap="closeReport">关闭</button>
    </view>
  </view>
</view>
