<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <live-search :channelId="channelId" @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :need-page="false"
        cache-key="syliveLiveTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            class="ele-btn-icon"
            icon="el-icon-download"
            @click="exportData"
            v-show="false"
          >
            导出
          </el-button>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-view"
            @click="openSession('view', row)"
          >
            观看日志
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openSession('viewApi', row)"
          >
            同步观看
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 同步观看弹窗 -->
    <live-viewapi :data="current" :visible.sync="showViewApi" @done="reload" />
  </div>
</template>

<script>
  import { setPageTabTitle } from '@/utils/page-tab-util';
  import LiveSearch from './components/live-search.vue';
  import { getActivity } from '@/api/sylive/activity';
  import { listLive, exportLive } from '@/api/sylive/live';
  import { utils, writeFile } from 'xlsx';
  import LiveViewapi from './components/live-viewapi';
  const ROUTE_PATH = '/sylive/live';

  export default {
    name: 'syliveLive',
    components: { LiveViewapi, LiveSearch },
    data() {
      return {
        activityId: null,
        title: '直播数据',
        // 当前编辑数据
        current: {
          activityId: '',
          channelId: '',
          sessionId: '',
          sessionTime: '',
          viewlogTime: ''
        },
        channelId: null,
        loading: true,
        showViewApi: false,
        // 表格列配置
        columns: [
          {
            prop: 'sessionId',
            label: '场次ID',
            showOverflowTooltip: true,
            minWidth: 60
          },
          {
            prop: 'startTime',
            label: '开始时间',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'endTime',
            label: '结束时间',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'duration',
            label: '直播时长',
            showOverflowTooltip: true,
            minWidth: 70
          },
          {
            prop: 'liveUV',
            label: '观看人数',
            showOverflowTooltip: true,
            minWidth: 70
          },
          {
            prop: 'livePV',
            label: '观看次数',
            showOverflowTooltip: true,
            minWidth: 70
          },
          {
            prop: 'totalPlayDuration',
            label: '观看时长',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'sessionTime',
            label: '同步场次时间',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'viewlogTime',
            label: '同步观看时间',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action'
          }
        ]
      };
    },
    methods: {
      openSession(command, row) {
        if (command == 'view') {
          const to1 = '/sylive/live/viewlog?id=' + this.activityId;
          const to2 = '&sessionId=' + row.sessionId;
          this.$router.replace(to1 + to2);
        } else {
          row.activityId = this.activityId;
          this.current = row;
          this.showViewApi = true;
        }
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        const activityId = this.activityId;
        return listLive({ ...where, ...order, page, limit, activityId });
      },
      activityQuery() {
        this.loading = true;
        getActivity(this.activityId)
          .then((data) => {
            this.loading = false;
            // 修改页签标题
            if (this.$route.path === ROUTE_PATH) {
              this.title = data.title + '的直播数据';
              this.channelId = data.channelId;
              setPageTabTitle(this.title);
            }
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 导出数据 */
      exportData() {
        const array = [
          [
            '订单号',
            '姓名',
            '手机号',
            '商品标题',
            '订单价格',
            '付款时间',
            '创建时间',
            '区域',
            '门店',
            '顾问',
            '订单类型',
            '来源',
            '状态',
            '所选经销商',
            '地址'
          ]
        ];
        const loading = this.$loading({ lock: true });
        this.$refs.table.doRequest(({ where, order }) => {
          const activityId = this.$route.query.id;
          exportLive({ ...where, ...order, activityId })
            .then((data) => {
              loading.close();
              data.forEach((d) => {
                array.push([
                  d.sid,
                  d.uname,
                  d.mobile,
                  d.itemTitle,
                  d.totalPrice,
                  this.$util.toDateString(d.payTime),
                  this.$util.toDateString(d.createTime),
                  d.area,
                  d.stores,
                  d.consultant,
                  d.typeName,
                  d.cfromName,
                  d.statusName,
                  d.biz,
                  d.address
                ]);
              });
              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;
          if (!activityId || activityId == this.activityId) {
            return;
          }
          this.activityId = activityId;
          this.activityQuery();
          if (this.$refs.table) {
            this.$refs.table.reload({ page: 1 });
          }
        },
        immediate: true
      }
    }
  };
</script>
