<!--
 * @Author: Fly
 * @Date: 2025-12-10 10:06:13
 * @LastEditTime: 2025-12-22 15:15:23
 * @Description: 
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="店铺名称" prop="currentBusinessName">
        <el-input v-model="queryParams.currentBusinessName" placeholder="请输入" clearable :size="ELsize"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户手机号" prop="userMobile" label-width="90px">
        <el-input v-model="queryParams.userMobile" placeholder="请输入" clearable :size="ELsize"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入" clearable :size="ELsize"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="subsidyStatus">
        <el-select v-model="queryParams.subsidyStatus" placeholder="请选择" clearable :size="ELsize" style="width: 240px">
          <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 :size="ELsize"
          @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" :size="ELsize" @click.stop="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" :size="ELsize" @click.stop="resetQuery('queryForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-radio-group :size="ELsize" v-model="TableType">
          <template v-for="item in orderTypeOption">
            <el-radio-button :label="item.value">{{ item.label }}</el-radio-button>
          </template>
        </el-radio-group>
      </el-col>
      <!-- 插队订单 -->
      <template v-if="getCutInType(TableType)">
        <el-col :span="1.5">
          <el-button type="primary" :size="ELsize" @click.stop="$refs.CutInLineRef.newQuotaCutIn()">定额插队</el-button>
        </el-col>
      </template>
      <!-- 排队订单 -->
      <template v-else>
        <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">
          <span :class="['table-top-type invariant-content', ELsize]">
            今日补贴：<span class="table-top-num">{{ QueueStatisticsVo.todaySubsidy || 0 }}</span>
          </span>
          <span :class="['table-top-type', ELsize]">
            补贴完成：<span class="table-top-num">{{ QueueStatisticsVo.subsidyComplete || 0 }}</span>
          </span>
          <span :class="['table-top-type', ELsize]">
            排队待补贴：<span class="table-top-num">{{ QueueStatisticsVo.waitSubsidy || 0 }}</span>
          </span>
          <span :class="['table-top-type', ELsize]">
            已补贴积分：<span class="table-top-num">{{ QueueStatisticsVo.subsidyIntegral || 0 }}</span>
          </span>
        </div>
      </template>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="handleRefreshPage" />
    </el-row>


    <!-- 插队订单 -->
    <template v-if="getCutInType(TableType)">
      <CutInLine ref="CutInLineRef" :queryParams="queryParams" :data-list="dataList" :loading="loading"
        :refreshFn="handleRefreshPage" />
    </template>
    <!-- 排队订单 -->
    <template v-else>
      <LineUp ref="LineUpRef" :queryParams="queryParams" :data-list="dataList" :loading="loading"
        :refreshFn="handleRefreshPage" />
    </template>
    <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.js';
import LineUp from './components/lineUp.vue';
import CutInLine from './components/cutInLine.vue'
import { SubsidyStatusOption } from '@/utils/statusEnum.js';
import { orderTypeOption } from './components/data.js';
import { getSubsidyCutInPageApi, getSubsidyQueueStatisticsApi, getSubsidyQueuePageApi, exportSubsidyQueueApi } from '@/api/OnlineOrder.js';
export default {
  mixins: [getListApi],
  components: { LineUp, CutInLine },
  data() {
    return {
      SubsidyStatusOption,
      orderTypeOption,
      listURL: null,
      TableType: 1, //  订单类型
      showSearch: true,
      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' }
      },
      initializationShow: false,

      QueueStatisticsVo: {},

      exportObj: {
        url: exportSubsidyQueueApi,
        name: '平台补贴排队列表'
      }
    }
  },
  watch: {
    TableType: {
      handler(newVal, oldVal) {
        this.listURL = newVal === 1 ? getSubsidyQueuePageApi : getSubsidyCutInPageApi;
        this.initData();
      },
      immediate: true
    }
  },
  methods: {
    initData() {
      this.dataList = [];
      this.handleQuery();
    },
    handleRefreshPage() {
      if (!this.getCutInType()) {
        this.getSubsidyQueueStatistics();
      }
      this.getList();
    },
    async getSubsidyQueueStatistics() {
      try {
        // 处理日期范围参数
        const Params = this.processDateRangeParams({
          ...this.queryParams,
          ...this.search,
        });
        const { data } = await getSubsidyQueueStatisticsApi(Params);
        this.QueueStatisticsVo = data || {};
      } catch {
        this.QueueStatisticsVo = {};
      }
    },
    // 判断是否插队
    getCutInType(type) {
      return type === orderTypeOption[1].value ? true : false;
    }
  }
}
</script>
