<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto" :size="ELsize">
      <el-form-item label="店铺名称" prop="currentBusinessName">
        <el-input v-model="queryParams.currentBusinessName" placeholder="请输入店铺名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="用户手机号" prop="userMobile">
        <el-input v-model="queryParams.userMobile" placeholder="请输入用户手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="用户姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="排队状态" prop="subsidyStatus">
        <el-select v-model="queryParams.subsidyStatus" placeholder="请选择排队状态" clearable>
          <el-option label="全部" :value="undefined" />
          <template v-for="item in SubsidyStatusOption">
            <el-option :label="item.label" :value="item.value" />
          </template>
        </el-select>
      </el-form-item>

      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="消费时间" prop="consumeTime">
        <el-date-picker v-model="queryParams.consumeTime" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item label="排队开始时间" prop="queueTime">
        <el-date-picker v-model="queryParams.queueTime" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery('queryForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" :size="ELsize" @click="handleExport">导出</el-button>
      </el-col>
      <div class="table-top-statistics">
        <template v-for="item in statisticsList">
          <span :class="['table-top-type', item.isInvariant ? 'invariant-content' : '', ELsize]">
            {{ item.label }}：<span class="table-top-num">{{ StatisticsData[item.field] || 0 }}</span>
          </span>
        </template>
      </div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="handleRefreshPage" />
    </el-row>
    <el-table v-loading="loading" :border="TableBorder" :size="ELTableSize" :data="dataList">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="{$index}">
          {{ $getTableIndex(queryParams, $index + 1) }}
        </template>
      </el-table-column>
      <template v-for="item in TableColumn">
        <el-table-column :label="item.label" :align="item.align || 'center'">
          <template slot-scope="{row}">
            {{ (item.formatter ? item.formatter(row) : row[item.prop]) | TableItemEmpty }}
          </template>
        </el-table-column>
      </template>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>
<script>
import getListApi from "@/mixins/getListApi";
import { SubsidyStatusOption } from "@/utils/statusEnum.js";
import { getChainSubsidyQueuePageApi, exportChainSubsidyQueueApi, getChainSubsidyQueueStatisticsApi } from "@/api/chainStore";
export default {
  mixins: [getListApi],
  data() {
    return {
      SubsidyStatusOption,
      listURL: getChainSubsidyQueuePageApi,
      queryParams: {
        currentBusinessName: undefined,
        userMobile: undefined,
        userName: undefined,
        subsidyStatus: undefined,
        orderNo: undefined,
        consumeTime: undefined,
        queueTime: undefined
      },
      // 日期范围字段映射配置
      dateRangeMapping: {
        // 日期范围字段名: { start: 开始时间字段名, end: 结束时间字段名 }
        consumeTime: { start: 'consumeStartTime', end: 'consumeEndTime' },
        queueTime: { start: 'queueStartTime', end: 'queueEndTime' }
      },
      exportObj: {
        url: exportChainSubsidyQueueApi,
        name: '连锁店排队订单导出'
      },
      TableColumn: [
        {
          label: '店铺名称',
          prop: 'currentBusinessName'
        },
        {
          label: '订单编号',
          prop: 'orderNo'
        },
        {
          label: '当前队序',
          prop: 'queueOrder'
        },
        {
          label: '最多补贴金额',
          prop: 'shouldSubsidyAmount'
        },
        {
          label: '已补贴金额',
          prop: 'actualSubsidyAmount'
        },
        {
          label: '用户手机号',
          prop: 'userMobile'
        },
        {
          label: '用户姓名',
          prop: 'userName'
        },
        {
          label: '消费时间',
          prop: 'consumeTime'
        },
        {
          label: '排队开始时间',
          prop: 'queueTime'
        },
        {
          label: '排队状态',
          prop: 'subsidyStatus',
          formatter: (row) => {
            const { subsidyStatus } = row;
            try {
              return this.$options.filters.SubsidyStatusOptionFilter(subsidyStatus) || subsidyStatus;
            } catch (error) {
              return subsidyStatus;
            }
          }
        }
      ],
      StatisticsData: {},
      statisticsList: [
        {
          label: '今日补贴',
          field: 'todaySubsidy',
          isInvariant: true
        },
        {
          label: '补贴完成',
          field: 'subsidyComplete'
        },
        {
          label: '排队待补贴',
          field: 'waitSubsidy'
        },
        {
          label: '已补贴积分',
          field: 'subsidyIntegral'
        }],
    };
  },
  methods: {
    handleRefreshPage() {
      this.getStatistics();
      this.getList();
    },
    // 获取补贴队列业务统计
    async getStatistics() {
      try {
        const Params = this.processDateRangeParams({
          ...this.queryParams,
          ...this.search,
        });
        Params.pageNum && delete Params.pageNum;
        Params.pageSize && delete Params.pageSize;
        const { data } = await getChainSubsidyQueueStatisticsApi(Params);
        this.StatisticsData = data || {};
      } catch (error) {
        console.log('error = ' , error);
        this.StatisticsData = {};
      }
    },
  },
};
</script>
<style scoped lang="scss"></style>