<template>
    <div class="ele-body">
        <a-card :bordered="false">
            <!-- 搜索表单 -->
            <a-form
                :model="where"
                layout="inline"
            >

                    <a-form-item label="时间:">
                        <a-month-picker v-model:value="time"/>
                    </a-form-item>
                <a-space>
                    <a-radio-group @change="cityTypeChange" v-model:value="cityType">
                        <a-radio-button :value="1">全部</a-radio-button>
                        <a-radio-button :value="2">省会</a-radio-button>
                    </a-radio-group>
<!--                    <a-radio-group @change="rankTypeChange" v-model:value="rankType">-->
<!--                        <a-radio-button :value="1">综合指数</a-radio-button>-->
<!--                        <a-radio-button :value="2">PM2.5</a-radio-button>-->
<!--                    </a-radio-group>-->
                    <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>
            <!-- 表格 -->
            <a-table
                :columns="columns"
                :row-key="rowKey"
                :data-source="data"
                :loading="loading"
                :pagination="false"
            >
            </a-table>
        </a-card>
    </div>
    <!-- 编辑弹窗 -->
</template>

<script>
// import _ from "lodash";
import XLSX from "xlsx";
import {
    airMonthRankUrl,
    airMonthProvincialCapitalRankUrl,
} from "@/api/ecology/atmosphere/air";
import locale from "ant-design-vue/es/date-picker/locale/zh_CN";
import moment from "moment";
import axios from "axios";
//   import utils from "./utils";
export default {
    name: "AirMonthRanking",
    components: {},
    data() {
        return {
            data: [],
            locale,
            bill: {},
            // 表格数据接口
            url: airMonthRankUrl,
            selection: [],
            // 表格列配置
            columns: [
                {title: "排名", dataIndex: "rankNumber",},
                {title: "省份", dataIndex: "province",},
                {title: "城市", dataIndex: "city",},
                {title: "综合指数", dataIndex: "comprehensiveAqi",},
                {title: "最大指数", dataIndex: "maxAqi",},
                {title: "首要污染物", dataIndex: "primaryPollutant",},
            ],
            placeOptions: [],
            time: moment(),
            rankType: 1,
            cityType: 1,
            rowKey: "ambientAirZhenqiCityMonthComprehensiveAqiRankDataId",
            loading: false,
            // 表格搜索条件
            where: {},
            // 表格选中数据
            selectionList: [],
        };
    },
    mounted() {
        this.reload()
    },
    methods: {

        /* 刷新表格 */
        reload() {
            if (!this.time) {
                return;
            }
            const where = {
                year: this.time.get("year"),
                month: this.time.get("month") + 1
            }

            this.loading = true;

            if(this.cityType == 1){
                axios.get(this.url, {params: {limit: 168, ...where}}).then(res => {
                    this.data = res.data.data
                }).finally(() => {
                    this.loading = false;
                })
            }else {
                axios.post(this.url, {limit: 168, ...where}).then(res => {
                    this.data = res.data.data
                }).finally(() => {
                    this.loading = false;
                })
            }
        },
        /* 重置搜索 */
        reset() {
            this.where = {
                year: moment().get("year"),
                month: moment().get("month")
            };
            this.reload();
        },
        cityTypeChange(){
            if (this.rankType == 1) {
                if (this.cityType == 1) {
                    this.url = airMonthRankUrl;
                } else {
                    this.url = airMonthProvincialCapitalRankUrl;
                }
            } else {
                if (this.cityType == 1) {
                    this.url = "";

                } else {
                    this.url = ""
                }
            }
            this.reload();
        },
        rankTypeChange() {
            if (this.rankType == 1) {
                if (this.cityType == 1) {
                    this.url = airMonthRankUrl;
                } else {
                    this.url = airMonthProvincialCapitalRankUrl;
                }
            } else {
                if (this.cityType == 1) {
                    this.url = "";
                } else {
                    this.url = ""
                }
            }
            this.reload();
        },
        exportFile() {

            const columns = this.columns;
            const arr = [];
            const th = columns.map((item) => item.title);
            arr.push(th);
            this.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>