<template>
    <div class="ele-body">
        <a-card :bordered="false">
            <!-- 搜索表单 -->
            <a-form :model="where" layout="inline">
                <a-form-item label="区域等级:">
                    <a-select v-model:value="where.regionLevel">
                        <a-select-option
                            v-for="(item) in modelOptions"
                            :key="item.value"
                        >{{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-item>

                <a-form-item label="起始日期:">
                    <a-date-picker valueFormat="YYYY-MM-DD 00:00:00" v-model:value="where.timeStart"></a-date-picker>
                </a-form-item>
                <a-form-item label="结束日期:">
                    <a-date-picker valueFormat="YYYY-MM-DD 23:59:59" v-model:value="where.timeEnd"></a-date-picker>
                </a-form-item>

                <a-space>
                    <a-button type="primary" @click="reload">查询</a-button>
                    <a-button @click="reset">重置</a-button>
                    <a-button @click="exportFile">导出Excel</a-button>
                </a-space>

            </a-form>
            <!-- 表格 -->
            <ele-pro-table
                ref="table"
                row-key="ambientAirId"
                :need-page="false"
                :datasource="url"
                :columns="columns"
                :where="where"
                :scroll="{ x: 'max-content' }"
                :init-load="false"
                @done="(d) => (data = d.data)"
                method="POST"
            >

            </ele-pro-table>
        </a-card>
    </div>
    <!-- 编辑弹窗 -->
</template>

<script>
import _ from "lodash";
import XLSX from "xlsx";
import {statisticAreaBase, getColumnOptions} from "@/api/ecology/atmosphere/air";
// import { Modal } from "ant-design-vue";
import locale from "ant-design-vue/es/date-picker/locale/zh_CN";
import moment from "moment";

//   import utils from "./utils";
export default {
    name: "StatisticAirAreaBase",
    components: {},
    data() {
        const year = new Date().getFullYear();
        return {
            data: [],
            locale,
            bill: {},
            // 表格数据接口
            url: statisticAreaBase,
            selection: [],
            modelOptions: [
                {label: "市", value: "city"},
                {label: "县", value: "county"},
                {label: "站点", value: "place"},

            ],
            // 表格列配置
            columns: [
                {title: "日期", dataIndex: "monitorTime", align: "center",customRender:({text})=>moment(text).format("YYYY-MM-DD"),fixed: "left"},

                {title: "SO2五象", dataIndex: "wuXiangXinQuSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2五象", dataIndex: "wuXiangXinQuNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10五象", dataIndex: "wuXiangXinQuPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO五象", dataIndex: "wuXiangXinQuCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3五象", dataIndex: "wuXiangXinQuO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5五象", dataIndex: "wuXiangXinQuPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI五象", dataIndex: "wuXiangXinQuAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别五象", dataIndex: "wuXiangXinQuAqiCategory", align: "center"},

                {title: "SO2兴宁", dataIndex: "xingNingSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2兴宁", dataIndex: "xingNingNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10兴宁", dataIndex: "xingNingPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO兴宁", dataIndex: "xingNingCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h兴宁", dataIndex: "xingNingO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5兴宁", dataIndex: "xingNingPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI兴宁", dataIndex: "xingNingAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别兴宁", dataIndex: "xingNingAqiCategory", align: "center"},

                {title: "SO2江南", dataIndex: "jiangNanSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2江南", dataIndex: "jiangNanNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10江南", dataIndex: "jiangNanPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO江南", dataIndex: "jiangNanCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h江南", dataIndex: "jiangNanO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5江南", dataIndex: "jiangNanPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI江南", dataIndex: "jiangNanAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别江南", dataIndex: "jiangNanAqiCategory", align: "center"},

                {title: "SO2青秀", dataIndex: "qingXiuSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2青秀", dataIndex: "qingXiuNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10青秀", dataIndex: "qingXiuPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO青秀", dataIndex: "qingXiuCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h青秀", dataIndex: "qingXiuO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5青秀", dataIndex: "qingXiuPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI青秀", dataIndex: "qingXiuAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别青秀", dataIndex: "qingXiuAqiCategory", align: "center"},

                {title: "SO2西乡塘", dataIndex: "xiXiangTangSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2西乡塘", dataIndex: "xiXiangTangNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10西乡塘", dataIndex: "xiXiangTangPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO西乡塘", dataIndex: "xiXiangTangCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h西乡塘", dataIndex: "xiXiangTangO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5西乡塘", dataIndex: "xiXiangTangPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI西乡塘", dataIndex: "xiXiangTangAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别西乡塘", dataIndex: "xiXiangTangAqiCategory", align: "center"},

                {title: "SO2邕宁", dataIndex: "yongNingSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2邕宁", dataIndex: "yongNingNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10邕宁", dataIndex: "yongNingPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO邕宁", dataIndex: "yongNingCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h邕宁", dataIndex: "yongNingO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5邕宁", dataIndex: "yongNingPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI邕宁", dataIndex: "yongNingAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别邕宁", dataIndex: "yongNingAqiCategory", align: "center"},

                {title: "SO2良庆", dataIndex: "liangQingSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2良庆", dataIndex: "liangQingNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10良庆", dataIndex: "liangQingPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO良庆", dataIndex: "liangQingCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h良庆", dataIndex: "liangQingO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5良庆", dataIndex: "liangQingPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI良庆", dataIndex: "liangQingAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别良庆", dataIndex: "liangQingAqiCategory", align: "center"},

                {title: "SO2高新", dataIndex: "gaoXinSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2高新", dataIndex: "gaoXinNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10高新", dataIndex: "gaoXinPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "CO高新", dataIndex: "gaoXinCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h高新", dataIndex: "gaoXinO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5高新", dataIndex: "gaoXinPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI高新", dataIndex: "gaoXinAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别高新", dataIndex: "gaoXinAqiCategory", align: "center"},

                {title: "SO2经开", dataIndex: "jingKaiSo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "NO2经开", dataIndex: "jingKaiNo2", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM10经开", dataIndex: "jingKaiPm10", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "Co经开", dataIndex: "jingKaiCo", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                {title: "O3_8h经开", dataIndex: "jingKaiO3", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "PM2.5经开", dataIndex: "jingKaiPm25", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "AQI经开", dataIndex: "jingKaiAqi", align: "center",customRender:({text})=> typeof text == "number"?text.toFixed(0)+"":""},
                {title: "类别经开", dataIndex: "jingKaiAqiCategory", align: "center"},
            ],
            regionLevelOptions: [],
            time: [],
            // 表格搜索条件
            where: {
                regionLevel: "place",
                timeStart: moment(`${year}-01-01 00:00:00`).format("YYYY-MM-DD 00:00:00"),
                timeEnd: moment().format("YYYY-MM-DD 23:59:59")
            },
            // 表格选中数据
            selectionList: [],
        };
    },
    mounted() {

        this.loadOptionData();
        this.reload();
    },
    methods: {
        /**获取下来框数据 */
        loadOptionData() {
            getColumnOptions("region_level").then((res) => {
                this.regionLevelOptions = res.data.data.map((item) => {
                    return {
                        label: item,
                        value: item,
                    };
                });
            });
        },
        /* 刷新表格 */
        reload() {
            const where = _.cloneDeep(this.where);
            this.$refs.table.reload({
                where
            });
        },
        /* 重置搜索 */
        reset() {
            this.where = {
                regionLevel: "place",
            };
            this.time = undefined;
            this.reload();
        },
        exportFile() {
            const arr = [];
            const th1 = [];
            const th2 = [];
            const merges = []; // 合并
            const columnsTemp = []; // 树形结构整理成list
            this.columns.forEach((item, index) => {
                if (item.children) {
                    item.children.forEach((citem) => {
                        th1.push(item.title)
                        th2.push(citem.title)
                        columnsTemp.push(citem)
                    })
                    merges.push({s: {r: 0, c: th1.length - item.children.length}, e: {r: 0, c: th1.length - 1}})
                } else {
                    th1.push(item.title)
                    th2.push("")
                    columnsTemp.push(item)
                    merges.push({s: {r: 0, c: index}, e: {r: 1, c: index}})
                }
            })
            arr.push(th1, th2);
            this.data.forEach((d) => {
                const td = columnsTemp.map((item) => {
                    if(item.customRender){
                        return item.customRender({text:d[item.dataIndex]});
                    }
                    return d[item.dataIndex];
                });
                arr.push(td);
            });
            let sheet = XLSX.utils.aoa_to_sheet(arr);
            sheet['!merges'] = merges;

            this.$util.exportSheet(XLSX, sheet, moment(this.where.timeStart).format("YYYY年MM月DD日") + "至" + moment(this.where.timeEnd).format("YYYY年MM月DD日") + "城区浓度统计");

        },
    },

};
</script>

<style scoped lang="less">
</style>
