<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <viewlog-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="syliveViewlogTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            class="ele-btn-icon"
            icon="el-icon-download"
            @click="exportData"
          >
            导出
          </el-button>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
  import { setPageTabTitle } from '@/utils/page-tab-util';
  import ViewlogSearch from './components/viewlog-search.vue';
  import { listViewlog, exportViewlog } from '@/api/sylive/live';
  import { utils, writeFile } from 'xlsx';
  const ROUTE_PATH = '/sylive/live/viewlog';

  export default {
    name: 'syliveLiveViewlog',
    components: { ViewlogSearch },
    data() {
      return {
        activityId: null,
        sessionId: null,
        // 加载状态
        title: '观看日志',
        loading: true,
        // 表格列配置
        columns: [
          {
            prop: 'userId',
            label: 'userId',
            minWidth: 45,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'uname',
            label: '昵称',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'createTime',
            label: '观看开始时间',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'playDuration',
            label: '观看时长',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'consultant',
            label: '顾问',
            showOverflowTooltip: true,
            minWidth: 60
          },
          {
            prop: 'levelName1',
            label: '战区',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'levelName2',
            label: '战队',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'stores',
            label: '门店',
            showOverflowTooltip: true,
            minWidth: 80
          }
        ]
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        const activityId = this.activityId;
        const sessionId = this.sessionId;
        return listViewlog({
          ...where,
          ...order,
          page,
          limit,
          activityId,
          sessionId
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 导出数据 */
      exportData() {
        const array = [
          [
            'userId',
            '昵称',
            '观看开始时间',
            '观看时长',
            '顾问',
            '战区',
            '战队',
            '门店'
          ]
        ];
        const loading = this.$loading({ lock: true });
        this.$refs.table.doRequest(({ where, order }) => {
          const activityId = this.activityId;
          exportViewlog({ ...where, ...order, activityId })
            .then((data) => {
              loading.close();
              data.forEach((d) => {
                array.push([
                  d.userId,
                  d.uname,
                  this.$util.toDateString(d.createTime),
                  d.playDuration,
                  d.consultant,
                  d.levelName1,
                  d.levelName2,
                  d.stores
                ]);
              });
              writeFile(
                {
                  SheetNames: ['Sheet1'],
                  Sheets: {
                    Sheet1: utils.aoa_to_sheet(array)
                  }
                },
                this.title + '.xlsx'
              );
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        });
      }
    },
    watch: {
      $route: {
        handler(route) {
          const { path } = route;
          if (path !== ROUTE_PATH) {
            return;
          }
          const activityId = this.$route.query.id;
          const sessionId = this.$route.query.sessionId;
          if (
            !activityId ||
            (activityId == this.activityId && sessionId == this.sessionId)
          ) {
            return;
          }
          this.activityId = activityId;
          this.sessionId = sessionId;
          if (this.sessionId) {
            this.title = '场次观看日志';
          } else {
            this.title = '频道观看日志';
          }
          setPageTabTitle(this.title);
          if (this.$refs.table) {
            this.$refs.table.reload({ page: 1 });
          }
        },
        immediate: true
      }
    }
  };
</script>
