<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" :size="ELsize" label-width="auto">
      <el-form-item label="店铺名称" prop="businessName">
        <el-input v-model="queryParams.businessName" placeholder="请输入店铺名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option label="全部" :value="undefined" />
          <template>
            <el-option v-for="item in OrderStatusOption" :key="item.value" :label="item.label" :value="item.value" />
          </template>
        </el-select>
      </el-form-item>

      <el-form-item label="支付方式" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付方式" clearable>
          <el-option label="全部" :value="undefined" />
          <template>
            <el-option v-for="item in PayTypeOption" :key="item.value" :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" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery('queryForm')">重置</el-button>
        <!-- <el-button type="primary" size="mini">导出</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>
      <el-table-column label="店铺名" align="center" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.businessName | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center">
        <template slot-scope="{ row }">
          {{ row.orderNo | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center">
        <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="100">
        <template slot-scope="{ row }">
          {{ row.orderStatus | OrderStatusFilter | TableItemEmpty }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="{row}">
          <el-button type="text" @click="$refs.OnlineDetailsRef.openHandle(row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 查看订单详情弹窗 -->
    <online-details ref="OnlineDetailsRef" />
  </div>
</template>
<script>
import getListApi from "@/mixins/getListApi";
import { getChainStoreOnlineorderPageApi, exportChainStoreOnlineorderApi, getChainStoreOnlineorderStatisticsApi } from "@/api/chainStore";
import { PayTypeOption, OrderStatusOption } from "@/utils/statusEnum.js";
import OnlineDetails from "@/views/chainStore/components/onlineDetails.vue";
export default {
  mixins: [getListApi],
  components: { OnlineDetails },
  data() {
    return {
      OrderStatusOption,
      PayTypeOption,
      listURL: getChainStoreOnlineorderPageApi,
      queryParams: {
        businessName: undefined,
        orderStatus: undefined,
        payStatus: undefined,
        orderNo: undefined,
        payTime: undefined,
        refundTime: undefined
      },
      // 日期范围字段映射配置
      dateRangeMapping: {
        // 日期范围字段名: { start: 开始时间字段名, end: 结束时间字段名 }
        payTime: { start: 'payStartTime', end: 'payEndTime' },
        refundTime: { start: 'refundStartTime', end: 'refundEndTime' }
      },
      exportObj: {
        url: exportChainStoreOnlineorderApi,
        name: '提现列表'
      },
      StatisticsData: {},
      statisticsList: [
        {
          label: '今日收款',
          field: 'todayAmount',
          isInvariant: true
        },
        {
          label: '补贴完成',
          field: 'subsidyComplete'
        },
        {
          label: '累计收款',
          field: 'totalAmount'
        },
        {
          label: '已退款',
          field: 'totalRefund'
        }],

    };
  },
  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 getChainStoreOnlineorderStatisticsApi(Params);
        this.StatisticsData = data || {};
      } catch (error) {
        this.StatisticsData = {};
      }
    },
  },
};
</script>
<style scoped lang="scss">
.tab-pane {
  display: flex;
  flex-wrap: wrap;

  .tab-flex-row {
    display: flex;
    align-items: center;
    margin-right: 30px;

    .value {
      color: #ffb017;
    }
  }
}

.flex-col {
  padding: 0 20px;

  .flex-row {
    margin-bottom: 15px;

    .lable {
      width: 120px;
      display: inline-block;
    }

    .value {
      flex: 1;
      color: #666;
    }
  }
}
</style>
