<template>
    <div>
        <el-select v-model="value" placeholder="请选择" clearable>
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <el-button @click="handleClickDownload" :disabled="!value" type="primary" :loading="loading" style="margin-left: 15px; margin-right: 10px"
            >导出Excel</el-button
        >
    </div>
</template>

<script>
import jsontoExcel from "@/utils/JOSNToExcel";
import { State } from "@/data/mapTable";
import { queryProperty, dBindingPoint, dEquipCount, dEquipExport, dSystemExport } from "@/api/scan/request";
import { queryEquip } from "@/api/object/equip";
import { mapGetters } from "vuex";

export default {
    data() {
        return {
            loading: false,
            value: "",
            dataList: [],
            options: [
                {
                    value: "header1",
                    label: "已建立关联的设备资产",
                },
                {
                    value: "header2",
                    label: "未关联资产的设备",
                },
                {
                    value: "header3",
                    label: "未关联设备的资产",
                },
                {
                    value: "header4",
                    label: "设备类-交付统计",
                },
                {
                    value: "header5",
                    label: "设备实例交付",
                },
                // {
                //     value: "header6",
                //     label: "设备已交付静态信息点",
                // },
                // {
                //     value: "header7",
                //     label: "设备已交付iot信息点",
                // },
                // {
                //     value: "header8",
                //     label: "系统已交付静态信息点",
                // },
                // {
                //     value: "header9",
                //     label: "系统已交付iot信息点",
                // },
            ],
            header1: [
                {
                    fieldName: "building.localName",
                    headerName: "建筑",
                },
                {
                    fieldName: "floor.localName",
                    headerName: "楼层",
                },
                {
                    fieldName: "equipCategory.name",
                    headerName: "设备类",
                },
                {
                    fieldName: "localName",
                    headerName: "设备本地名称",
                },
                {
                    fieldName: "localId",
                    headerName: "设备本地编码",
                },
                {
                    fieldName: "id",
                    headerName: "设备id",
                },
                {
                    fieldName: "bimId",
                    headerName: "设备BIMID",
                },
                {
                    fieldName: "property.equipFamilyList.0.familyName",
                    headerName: "资产设备族",
                },
                {
                    fieldName: "property.localName",
                    headerName: "资产本地名称",
                },
                {
                    fieldName: "property.localId",
                    headerName: "资产本地编码",
                },
                {
                    fieldName: "property.id",
                    headerName: "资产id",
                },
                {
                    fieldName: "property.createTime",
                    headerName: "资产创建时间",
                },
                {
                    fieldName: "property.codeType",
                    headerName: "资产现场验证状态",
                },
                {
                    fieldName: "infos.installLocation",
                    headerName: "安装位置",
                },
            ],
            header2: [
                {
                    fieldName: "building.localName",
                    headerName: "建筑",
                },
                {
                    fieldName: "floor.localName",
                    headerName: "楼层",
                },
                {
                    fieldName: "equipCategory.name",
                    headerName: "设备类",
                },
                {
                    fieldName: "localName",
                    headerName: "设备本地名称",
                },
                {
                    fieldName: "localId",
                    headerName: "设备本地编码",
                },
                {
                    fieldName: "id",
                    headerName: "设备id",
                },
                {
                    fieldName: "bimId",
                    headerName: "设备BIMID",
                },
                {
                    fieldName: "property.equipFamilyList.0.familyName",
                    headerName: "资产设备族",
                },
                {
                    fieldName: "property.localName",
                    headerName: "资产本地名称",
                },
                {
                    fieldName: "property.localId",
                    headerName: "资产本地编码",
                },
                {
                    fieldName: "property.id",
                    headerName: "资产id",
                },
                {
                    fieldName: "property.createTime",
                    headerName: "资产创建时间",
                },
                {
                    fieldName: "property.codeType",
                    headerName: "资产现场验证状态",
                },
                {
                    fieldName: "infos.installLocation",
                    headerName: "安装位置",
                },
            ],
            header3: [
                {
                    fieldName: "building.localName",
                    headerName: "建筑",
                },
                {
                    fieldName: "floor.localName",
                    headerName: "楼层",
                },
                {
                    fieldName: "equipment.equipCategory.name",
                    headerName: "设备类",
                },
                {
                    fieldName: "equipment.localName",
                    headerName: "设备本地名称",
                },
                {
                    fieldName: "equipment.localId",
                    headerName: "设备本地编码",
                },
                {
                    fieldName: "equipment.id",
                    headerName: "设备id",
                },
                {
                    fieldName: "equipment.bimId",
                    headerName: "设备BIMID",
                },
                {
                    fieldName: "equipFamilyList.0.familyName",
                    headerName: "资产设备族",
                },
                {
                    fieldName: "localName",
                    headerName: "资产本地名称",
                },
                {
                    fieldName: "localId",
                    headerName: "资产本地编码",
                },
                {
                    fieldName: "id",
                    headerName: "资产id",
                },
                {
                    fieldName: "createTime",
                    headerName: "资产创建时间",
                },
                {
                    fieldName: "codeType",
                    headerName: "资产现场验证状态",
                },
                {
                    fieldName: "infos.installLocation",
                    headerName: "安装位置",
                },
            ],
        };
    },
    computed: {
        ...mapGetters("layout", ["projectId", "projectName"]),
    },
    methods: {
        formatDate(now) {
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            let date = now.getDate();
            let hour = now.getHours();
            let minute = now.getMinutes();
            let second = now.getSeconds();
            return (
                year +
                (month > 10 ? month : "0" + month) +
                (date > 10 ? date : "0" + date) +
                hour +
                (minute > 10 ? minute : "0" + minute) +
                (second > 10 ? second : "0" + second)
            );
        },
        handleClickDownload() {
            this.loading = true;
            this.dataList = [];
            if (this.value == "header1") {
                this.getheader1Data();
            } else if (this.value == "header2") {
                this.getheader2Data();
            } else if (this.value == "header3") {
                this.getheader3Data();
            } else if (this.value == "header4") {
                this.getheader4Data();
            } else if (this.value == "header5") {
                this.getheader5Data();
            } else if (this.value == "header6") {
                this.getheader6Data();
            } else if (this.value == "header7") {
                this.getheader7Data();
            } else if (this.value == "header8") {
                this.getheader8Data();
            } else if (this.value == "header9") {
                this.getheader9Data();
            }
        },
        getheader1Data(pageNum) {
            pageNum = pageNum ? pageNum : 1;
            let params = {
                cascade: [
                    {
                        name: "equipCategory",
                        // projection: ["equipCode", "name"]
                    },
                    {
                        name: "property",
                        cascade: [
                            {
                                name: "equipFamilyList",
                                // projection: [ "family", "familyName" ]
                            },
                        ],
                        // projection: ["family", "localName", "localId", "id", "codeType", "createTime"]
                    },
                    {
                        name: "building",
                        // projection: ["localName", "name", "id"]
                    },
                    {
                        name: "floor",
                        // projection: ["localName", "name", "id"]
                    },
                ],
                filters: "not propertyId isnull",
                orders: "createTime desc, id asc",
                pageNumber: pageNum,
                pageSize: 1000,
                // projection: ["createTime", "buildingId", "floorId", "classCode", "localName", "localId", "id", "bimId", "infos"]
            };
            queryEquip(params, (res) => {
                this.dataList = this.dataList.concat(
                    res.content.map((item) => {
                        item.property.codeType = State[item.property.codeType];
                        // item.property.Family = Family[item.property.Family]
                        return item;
                    })
                );
                if (res.total / (res.pageSize * res.pageNumber) > 1) {
                    this.getheader1Data(res.pageNumber + 1);
                } else {
                    let time = +new Date();
                    this.loading = false;
                    jsontoExcel.downloadExl(this.header1, this.dataList, `已建立关联的资产设备-${time}`);
                }
            });
        },
        getheader2Data(pageNum) {
            pageNum = pageNum ? pageNum : 1;
            let params = {
                cascade: [
                    {
                        name: "equipCategory",
                        // projection: ["equipCode", "name"]
                    },
                    {
                        name: "building",
                        // projection: ["localName", "name", "id"]
                    },
                    {
                        name: "floor",
                        // projection: ["localName", "name", "id"]
                    },
                ],
                filters: " propertyId isnull ",
                orders: "createTime desc, id asc",
                pageNumber: pageNum,
                pageSize: 1000,
                // projection: ["createTime", "buildingId", "floorId", "classCode", "localName", "localId", "id", "bimId", "infos"]
            };
            queryEquip(params, (res) => {
                this.dataList = this.dataList.concat(res.content);
                if (res.total / (res.pageSize * res.pageNumber) > 1) {
                    this.getheader2Data(res.pageNumber + 1);
                } else {
                    let time = +new Date();
                    this.loading = false;
                    jsontoExcel.downloadExl(this.header2, this.dataList, `未关联资产的设备-${time}`);
                }
            });
        },
        getheader3Data(pageNum) {
            pageNum = pageNum ? pageNum : 1;
            let params = {
                cascade: [
                    {
                        name: "equipFamilyList",
                        // projection: [ "family", "familyName" ]
                    },
                    {
                        name: "building",
                        // projection: [ "localName", "name", "id" ]
                    },
                    {
                        name: "floor",
                        // projection: [ "localName", "name", "id" ]
                    },
                ],
                filters: "id isnull",
                orders: "createTime desc, id asc",
                pageNumber: pageNum,
                pageSize: 1000,
                // projection: [ "buildingId", "floorId", "family", "localName", "localId", "id", "createTime", "codeType", "infos"]
            };
            queryProperty(params, (res) => {
                this.dataList = this.dataList.concat(
                    res.content.map((item) => {
                        item.codeType = State[item.codeType];
                        return item;
                    })
                );
                if (res.total / (res.pageSize * res.pageNumber) > 1) {
                    this.getheader3Data(res.pageNumber + 1);
                } else {
                    let time = +new Date();
                    this.loading = false;
                    jsontoExcel.downloadExl(this.header3, this.dataList, `未关联设备的资产-${time}`);
                }
            });
        },
        getheader4Data() {
            dEquipCount(
                { projectId: this.projectId },
                () => {
                    this.loading = false;
                },
                `${this.projectName}-设备类-交付统计-${this.formatDate(new Date())}.xlsx`
            );
        },
        getheader5Data() {
            dBindingPoint(
                { projectId: this.projectId },
                () => {
                    this.loading = false;
                },
                `${this.projectName}-设备实例交付-${this.formatDate(new Date())}.xlsx`
            );
        },
        getheader6Data() {
            dEquipExport(
                { type: "static", projectId: this.projectId },
                () => {
                    this.loading = false;
                },
                `${this.projectName}-设备已交付静态信息点-${this.formatDate(new Date())}.xlsx`
            );
        },
        getheader7Data() {
            dEquipExport(
                { type: "iot", projectId: this.projectId },
                () => {
                    this.loading = false;
                },
                `${this.projectName}-设备已交付iot信息点-${this.formatDate(new Date())}.xlsx`
            );
        },
        getheader8Data() {
            dSystemExport(
                { type: "static", projectId: this.projectId },
                () => {
                    this.loading = false;
                },
                `${this.projectName}-系统已交付静态信息点-${this.formatDate(new Date())}.xlsx`
            );
        },
        getheader9Data() {
            dSystemExport(
                { type: "iot", projectId: this.projectId },
                () => {
                    this.loading = false;
                },
                `${this.projectName}-系统已交付iot信息点-${this.formatDate(new Date())}.xlsx`
            );
        },
    },
};
</script>

<style scoped>
</style>
