import ScanController from "@/controller/old-adm/ScanController";
import FloorController from "@/controller/old-adm/floorController";

const buildFloor = {
    getData: function (buildFloorData) {
        let data, buildParams = {
            pageNumber: 1,
            pageSize: 1000,
            orders: "localName asc",
            projection: [
                "id",
                "localName"
            ]
        }, floorParams = {
            orders: "floorSequenceID desc",
            pageNumber: 1,
            pageSize: 1000,
            projection: [
                "buildingId",
                "id",
                "localName",
                "floorSequenceID"
            ]
        }

        let promise1 = new Promise((resolve, reject) => {
            (async function () {
                var res = await ScanController.buildingQuery(buildParams);
                resolve(res);
            })();
        })
        let promise2 = new Promise((resolve, reject) => {
            (async function () {
                var res = await FloorController.floorQuery(floorParams);
                resolve(res);
            })();
        })

        Promise.all([promise1, promise2]).then(function (values) {
            let builData = values[0].content, floorData = values[1].content
            data = builData.map(build => {
                return {
                    value: build.id,
                    label: build.localName
                }
            })
            data.unshift({
                value: "",
                label: "未明确建筑"
            })
            data.forEach(build => {
                floorData.forEach(floor => {
                    if (build.value == floor.buildingId && floor.id && floor.localName) {
                        if (build.children) {
                            build.children.push({
                                value: floor.id,
                                label: floor.localName
                            })
                        } else {
                            build.children = []
                            build.children.push({
                                value: "",
                                label: "未明确楼层"
                            }, {
                                value: floor.id,
                                label: floor.localName
                            })
                        }
                    }
                })
            })
            data.forEach(item => {
                if (item.children && item.children.length) {
                    item.children.forEach(child => {
                        if (child.value != "") {
                            buildFloorData.push({
                                Code: item.value + "-" + child.value,
                                Name: item.label + "-" + child.label
                            })
                        } else {
                            buildFloorData.push({
                                Code: item.value,
                                Name: item.label + "-" + child.label
                            })
                        }
                    })
                } else {
                    buildFloorData.push({
                        Code: item.value,
                        Name: item.label
                    })
                }
            })
        })
    }
}

export default buildFloor