<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :size="ELsize" :inline="true" v-show="showSearch">
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <template v-for="item in OrderStatusOption">
            <el-option :label="item.label" :value="item.value" />
          </template>
        </el-select>
      </el-form-item>

      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable>
          <template v-for="item in PayTypeOption">
            <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="payTime">
        <el-date-picker v-model="queryParams.payTime" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker v-model="queryParams.refundTime" 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="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" :size="ELsize" @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">
        <span :class="['table-top-type invariant-content', ELsize]">
          今日收款：<span class="table-top-num">{{ StatisticsData.todayAmount || 0 }}</span>
        </span>
        <span :class="['table-top-type', ELsize]">
          补贴完成：<span class="table-top-num">{{ StatisticsData.subsidyComplete || 0 }}</span>
        </span>
        <span :class="['table-top-type', ELsize]">
          累计收款：<span class="table-top-num">{{ StatisticsData.totalAmount || 0 }}</span>
        </span>
        <span :class="['table-top-type', ELsize]">
          已退款：<span class="table-top-num">{{ StatisticsData.totalRefund || 0 }}</span>
        </span>
      </div>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="handleRefreshPage"></right-toolbar>
    </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>
      <el-table-column label="订单号" align="center" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.orderNo | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.payType | PayTypeFilter | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" align="center" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.userMobile | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center">
        <template slot-scope="{ row }">
          {{ row.payAmount | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center">
        <template slot-scope="{ row }">
          {{ row.payTime | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="已补贴金额" align="center">
        <template slot-scope="{ row }">
          {{ row.actualSubsidyAmount | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center">
        <template slot-scope="{ row }">
          {{ row.refundTime | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="已退金额" align="center">
        <template slot-scope="{ row }">
          {{ row.refundAmount | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" width="120">
        <template slot-scope="{ row }">
          <StatusTag :status-vo="row.orderStatus | OrderStatusFilter(true)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="{row}">
          <el-button :size="ELTableSize" type="text" @click="$refs.offlineOrdersDetailsRef.openHandle(row)">查看</el-button>
          <template v-if="getIsRefund(row.orderStatus)">
            <el-button :size="ELTableSize" type="text" @click="$refs.offlineOrdersRefundRef.openHandle(row)">退款</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 查看 -->
    <OfflineOrdersDetails ref="offlineOrdersDetailsRef" />
    <!-- 退款 -->
    <OfflineOrdersRefund ref="offlineOrdersRefundRef" @success="handleRefreshPage" />
  </div>
</template>
<script>
import { PayTypeOption, OrderStatusOption, } from "@/utils/statusEnum.js";
import { getOnlineorderBusinessStatisticsApi, getOnlineorderBusinessPageApi, exportBusinessOnlineOrderApi } from "@/api/OnlineOrder";
import getListApi from "@/mixins/getListApi";
import OfflineOrdersDetails from "./components/offlineOrdersDetails.vue";
import OfflineOrdersRefund from "./components/offlineOrdersRefund.vue";

export default {
  mixins: [getListApi],
  components: { OfflineOrdersDetails, OfflineOrdersRefund },
  data() {
    return {
      OrderStatusOption,
      PayTypeOption,
      listURL: getOnlineorderBusinessPageApi,
      queryParams: {
        orderStatus: undefined,
        payType: undefined,
        orderNo: undefined,
        payTime: undefined,
        businessName: undefined,
        userMobile: undefined
      },
      // 日期范围字段映射配置
      dateRangeMapping: {
        // 日期范围字段名: { start: 开始时间字段名, end: 结束时间字段名 }
        payTime: { start: 'payStartTime', end: 'payEndTime' },
        refundTime: { start: 'refundStartTime', end: 'refundEndTime' }
      },
      StatisticsData: {},

      exportObj: {
        url: exportBusinessOnlineOrderApi,
        name: '店铺线下订单列表'
      }
    };
  },
  methods: {
    handleRefreshPage() {
      this.getStatistics();
      this.getList();
    },
    // 获取补贴队列业务统计
    async getStatistics() {
      try {
        // 处理日期范围参数
        const Params = this.processDateRangeParams({
          ...this.queryParams,
          ...this.search,
        });
        const { data } = await getOnlineorderBusinessStatisticsApi(Params);
        this.StatisticsData = data || {};
      } catch (error) {
        this.StatisticsData = {};
      }
    },

    getIsRefund(orderStatus) {
      const item = OrderStatusOption.find(item => item.value === orderStatus);
      return item?.isRefund || false;
    },

  },
};
</script>