<template>
  <div class="account-statistics">
    <a-space direction="vertical" size="large" fill>
      <!-- 账号统计卡片 -->
      <!-- <a-row :gutter="16">
        <a-col :span="8">
          <a-card class="stat-card general-card">
            <template #title>账号总数</template>
            <div class="stat-value text-primary">
              {{ formatNumber(accountData.totalAccounts) }}
            </div>
          </a-card>
        </a-col>
        <a-col :span="8">
          <a-card class="stat-card general-card">
            <template #title>活跃账号数</template>
            <div class="stat-value text-success">
              {{ formatNumber(accountData.activeAccounts) }}
            </div>
          </a-card>
        </a-col>
        <a-col :span="8">
          <a-card class="stat-card general-card">
            <template #title>平均订单数</template>
            <div class="stat-value text-warning">
              {{ formatNumber(accountData.averageOrders) }}
            </div>
          </a-card>
        </a-col>
      </a-row> -->

      <!-- 图表区域 -->
      <!-- <a-row :gutter="16">
        <a-col :span="12">
          <a-card class="chart-card general-card">
            <template #title>账号活跃度趋势</template>
            <v-chart class="chart" :option="accountActivityOption" autoresize />
          </a-card>
        </a-col>
        <a-col :span="12">
          <a-card class="chart-card general-card">
            <template #title>账号订单分布</template>
            <v-chart
              class="chart"
              :option="accountOrderDistributionOption"
              autoresize
            />
          </a-card>
        </a-col>
      </a-row> -->

      <!-- 账号列表 -->
      <a-card>
        <template #title>账号详情</template>
        <a-table
          :data="accountList"
          :loading="loading"
          :pagination="pagination"
          @page-change="onPageChange"
          @page-size-change="onPageSizeChange"
        >
          <template #columns>
            <a-table-column title="账号ID" data-index="id" />
            <a-table-column title="账号名称" data-index="name" />
            <a-table-column title="订单数" data-index="orderCount" />
            <a-table-column title="成功订单数" data-index="successOrderCount" />
            <a-table-column title="订单总额" data-index="totalAmount">
              <template #cell="{ record }">
                ${{ formatNumber(record.totalAmount) }}
              </template>
            </a-table-column>
            <a-table-column title="最后活跃时间" data-index="lastActiveTime" />
            <a-table-column title="状态" data-index="status">
              <template #cell="{ record }">
                <a-tag :color="record.status === 'active' ? 'green' : 'red'">
                  {{ record.status === 'active' ? '活跃' : '非活跃' }}
                </a-tag>
              </template>
            </a-table-column>
          </template>
        </a-table>
      </a-card>
    </a-space>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { LineChart, BarChart } from 'echarts/charts';
import {
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
} from 'echarts/components';
import VChart from 'vue-echarts';
import { apiClient } from '@/api/index.ts';
import { Message } from '@arco-design/web-vue';

use([
  CanvasRenderer,
  LineChart,
  BarChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
]);

const props = defineProps<{
  groupId?: number;
}>();

const loading = ref(false);
const accountData = ref({
  totalAccounts: 0,
  activeAccounts: 0,
  averageOrders: 0
});

const accountList = ref([]);
const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 0,
  showTotal: true,
  showPageSize: true,
  pageSizeOptions: [10, 20, 50, 100]
});

// 账号活跃度趋势图表配置
const accountActivityOption = ref({
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['活跃账号数', '订单数']
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: [
    {
      type: 'value',
      name: '账号数'
    },
    {
      type: 'value',
      name: '订单数'
    }
  ],
  series: [
    {
      name: '活跃账号数',
      type: 'line',
      data: []
    },
    {
      name: '订单数',
      type: 'line',
      yAxisIndex: 1,
      data: []
    }
  ]
});

// 账号订单分布图表配置
const accountOrderDistributionOption = ref({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: {
    type: 'value',
    name: '账号数'
  },
  series: [
    {
      type: 'bar',
      data: []
    }
  ]
});

// 格式化数字
const formatNumber = (num: number) => {
  return new Intl.NumberFormat('en-US').format(num);
};

// 获取账号统计数据
const fetchAccountData = async () => {
  loading.value = true;
  try {
    // const res = await apiClient.apiCardInfoWalmartAccountStatisticsGet({
    //   groupId: props.groupId
    // });
    // accountData.value = res.data.summary;
    // accountList.value = res.data.accounts;
    // pagination.value.total = res.data.total;
    // 更新图表数据
    // updateCharts(res.data);
  } catch (error) {
    Message.error('获取账号统计数据失败');
  } finally {
    loading.value = false;
  }
};

// 更新图表数据
const updateCharts = (data: any) => {
  // 更新账号活跃度趋势图表
  accountActivityOption.value.xAxis.data = data.trend.dates;
  accountActivityOption.value.series[0].data = data.trend.activeAccounts;
  accountActivityOption.value.series[1].data = data.trend.orderCounts;

  // 更新账号订单分布图表
  accountOrderDistributionOption.value.xAxis.data = data.distribution.ranges;
  accountOrderDistributionOption.value.series[0].data =
    data.distribution.counts;
};

// 分页处理
const onPageChange = (current: number) => {
  pagination.value.current = current;
  fetchAccountData();
};

const onPageSizeChange = (pageSize: number) => {
  pagination.value.pageSize = pageSize;
  fetchAccountData();
};

onMounted(() => {
  fetchAccountData();
});
</script>

<style scoped>
.account-statistics {
  padding: 16px;
}

.stat-card {
  height: 100%;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  line-height: 1.5;
}

.chart-card {
  height: 100%;
}

.chart {
  height: 400px;
}

.mt-4 {
  margin-top: 16px;
}

.text-primary {
  color: rgb(var(--primary-6));
}

.text-success {
  color: rgb(var(--success-6));
}

.text-warning {
  color: rgb(var(--warning-6));
}
</style>
