<template>
    <div class="ele-body">
        <a-card :bordered="false">
            <!-- 搜索表单 -->
            <a-form
                :model="where"
                layout="vertical"
                :label-col="{ md: { span: 6 }, sm: { span: 24 } }"
                :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }"
            >
                <a-row>
                    <a-col :lg="6" :md="12" :sm="24" :xs="24">
                        <a-form-item label="区域等级:">
                            <a-select v-model:value="where.model" >
                                <a-select-option
                                    v-for="(item) in modelOptions"
                                    :key="item.value"
                                >{{ item.label }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :lg="24" :md="24" :sm="24" :xs="24">
                        <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-col>
                </a-row>
            </a-form>
            <!-- 表格 -->
            <ele-pro-table
                v-model:selection="selectionList"
                ref="table"
                row-key="ambientAirId"
                :datasource="url"
                :columns="columns"
                :where="where"
                :scroll="{ x: 'max-content' }"
                @done="(d) => (data = d.data)"
            >

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

<script>
// import _ from "lodash";
import XLSX from "xlsx";
import {statisticAvg, getColumnOptions, listAllAir} from "@/api/ecology/atmosphere/air";
import locale from "ant-design-vue/es/date-picker/locale/zh_CN";
import moment from "moment";
//   import utils from "./utils";
export default {
    name: "StatisticAirAvg",
    components: {},
    data() {
        return {
            data: [],
            locale,
            bill: {},
            // 表格数据接口
            url: statisticAvg,
            selection: [],
            modelOptions: [
                {label: "市", value: "city"},
                {label: "县", value: "county"},
                {label: "站点", value: "place"},

            ],
            // 表格列配置
            columns: [
                {title:"SO2",dataIndex:"avgSo2"},
                {title:"NO2",dataIndex:"avgNo2"},
                {title:"PM10",dataIndex:"avgPm10"},
                {title:"PM25",dataIndex:"avgPm25"},
                {title:"CO",dataIndex:"avgCo"},
                {title:"O3",dataIndex:"avgO3"},
                {title:"AQI",dataIndex:"avgAqi"},
                {title:"首要污染物",dataIndex:"primaryPollutant"},
                {title:"空气质量指数级别",dataIndex:"aqiLevel"},
                {title:"空气质量状况",dataIndex:"airQualityStatus"},
                {title:"一氧化碳百分位数评价",dataIndex:"evaluationCo"},
                {title:"臭氧百分位数评价",dataIndex:"evaluationO3"},
            ],
            regionLevelOptions: [],
            // 表格搜索条件
            where: {
                model: "city",
            },
            // 表格选中数据
            selectionList: [],
        };
    },
    mounted() {
        this.loadOptionData();
    },
    methods: {
        /**获取下来框数据 */
        loadOptionData() {
            getColumnOptions("region_level").then((res) => {
                this.regionLevelOptions = res.data.data.map((item) => {
                    return {
                        label: item,
                        value: item,
                    };
                });
            });
        },
        /* 刷新表格 */
        reload() {
            this.$refs.table.reload({
                where: this.where,
            });
        },
        /* 重置搜索 */
        reset() {
            this.where = {
                model: "city",
            };
            this.reload();
        },
        exportFile() {

            const columns = [
                {
                    title: "监测日期",
                    dataIndex: "monitorTime",
                    sorter: true,
                    customRender: ({text}) => moment(text).format("YYYY-MM-DD HH:mm")
                },
                {
                    title: "城市",
                    dataIndex: "city",
                    sorter: true,
                },
                {
                    title: "测点名称",
                    dataIndex: "place",
                    sorter: true,
                },
                {
                    title: "SO2/(μg/m3)",
                    dataIndex: "so2",
                    sorter: true,
                },
                {
                    title: "NO2/(μg/m3)",
                    dataIndex: "no2",
                    sorter: true,
                },
                {
                    title: "PM10/(μg/m3)",
                    dataIndex: "pm10",
                    sorter: true,
                },
                {
                    title: "CO/(mg/m3)",
                    dataIndex: "co",
                    sorter: true,
                },
                {
                    title: "臭氧（O3）最大8小时滑动平均浓度/(μg/m3)",
                    dataIndex: "o3",
                    sorter: true,
                },
                {
                    title: "PM2.5",
                    dataIndex: "pm25",
                    sorter: true,
                },
                {
                    title: "空气质量指数AQI",
                    dataIndex: "aqi",
                    sorter: true,
                },
                {
                    title: "首要污染物",
                    dataIndex: "primaryPollutant",
                    sorter: true,
                },
                {
                    title: "空气质量指数级别",
                    dataIndex: "aqiLevel",
                    sorter: true,
                },
                {
                    title: "空气质量状况",
                    dataIndex: "airQualityStatus",
                    sorter: true,
                },
                {
                    title: "备注",
                    dataIndex: "remark",
                    sorter: true,
                },
                {
                    title: "创建人",
                    dataIndex: "username",
                    sorter: true,
                },
            ];
            const arr = [];
            const th = columns.map((item) => item.title);
            arr.push(th);
            listAllAir(this.where).then(res => {
                if (res.data.code == 0) {
                    res.data.data.forEach((d) => {
                        const td = columns.map((item) => d[item.dataIndex]);
                        arr.push(td);
                    });
                    let sheet = XLSX.utils.aoa_to_sheet(arr);
                    this.$util.exportSheet(XLSX, sheet, new Date().getTime().toString());
                }
            })

        },
    },
};
</script>

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