<template>
  <div class="app-container">
    <div class="reward-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="公司名" prop="companyId">
          <el-select filterable  v-model="queryParams.companyId" placeholder="请选择公司名"  @change="companyChange" clearable size="small">
            <el-option
              v-for="item in companys"
              :key="item.companyId"
              :label="item.companyName"
              :value="item.companyId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="查询日期" prop="step">
          <el-date-picker
            type="month"
            v-model="selectedTimeStep"
            @change="handleTimeStepChange"
            placeholder="选择一个日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <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>
        </el-form-item>
      </el-form>
      <!-- 时间步长选择 -->
      <span class="demonstration">月份选择（可多选）</span>

    </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 label="公司主题" prop="companyName" 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'
import Treeselect from "@riophae/vue-treeselect";
import {getCompanyList} from "@/api/company/company";

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

    },
    initData() {
      if(this.queryParams.companyId == null) {
        this.$message.error('请选择公司名')
        return
      }
      this.queryParams.step=[]
      this.queryParams.step.push(this.selectedTimeStep);
      statistics(this.queryParams).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, companyName: liveItem.companyName }
      })
    },
    handleTimeStepChange() {
      // this.initData()
    },
  }
}
</script>

<style scoped>

</style>
