<script setup lang="ts">
const { user } = useAuthSession()

const stats = await useHttpFetch("/api/scheduler/stats")
const statsData = computed(() => (stats.data.value ?? {}) as {
  totalTasks: number
  enabledTasks: number
  activeJobs: number
  last24hExecutions: number
})

const quickActions = [
  { label: '查看定时任务', to: '/admin/scheduler', icon: 'schedule', desc: '管理系统定时任务' },
]
</script>

<template>
  <div class="dashboard-page">
    <header class="page-header">
      <div class="welcome-section">
        <h1 class="page-title">欢迎回来，{{ user?.username || '管理员' }}</h1>
        <p class="page-subtitle">以下是今日任务概况</p>
      </div>
    </header>

    <!-- Quick stats -->
    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon stat-icon-total">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <path d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </div>
        <div class="stat-content">
          <div class="stat-label">任务总数</div>
          <div class="stat-value">{{ statsData.totalTasks ?? 0 }}</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon stat-icon-active">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <path d="M14.828 14.828a4 4 0 01-5.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </div>
        <div class="stat-content">
          <div class="stat-label">运行中</div>
          <div class="stat-value">{{ statsData.enabledTasks ?? 0 }}</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon stat-icon-jobs">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <path d="M13 10V3L4 14h7v7l9-11h-7z" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </div>
        <div class="stat-content">
          <div class="stat-label">执行中</div>
          <div class="stat-value">{{ statsData.activeJobs ?? 0 }}</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon stat-icon-exec">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <path d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </div>
        <div class="stat-content">
          <div class="stat-label">24小时执行</div>
          <div class="stat-value">{{ statsData.last24hExecutions ?? 0 }}</div>
        </div>
      </div>
    </div>

    <!-- Quick actions -->
    <section class="quick-actions-section">
      <h2 class="section-title">快捷操作</h2>
      <div class="actions-grid">
        <NuxtLink v-for="action in quickActions" :key="action.to" :to="action.to" class="action-card">
          <div class="action-icon">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
              <path d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </div>
          <div class="action-content">
            <div class="action-label">{{ action.label }}</div>
            <div class="action-desc">{{ action.desc }}</div>
          </div>
          <svg class="action-arrow" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <path d="M9 5l7 7-7 7" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </NuxtLink>
      </div>
    </section>
  </div>
</template>

<style scoped>
.dashboard-page {
  padding: 40px;
}

.page-header {
  margin-bottom: 32px;
}

.page-title {
  font-family: var(--font-display);
  font-size: 32px;
  font-weight: 400;
  color: var(--color-ink);
  letter-spacing: -0.3px;
  margin: 0 0 8px 0;
}

.page-subtitle {
  font-size: 15px;
  color: var(--color-muted);
  margin: 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 40px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--color-surface-card);
  border-radius: 12px;
  padding: 20px;
}

.stat-icon {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.stat-icon svg {
  width: 22px;
  height: 22px;
}

.stat-icon-total {
  background: rgba(93, 184, 166, 0.12);
  color: var(--color-accent-teal);
}

.stat-icon-active {
  background: rgba(204, 120, 92, 0.12);
  color: var(--color-primary);
}

.stat-icon-jobs {
  background: rgba(232, 165, 90, 0.12);
  color: var(--color-accent-amber);
}

.stat-icon-exec {
  background: rgba(93, 136, 247, 0.12);
  color: #5b88f7;
}

.stat-content {
  min-width: 0;
}

.stat-label {
  font-size: 13px;
  color: var(--color-muted);
  font-weight: 500;
  margin-bottom: 2px;
}

.stat-value {
  font-family: var(--font-display);
  font-size: 26px;
  font-weight: 400;
  color: var(--color-ink);
  line-height: 1.1;
}

.section-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0 0 16px 0;
}

.quick-actions-section {
  margin-bottom: 32px;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}

.action-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 18px;
  background: var(--color-surface-card);
  border-radius: 10px;
  text-decoration: none;
  transition: all 0.15s ease;
}

.action-card:hover {
  background: var(--color-surface-soft);
}

.action-card:hover .action-arrow {
  transform: translateX(4px);
}

.action-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: var(--color-surface-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.action-icon svg {
  width: 20px;
  height: 20px;
  color: var(--color-muted);
}

.action-content {
  flex: 1;
  min-width: 0;
}

.action-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--color-ink);
  margin-bottom: 2px;
}

.action-desc {
  font-size: 13px;
  color: var(--color-muted);
}

.action-arrow {
  width: 18px;
  height: 18px;
  color: var(--color-muted-soft);
  flex-shrink: 0;
  transition: transform 0.15s ease;
}

@media (max-width: 1024px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 640px) {
  .dashboard-page {
    padding: 24px;
  }

  .page-title {
    font-size: 26px;
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .stat-card {
    padding: 16px;
  }
}
</style>