<?php
$s = is_array($stats ?? null) ? $stats : [];
$daily = is_array($s['daily_activations'] ?? null) ? $s['daily_activations'] : [];
?>
<div class="row">
  <div class="col-md-3">
    <div class="card">
      <div class="card-body">
        <div style="display:flex;align-items:center;justify-content:space-between;">
          <div>
            <div style="color:#64748b;font-size:12px;"><?= e(app_zh('用户总数')) ?></div>
            <div style="font-size:22px;font-weight:700;"><?= e((string)($s['users_total'] ?? 0)) ?></div>
          </div>
          <i class="mdi mdi-account-multiple" style="font-size:28px;color:#0ea5e9;"></i>
        </div>
        <div style="margin-top:8px;color:#94a3b8;font-size:12px;"><?= e(app_zh('今日新增')) ?>: <?= e((string)($s['users_new_today'] ?? 0)) ?></div>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="card">
      <div class="card-body">
        <div style="display:flex;align-items:center;justify-content:space-between;">
          <div>
            <div style="color:#64748b;font-size:12px;"><?= e(app_zh('卡密总数')) ?></div>
            <div style="font-size:22px;font-weight:700;"><?= e((string)($s['keys_total'] ?? 0)) ?></div>
          </div>
          <i class="mdi mdi-key" style="font-size:28px;color:#22c55e;"></i>
        </div>
        <div style="margin-top:8px;color:#94a3b8;font-size:12px;"><?= e(app_zh('今日生成')) ?>: <?= e((string)($s['keys_created_today'] ?? 0)) ?></div>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="card">
      <div class="card-body">
        <div style="display:flex;align-items:center;justify-content:space-between;">
          <div>
            <div style="color:#64748b;font-size:12px;"><?= e(app_zh('已激活')) ?></div>
            <div style="font-size:22px;font-weight:700;"><?= e((string)($s['keys_activated_total'] ?? 0)) ?></div>
          </div>
          <i class="mdi mdi-flash" style="font-size:28px;color:#f59e0b;"></i>
        </div>
        <div style="margin-top:8px;color:#94a3b8;font-size:12px;">
          <?= e(app_zh('今日激活')) ?>:
          <?php if (!empty($s['has_activated_at'])): ?>
            <?= e((string)($s['keys_activated_today'] ?? 0)) ?>
          <?php else: ?>
            <span style="color:#ef4444;"><?= e(app_zh('未启用（需执行数据库迁移，新增 activated_at 字段）')) ?></span>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="card">
      <div class="card-body">
        <div style="display:flex;align-items:center;justify-content:space-between;">
          <div>
            <div style="color:#64748b;font-size:12px;"><?= e(app_zh('已过期')) ?></div>
            <div style="font-size:22px;font-weight:700;"><?= e((string)($s['keys_expired_total'] ?? 0)) ?></div>
          </div>
          <i class="mdi mdi-timer-sand" style="font-size:28px;color:#ef4444;"></i>
        </div>
        <div style="margin-top:8px;color:#94a3b8;font-size:12px;">
          <?= e(app_zh('状态')) ?>: <?= e(app_zh('启用')) ?> <?= e((string)($s['keys_active'] ?? 0)) ?> / <?= e(app_zh('禁用')) ?> <?= e((string)($s['keys_inactive'] ?? 0)) ?>
        </div>
      </div>
    </div>
  </div>

  <div class="col-md-12">
    <div class="card">
      <div class="card-header"><h4><?= e(app_zh('近7天激活量')) ?></h4></div>
      <div class="card-body">
        <?php if (empty($s['has_activated_at'])): ?>
          <p style="color:#777;"><?= e(app_zh('需要数据库迁移：为 keys_code 表添加 activated_at 字段后才可统计激活日期。')) ?></p>
        <?php else: ?>
          <div class="table-responsive">
            <table class="table table-bordered">
              <thead>
                <tr>
                  <th><?= e(app_zh('日期')) ?></th>
                  <th><?= e(app_zh('激活数量')) ?></th>
                </tr>
              </thead>
              <tbody>
                <?php foreach ($daily as $row): ?>
                  <tr>
                    <td><?= e((string)($row['date'] ?? '')) ?></td>
                    <td><?= e((string)($row['count'] ?? 0)) ?></td>
                  </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>
