<template>
  <div class="token-usage-page">
    <div class="usage-shell">
      <div class="page-header">
        <div>
          <div class="title-line">
            <span class="title-icon">
              <i class="el-icon-data-line"></i>
            </span>
            <h2>Token用量</h2>
          </div>
          <p>查看模型调用 Token 消耗、调用次数和费用趋势。</p>
        </div>
        <el-button size="small" icon="el-icon-refresh" class="refresh-btn">刷新</el-button>
      </div>

      <div class="stat-row">
        <div v-for="item in statCards" :key="item.label" class="stat-card" :class="item.type">
          <span>{{ item.label }}</span>
          <strong>{{ item.value }}</strong>
          <em>{{ item.desc }}</em>
        </div>
      </div>

      <div class="content-card">
        <div class="card-header">
          <div>
            <h3>用量明细</h3>
            <p>按模型维度统计 Token 输入、输出与费用。</p>
          </div>
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            size="small"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </div>

        <el-table :data="usageList" class="usage-table" style="width: 100%">
          <el-table-column prop="modelName" label="模型名称" min-width="180"></el-table-column>
          <el-table-column prop="calls" label="调用次数" width="120"></el-table-column>
          <el-table-column prop="inputTokens" label="输入Token" min-width="140"></el-table-column>
          <el-table-column prop="outputTokens" label="输出Token" min-width="140"></el-table-column>
          <el-table-column prop="totalTokens" label="总Token" min-width="140"></el-table-column>
          <el-table-column prop="cost" label="预估费用" width="120"></el-table-column>
          <el-table-column prop="updatedAt" label="更新时间" min-width="170"></el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TokenUsage',
  data() {
    return {
      dateRange: [],
      statCards: [
        { label: '总消耗 Token', value: '1,286,400', desc: '较昨日 +12.6%', type: 'primary' },
        { label: '调用次数', value: '3,482', desc: '今日累计调用', type: 'success' },
        { label: '预估费用', value: '¥ 128.64', desc: '按当前单价估算', type: 'warning' },
        { label: '活跃模型', value: '8', desc: '最近 7 天有调用', type: 'purple' }
      ],
      usageList: [
        {
          modelName: 'DeepSeek-V4',
          calls: 1260,
          inputTokens: '420,000',
          outputTokens: '318,000',
          totalTokens: '738,000',
          cost: '¥ 73.80',
          updatedAt: '2026-05-25 10:00:00'
        },
        {
          modelName: '通义千问-Plus',
          calls: 856,
          inputTokens: '210,400',
          outputTokens: '156,000',
          totalTokens: '366,400',
          cost: '¥ 38.20',
          updatedAt: '2026-05-25 09:42:16'
        },
        {
          modelName: '腾讯混元-Pro',
          calls: 342,
          inputTokens: '102,000',
          outputTokens: '80,000',
          totalTokens: '182,000',
          cost: '¥ 16.64',
          updatedAt: '2026-05-25 09:18:31'
        }
      ]
    }
  }
}
</script>

<style lang="less" scoped>
.token-usage-page {
  min-height: 100vh;
  padding: 24px;
  background: linear-gradient(180deg, #f3f7ff 0%, #f7f9fc 48%, #ffffff 100%);
}

.usage-shell {
  min-height: calc(100vh - 48px);
  padding: 24px;
  background: #ffffff;
  border: 1px solid #edf1f7;
  border-radius: 18px;
  box-shadow: 0 12px 30px rgba(31, 45, 61, 0.06);
}

.page-header,
.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
}

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

.title-line {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;

  h2 {
    margin: 0;
    color: #1f2d3d;
    font-size: 24px;
  }
}

.title-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  color: #409eff;
  font-size: 18px;
  background: #eef5ff;
  border-radius: 12px;
}

.page-header p,
.card-header p {
  margin: 0;
  color: #909399;
  font-size: 13px;
}

.refresh-btn {
  border-radius: 10px;
}

.stat-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card {
  padding: 20px;
  border-radius: 16px;
  border: 1px solid #edf1f7;
  background: #ffffff;

  span,
  em {
    display: block;
    color: #909399;
    font-style: normal;
    font-size: 13px;
  }

  strong {
    display: block;
    margin: 10px 0 8px;
    color: #1f2d3d;
    font-size: 26px;
  }

  &.primary {
    background: #eef5ff;
  }

  &.success {
    background: #f0fdf4;
  }

  &.warning {
    background: #fff7ed;
  }

  &.purple {
    background: #f5f3ff;
  }
}

.content-card {
  padding: 22px;
  border: 1px solid #edf1f7;
  border-radius: 16px;
}

.card-header {
  margin-bottom: 18px;

  h3 {
    margin: 0 0 8px;
    color: #1f2d3d;
  }
}

.usage-table {
  border-radius: 12px;
  overflow: hidden;
}

@media (max-width: 1100px) {
  .stat-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 700px) {
  .stat-row {
    grid-template-columns: 1fr;
  }

  .page-header,
  .card-header {
    flex-direction: column;
  }
}
</style>
