<template>
    <ele-page flex-table>
        <page-search @search="reload" />
        <common-table
            ref="pageRef"
            :pageConfig="pageConfig"
            :columns="columns"
            :tools="false"
            show-summary
            :flex-table="false"
            :summary-method="getSummaries"
        >
            <template #toolbar="{ row }">
                <div>
                    <el-button
                        type="success"
                        plain
                        v-permission="'statistic:defect:export'"
                        @click="handleExportExcel(row)"
                    >
                        导出EXCEL
                    </el-button>
                </div>
            </template>
        </common-table>
    </ele-page>
</template>

<script setup>
    import { ref, reactive, getCurrentInstance } from 'vue';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import pageSearch from './components/page-search.vue';
    const { proxy } = getCurrentInstance();
    defineOptions({ name: 'StatisticDefect' });

    /** 表格列配置 */
    const columns = ref([
        { type: 'index', label: '#', align: 'center', width: 80 },
        { label: '地区', prop: 'area', align: 'center' },
        { label: '收货仓库', prop: 'godownName', align: 'center' },
        {
            label: '不良率(%)',
            prop: 'badRatio',
            align: 'center'
        },
        { label: '统计日期', prop: 'statDate', align: 'center' }
    ]);

    /** 页面组件实例 */
    const pageRef = ref(null);

    const pageConfig = reactive({
        pageUrl: '/order/ordercheck/stat/badRationStat',
        exportUrl: '/order/ordercheck/stat/badRationStatExport',
        fileName: '不良统计',
        cacheKey: 'statisticDefectTable'
    });

    //获取不良率平均值
    function getBadRatioAverage(params = {}) {
        proxy.$http
            .get('/order/ordercheck/stat/badRationAverage', { params })
            .then((res) => {
                if (res.data.code === 200) {
                    badRatio.value = res.data.data;
                }
            });
    }

    getBadRatioAverage();

    //刷新表格
    const badRatio = ref('-');
    function reload(where) {
        pageRef.value?.reload(where);
        getBadRatioAverage(where);
    }

    /**
     * 自定义表格汇总计算方法
     * @param {Object} param0 - 表格数据和列信息
     * @returns {Array} - 汇总行显示的数据
     */
    function getSummaries({ columns, data }) {
        const sums = [];
        columns.forEach((column, index) => {
            if (index === 0) {
                sums[index] = '平均';
                return;
            }

            const values = data.map((item) => Number(item[column.property]));

            if (!values.length) {
                sums[index] = 'N/A';
                return;
            }

            switch (column.property) {
                case 'badRatio':
                    sums[index] = badRatio.value;
                    break;
                default:
                    sums[index] = '';
            }
        });

        return sums;
    }

    //导出excel
    function handleExportExcel() {
        pageRef.value?.exportData('不良统计');
    }
</script>
