<template>
  <div class="app-container">
    <div class="reward-container">
      <!-- 时间步长选择 -->
      <span class="demonstration">月份选择（可多选）</span>
      <el-date-picker
        type="month"
        v-model="selectedTimeStep"
        format="yyyy-MM"
        @change="handleTimeStepChange"
        placeholder="选择一个或多个日期">
      </el-date-picker>

      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        :loading="exportLoading"
        @click="handleExport"
        style="margin-left: 10px"
        v-hasPermi="['live:liveRewardRecord:export']"
      >导出</el-button>



    </div>

    <!-- 直播间数据表格 + 总消耗 -->
    <div style="margin-top: 24px;">
      <h3 style="font-size: 16px; font-weight: 600; margin-bottom: 12px;">直播间积分消耗明细</h3>
      <el-table
        :data="tableData"
        border
        show-summary
        stripe
        style="width: 100%; margin-bottom: 12px;"
      >
        <el-table-column label="直播名称" prop="liveNameAndId" width="100" align="center" />
        <el-table-column
          v-for="(date, index) in timeSeries"
          :key="index"
          :label="date"
          align="center"
        >
          <template #default="scope">
            {{ scope.row.dailyData[index] || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="单直播间总计" prop="total" width="120" align="center" />
      </el-table>
    </div>
  </div>
</template>

<script>
import { statistics,exportLiveIntegral } from '@/api/live/liveRewardRecord'

export default {
  name: 'liveRewardRecord',
  data() {
    return {
      exportLoading: false,
      selectedTimeStep: [],
      tableData: [],
      chartSeries: [],
      timeSeries: [],
      colorList: [
        '#34a853', '#fbbc05', '#4285f4', '#ea4335', '#9c27b0',
        '#ff9800', '#673ab7', '#e91e63', '#00bcd4', '#8bc34a'
      ]
    }
  },
  computed: {
  },
  mounted() {
    this.selectedTimeStep.push(new Date())
    this.initData(this.selectedTimeStep)
  },
  methods: {
    handleExport(){
      if(this.selectedTimeStep == null || this.selectedTimeStep.length < 1) {
        this.$message.error('请选择导出月份')
        return
      }
      var date = []
      if (this.selectedTimeStep == null || this.selectedTimeStep.length === 0) {
        date.push(new Date());
      } else {
        date = this.selectedTimeStep
      }
      this.$confirm('是否确认导出所有直播积分记录?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportLiveIntegral({ 'step': date, companyId:0});
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    initData(date) {
      statistics({ 'step': date, companyId:0}).then(res => {
        this.timeSeries = res.timeSeries || []
        this.tableData = res.data || []


        this.processTableData()
      }).catch(err => {
        console.error('数据加载失败:', err)
        this.$message.error('数据加载失败，请重试')
      })
    },
    processTableData() {
      const liveList = this.tableData
      this.tableData = liveList.map(liveItem => {
        const dailyData = liveItem.dataList || []
        const total = dailyData.reduce((sum, num) => sum + (num || 0), 0)
        return { liveNameAndId: liveItem.liveNameAndId, dailyData, total }
      })
    },
    handleTimeStepChange() {
      const date = [];
      date.push(this.selectedTimeStep)
      this.initData(date)
    },
  }
}
</script>

<style>
/* 基础布局样式 */
.reward-container {
  padding: 20px;
  background-color: #fff;
}
.charts-container {
  display: flex;
  gap: 24px;
  margin-bottom: 24px;
}
.chart-wrapper {
  flex: 1;
}
</style>
