<template>
    <Drawer :title="DrawerTitle" :visible.sync="DrawerVisible" :showFooter="true" :skeleton="skeletonLoading"
        :wrapperClosable="true" :size="$drawerWidth(1500)">
        <el-form :model="queryParams" ref="queryForm" :inline="true" :size="ELsize" label-width="auto">
            <el-form-item label="日期选择" prop="billDay">
                <el-date-picker v-model="queryParams.billDay" value-format="yyyy-MM-dd" type="daterange"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
            </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', ELsize]">
                        {{ item.label }}：<span class="table-top-num">{{ StatisticsData[item.field] || 0 }}</span>
                    </span>
                </template>
            </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>
            <template v-for="item in TableColumn">
                <el-table-column :label="item.label" :align="item.align || 'center'" :width="item.width">
                    <template slot-scope="{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" />


        <template v-slot:footer>
            <el-button :size="ELsize" @click.stop="DrawerVisible = false">取消</el-button>
        </template>
    </Drawer>
</template>

<script>
import { getBusinessDayBillPageApi, getBusinessDayBillStatisticsApi, getBusinessDayBillExportApi } from "@/api/financeManage";
import getListApi from "@/mixins/getListApi";
export default {
    name: '',
    mixins: [getListApi],
    data() {
        return {
            DrawerTitle: '连锁店经营数据',
            DrawerVisible: false,
            skeletonLoading: false,

            listURL: getBusinessDayBillPageApi,
            queryParams: {
                billDay: undefined,
            },
            // 日期范围字段映射配置
            dateRangeMapping: {
                // 日期范围字段名: { start: 开始时间字段名, end: 结束时间字段名 }
                billDay: { start: 'startTime', end: 'endTime' }
            },
            initializationShow: false,

            TableColumn: [
                {
                    label: '日期',
                    prop: 'dayValue',
                    width: 160
                },
                {
                    label: '实际收款',
                    prop: 'actualReceivePayment'
                },
                {
                    label: '实际到账',
                    prop: 'actualCredited'
                },
                {
                    label: '用户消耗积分',
                    prop: 'userUsePoint'
                },
                {
                    label: '店铺发放积分',
                    prop: 'businessGivePoint'
                },
                {
                    label: '在线订单数',
                    prop: 'onlineOrderNum'
                },
                {
                    label: '商城订单数',
                    prop: 'shopOrderNum'
                },
                {
                    label: '在线订单额',
                    prop: 'onlineOrderAmount'
                },
                {
                    label: '商城订单额',
                    prop: 'shopOrderAmount'
                },
                {
                    label: '商家让利',
                    prop: 'businessShouldSubsidy'
                },
                {
                    label: '预估今日结算',
                    prop: 'todaySettlement'
                }
            ],

            statisticsList: [
                {
                    label: '实际收款额',
                    field: 'actualReceivePayment'
                },
                {
                    label: '在线订单额',
                    field: 'onlineOrderAmount'
                },
                {
                    label: '商城订单额',
                    field: 'shopOrderAmount'
                },
                {
                    label: '商家让利',
                    field: 'businessShouldSubsidy'
                },
                {
                    label: '累计结算',
                    field: 'todaySettlement'
                }
            ],
            StatisticsData: {},


            exportObj: {
                url: getBusinessDayBillExportApi,
                name: '账目明细'
            }

        };
    },
    created() {
        // setTimeout(() => {
        //     this.openHandle({ businessId: 22, businessName: "总部国际专卖店" })
        // }, 100);
    },

    methods: {
        async openHandle(row) {
            this.DrawerVisible = true;
            const { businessId, businessName } = row || {};
            this.queryParams.businessId = businessId;
            this.queryParams.businessName = businessName;
            this.DrawerTitle = `${businessName || ''}经营数据`;
            this.exportObj.name = this.DrawerTitle;
            this.handleQuery();
        },
        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 getBusinessDayBillStatisticsApi(Params);
                this.StatisticsData = data || {};
            } catch (error) {
                console.log('获取连锁店经营统计失败:', error);
                this.StatisticsData = {};
            }
        },
    },
};
</script>