<template>
  <view>
    <view class="stat-grid">
      <view class="stat-card">
        <view class="stat-icon blue">📱</view>
        <text class="stat-value">{{ stats.device_count || 0 }}</text>
        <text class="stat-label">绑定设备数</text>
      </view>
      <view class="stat-card">
        <view class="stat-icon green">👥</view>
        <text class="stat-value">{{ stats.user_count || 0 }}</text>
        <text class="stat-label">注册用户数</text>
      </view>
      <view class="stat-card">
        <view class="stat-icon pink">💆</view>
        <text class="stat-value">{{ stats.treatment_count || 0 }}</text>
        <text class="stat-label">使用次数</text>
      </view>
      <view class="stat-card">
        <view class="stat-icon gold">💳</view>
        <text class="stat-value">{{ stats.subscription_count || 0 }}</text>
        <text class="stat-label">活跃订阅</text>
      </view>
    </view>

    <view class="stat-grid secondary-stats" v-if="subStats.monthly_count != null">
      <view class="stat-card">
        <text class="stat-value">{{ subStats.monthly_count || 0 }}</text>
        <text class="stat-label">月卡会员</text>
      </view>
      <view class="stat-card">
        <text class="stat-value">{{ subStats.yearly_count || 0 }}</text>
        <text class="stat-label">年卡会员</text>
      </view>
      <view class="stat-card">
        <text class="stat-value">{{ subStats.trial_count || 0 }}</text>
        <text class="stat-label">试用中</text>
      </view>
      <view class="stat-card">
        <text class="stat-value">¥{{ subStats.monthly_revenue || 0 }}</text>
        <text class="stat-label">本月收入</text>
      </view>
    </view>

    <view class="main-row">
      <view class="card table-card">
        <view class="card-header">
          <text class="card-title">实时使用数据</text>
          <text class="card-extra" @click="$emit('navigate', 'record')">查看全部 ›</text>
        </view>
        <view class="data-table">
          <view class="t-header">
            <view class="t-row">
              <text class="t-th" style="flex:2">用户</text>
              <text class="t-th" style="flex:2">设备</text>
              <text class="t-th" style="flex:1">模式</text>
              <text class="t-th" style="flex:1">时长</text>
              <text class="t-th" style="flex:2">时间</text>
              <text class="t-th" style="flex:1">状态</text>
            </view>
          </view>
          <view class="t-body">
            <view class="t-row" v-for="(item, idx) in recentTreatments" :key="idx">
              <text class="t-td" style="flex:2">{{ item.nickname || item.user_id || '-' }}</text>
              <text class="t-td" style="flex:2">{{ item.device_id || '-' }}</text>
              <text class="t-td" style="flex:1">
                <text :class="item.mode === 1 ? 'badge badge-blue' : 'badge badge-warning'">{{ item.mode === 1 ? '✨ 智能' : '🔄 普通' }}</text>
              </text>
              <text class="t-td" style="flex:1">{{ Math.floor((item.total_duration_ms || 0) / 60000) }}分</text>
              <text class="t-td" style="flex:2">{{ formatDate(item.start_time || item.started_at) }}</text>
              <text class="t-td" style="flex:1">
                <text class="badge badge-success">已完成</text>
              </text>
            </view>
            <view v-if="recentTreatments.length === 0" class="empty-state">
              <text class="empty-text">暂无使用记录</text>
            </view>
          </view>
        </view>
      </view>

      <view class="card quick-card">
        <view class="card-title">快捷操作</view>
        <view class="quick-list">
          <view class="quick-item" @click="$emit('navigate', 'device')">
            <view class="quick-icon" style="background:#E6508C">📱</view>
            <text class="quick-label">设备管理</text>
            <text class="quick-arrow">›</text>
          </view>
          <view class="quick-item" @click="$emit('navigate', 'user')">
            <view class="quick-icon" style="background:#E6508C">👥</view>
            <text class="quick-label">用户管理</text>
            <text class="quick-arrow">›</text>
          </view>
          <view class="quick-item" @click="$emit('navigate', 'subscription')">
            <view class="quick-icon" style="background:#E6508C">💳</view>
            <text class="quick-label">订阅管理</text>
            <text class="quick-arrow">›</text>
          </view>
          <view class="quick-item" @click="$emit('navigate', 'record')">
            <view class="quick-icon" style="background:#E6508C">📋</view>
            <text class="quick-label">数据报表</text>
            <text class="quick-arrow">›</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { get } from '../utils/request'
import { formatDate as formatDateUtil } from '../utils/format'

export default {
  name: 'DashboardView',
  data() {
    return {
      stats: {},
      subStats: {},
      recentTreatments: []
    }
  },
  mounted() {
    this.loadDashboard()
  },
  methods: {
    async loadDashboard() {
      try {
        const dashboard = await get('/api/v1/admin/dashboard')
        this.stats = dashboard
        if (dashboard.sub_stats) this.subStats = dashboard.sub_stats
      } catch (e) {
        uni.showToast({ title: '加载失败', icon: 'none' })
      }
      try {
        const data = await get('/api/v1/admin/records', { page: 1, page_size: 5 })
        this.recentTreatments = data.records || []
      } catch (e) {
        this.recentTreatments = []
      }
    },
    formatDate: formatDateUtil
  }
}
</script>

<style scoped>
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}

.stat-icon.blue {
  background: #e6f7ff;
}

.stat-icon.green {
  background: #f6ffed;
}

.stat-icon.pink {
  background: #fff0f6;
}

.stat-icon.gold {
  background: #fff7e6;
}

.stat-value {
  display: block;
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.stat-label {
  font-size: 14px;
  color: #999;
}

.main-row {
  display: flex;
  gap: 16px;
}

.card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
}

.table-card {
  flex: 1;
}

.quick-card {
  width: 260px;
  flex-shrink: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
}

.card-header .card-title {
  margin-bottom: 0;
}

.card-extra {
  font-size: 14px;
  color: #E6508C;
  cursor: pointer;
}

.data-table {
  width: 100%;
}

.t-header {
  background: #fafafa;
}

.t-row {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.t-th {
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.t-td {
  font-size: 14px;
  color: #333;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.badge-success {
  background: #f6ffed;
  color: #52c41a;
}

.badge-blue {
  background: #e6f7ff;
  color: #1890ff;
}

.quick-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.quick-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 8px;
  background: #fafafa;
  cursor: pointer;
}

.quick-item:hover {
  background: #f0f0f0;
}

.quick-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #fff;
}

.quick-label {
  flex: 1;
  font-size: 14px;
  color: #333;
}

.quick-arrow {
  font-size: 18px;
  color: #999;
}

.empty-state {
  padding: 24px 0;
  text-align: center;
}

.empty-text {
  font-size: 14px;
  color: #999;
}

.secondary-stats {
  margin-bottom: 20px;
}
</style>
