<template>
    <div class="ele-body">
        <a-card style="width: 100%" :bordered="false">
            <template #title>
                <a-space>
                    <a-input-number v-model:value="year" :min="1970" :max="2050"/>
                    <a-select
                        ref="select"
                        v-model:value="regionLevel"
                        style="width: 120px"
                    >
                        <a-select-option value="city">市</a-select-option>
                        <a-select-option value="county">县</a-select-option>
                        <a-select-option value="place">站点</a-select-option>
                    </a-select>
                    <a-upload
                        :before-upload="importFileCity"
                        :showUploadList="false"
                        accept=".xls,.xlsx,.csv"
                    >
                        <a-button type="primary">数据导入</a-button>
                    </a-upload>
                    <a-button @click="download">模板下载</a-button>

                </a-space>
            </template>
            <a-tabs v-model:activeKey="activeKey">
                <a-tab-pane tab="环境空气信息" key="">
                    <air-bill ref="air"></air-bill>
                </a-tab-pane>
            </a-tabs>
        </a-card>
    </div>
</template>

<script>

import utils from "./utils";
import {Modal} from "ant-design-vue";
import AirBill from "./air-bill.vue";
import {saveAirBill} from "@/api/ecology/atmosphere/air";
import moment from "moment";
import {downloadTemplate, excelToJson} from "@/utils/excel-util";

export default {
    name: "AirCollectIndex",
    components: {
        AirBill,
    },
    data() {
        return {
            activeKey: "",
            regionLevel: "city",
            time: moment(),
            year: new Date().getFullYear()
        };
    },
    methods: {
        download(){
            let filename = "环境空气-"
            if (this.regionLevel == "city") {
                filename += "市"
            } else if (this.regionLevel == "county") {
                filename += "县"
            } else {
                filename += "站点"
            }
          downloadTemplate(filename)
        },
        /* 导入本地excel文件 */
        importFileCity(file) {
            const hide = this.$message.loading("导入中..", 0);

            let reader = new FileReader();
            reader.onload = async (e) => {
                try {

                    let aoa = await excelToJson(e.target.result)
                    const airList = aoa.filter((item) => {
                            if (this.regionLevel == "city") {
                                return item.length >= 10&& item[0]&& item[0] != "城市";
                            } else if (this.regionLevel == "county") {
                                return item.length >= 10&& item[0] && item[0] != "测点名称";
                            } else {
                                return item.length >= 10&& item[0] && item[0] != "城市"
                            }
                        }
                    );
                    const billName = file.name;
                    let billData = undefined;
                    if (this.regionLevel == "city") {
                        billData = utils.toCityObjData(airList)
                    } else if (this.regionLevel == "county") {
                        billData = utils.toCountyObjData(airList)
                    } else {
                        billData = utils.toPlaceObjData(airList)
                    }
                    // 解析成对象数组
                    if (!billData || billData.length == 0) {
                        hide()
                        Modal.error({
                            title: "导入失败",
                            content: "找不到数据",
                        });
                        return;
                    }
                    saveAirBill({
                        billName: billName,
                        regionLevel: this.regionLevel,
                        ambientAirList: billData,
                        reportTime: new Date(`${this.year}-01-01 00:00:00`).getTime()
                    }).then((res) => {
                        if (res.data.code == 0) {
                            Modal.success({
                                title: "导入成功",
                                content: `成功导入${billData.length}条数据`,
                            });
                            this.$refs.air && this.$refs.air.reload();
                            this.$router.replace({
                                path: "/atmosphere/air/collect/air/" + res.data.data.ambientAirBillId
                            })
                        } else {
                            Modal.error({
                                title: "导入失败",
                                content: "数据上传出错",
                            });
                        }

                    })
                        .catch(() => {
                            Modal.error({
                                title: "导入失败",
                                content: "数据上传出错",
                            });
                        })
                        .finally(() => {
                            hide();
                        })

                    // 上传到服务器
                } catch (error) {
                    hide();
                    Modal.error({
                        title: "导入失败",
                        content: error.message,
                    });
                }
                // console.log(billData);
            };
            reader.readAsArrayBuffer(file);

            return false;
        },

    },
};
</script>

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