<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 {statisticUrl, getColumnOptions,statisticArea} 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() {
        const year = new Date().getFullYear();
        return {
            data: [],
            locale,
            bill: {},
            // 表格数据接口
            // url: statisticUrl,
            selection: [],
            modelOptions: [
                {label: "市", value: "city"},
                {label: "县", value: "county"},
                {label: "站点", value: "place"},
                {label: "城区", value: "area"},

            ],
            // 表格列配置
            columns: [
                {title: "站点/县", dataIndex: "place",align:"center",fixed: "left",},
                {
                    title: "二氧化硫", children: [
                        {title: "本次", dataIndex: "so2Current",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "so2SameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "so2ChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":text},
                        {title: "排名（按浓度值）", dataIndex: "so2Rank",align:"center"},
                    ]
                },
                {
                    title: "二氧化氮", children: [
                        {title: "本次", dataIndex: "no2Current",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "no2SameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "no2ChangePercentage",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":text},
                        {title: "排名（按浓度值）", dataIndex: "no2Rank",align:"center"},
                    ]
                },
                {
                    title: "可吸入颗粒物", children: [
                        {title: "本次", dataIndex: "pm10Current",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "pm10SameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "pm10ChangePercentage",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":text},
                        {title: "排名（按浓度值）", dataIndex: "pm10Rank",align:"center"},
                    ]
                },
                {
                    title: "一氧化碳", children: [
                        {title: "本次", dataIndex: "coCurrent",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "coSameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "coChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":text},
                        {title: "排名（按浓度值）", dataIndex: "coRank",align:"center"},
                    ]
                },
                {
                    title: "臭氧", children: [
                        {title: "本次", dataIndex: "o3Current",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "o3SameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "o3ChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":text},
                        {title: "排名（按浓度值）", dataIndex: "o3Rank",align:"center"},
                    ]
                },
                {
                    title: "细颗粒物", children: [
                        {title: "本次", dataIndex: "pm25Current",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "pm25SameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "pm25ChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":text},
                        {title: "排名（按浓度值）", dataIndex: "pm25Rank",align:"center"},
                    ]
                },
                {
                    title: "综合指数", children: [
                        {title: "本次", dataIndex: "comprehensiveAirQualityIndexCurrent",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "comprehensiveAirQualityIndexSameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "comprehensiveAirQualityIndexChangePercentage",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                        {title: "排名（按浓度值）", dataIndex: "comprehensiveAirQualityIndexRank",align:"center"},
                    ]
                },
                {
                    title: "优天数", children: [
                        {title: "本次", dataIndex: "excellentDaysCurrent",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "excellentDaysSameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "excellentDaysChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                        // {title: "排名（按浓度值）", dataIndex: "excellentDaysRank",align:"center"},
                    ]
                },
                {
                    title: "良天数", children: [
                        {title: "本次", dataIndex: "goodDaysCurrent",align:"center",customRender:({text})=>text + ""},
                        {title: "上年同期", dataIndex: "goodDaysSameTimeLastYear",align:"center",customRender:({text})=>text + ""},
                        {title: "增减（%）", dataIndex: "goodDaysChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                        // {title: "排名（按浓度值）", dataIndex: "goodDaysRank",align:"center"},
                    ]
                },
                // {
                //     title: "轻度污染天数", children: [
                //         {title: "本次", dataIndex: "lightPollutionDaysCurrent",align:"center"},
                //         {title: "上年同期", dataIndex: "lightPollutionDaysSameTimeLastYear",align:"center"},
                //         {title: "增减（%）", dataIndex: "lightPollutionDaysChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                //         // {title: "排名（按浓度值）", dataIndex: "lightPollutionDaysRank",align:"center"},
                //     ]
                // },
                // {
                //     title: "中度污染天数", children: [
                //         {title: "本次", dataIndex: "moderatelyPollutedDaysCurrent",align:"center"},
                //         {title: "上年同期", dataIndex: "moderatelyPollutedDaysSameTimeLastYear",align:"center"},
                //         {title: "增减（%）", dataIndex: "moderatelyPollutedDaysChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                //         // {title: "排名（按浓度值）", dataIndex: "moderatelyPollutedDaysRank",align:"center"},
                //     ]
                // },
                // {
                //     title: "良天数", children: [
                //         {title: "本次", dataIndex: "goodDaysCurrent",align:"center"},
                //         {title: "上年同期", dataIndex: "goodDaysSameTimeLastYear",align:"center"},
                //         {title: "增减（%）", dataIndex: "goodDaysChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                //         // {title: "排名（按浓度值）", dataIndex: "goodDaysRank",align:"center"},
                //     ]
                // },
                // {
                //     title: "重度污染天数", children: [
                //         {title: "本次", dataIndex: "heavyPollutionDaysCurrent",align:"center"},
                //         {title: "上年同期", dataIndex: "heavyPollutionDaysSameTimeLastYear",align:"center"},
                //         {title: "增减（%）", dataIndex: "heavyPollutionDaysChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                //     ]
                // },
                {
                    title: "优良率", children: [
                        {title: "本次", dataIndex: "excellentRateCurrent",align:"center"},
                        {title: "上年同期", dataIndex: "excellentRateSameTimeLastYear",align:"center"},
                        {title: "增减（%）", dataIndex: "excellentRateChangePercentage",align:"center",customRender:({text})=> typeof text == "number"?text.toFixed(1)+"":""},
                    ]
                },

            ],
            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 00:00:00")
            },
            // 表格选中数据
            selectionList: [],
        };
    },
    mounted() {

        this.loadOptionData();
        this.reload();
    },
    computed:{
        url(){
            if(this.where.regionLevel == "area"){
                return statisticArea
            }else{
                return statisticUrl
            }
        }
    },
    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);

            if(this.where.regionLevel == "area"){
                delete where["regionLevel"]
            }
            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) => 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>
