<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <order-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="syliveOrderTable"
      >
        <!-- 表头工具栏 -->
        <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 OrderSearch from './components/order-search.vue';
  import { getActivity } from '@/api/sylive/activity';
  import { listOrder, exportOrder } from '@/api/sylive/order';
  import { utils, writeFile } from 'xlsx';
  const ROUTE_PATH = '/sylive/order';

  export default {
    name: 'syliveOrder',
    components: { OrderSearch },
    data() {
      return {
        // 加载状态
        title: '权益订单',
        loading: true,
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'uname',
            label: '姓名',
            showOverflowTooltip: true,
            minWidth: 60
          },
          {
            prop: 'mobile',
            label: '手机号',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'itemTitle',
            label: '商品标题',
            showOverflowTooltip: true,
            minWidth: 130
          },
          {
            prop: 'totalPrice',
            label: '订单价格',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 70
          },
          {
            prop: 'payTime',
            label: '付款时间',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'createTime',
            label: '创建时间',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'area',
            label: '区域',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'stores',
            label: '门店',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'consultant',
            label: '顾问',
            showOverflowTooltip: true,
            minWidth: 60
          },
          {
            prop: 'typeName',
            label: '订单类型',
            showOverflowTooltip: true,
            minWidth: 70
          },
          {
            prop: 'cfromName',
            label: '来源',
            showOverflowTooltip: true,
            minWidth: 50
          },
          {
            prop: 'statusName',
            label: '状态',
            showOverflowTooltip: true,
            minWidth: 60
          }
        ]
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        const activityId = this.$route.query.id;
        return listOrder({ ...where, ...order, page, limit, activityId });
      },
      query() {
        const activityId = this.$route.query.id;
        if (!activityId) {
          return;
        }
        this.loading = true;
        getActivity(Number(activityId))
          .then((data) => {
            this.loading = false;
            // 修改页签标题
            if (this.$route.path === ROUTE_PATH) {
              this.title = data.title + '的订单';
              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;
          exportOrder({ ...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;
          }
          this.query();
          if (this.$refs.table) {
            this.$refs.table.reload({ page: 1 });
          }
        },
        immediate: true
      }
    }
  };
</script>
