<template>
    <div id="buildFloor">
        <span class="buildFloor">建筑楼层</span>
        <el-cascader ref="floorCascader" placeholder="请选择建筑楼层" :options="options" v-model="value" filterable size="small" :style="isWidth ? '' : 'width:160px;'" @change="changeCascader"></el-cascader>
    </div>
</template>
<script>
    import Builder from "@/controller/old-adm/buildController";
    import Floor from "@/controller/old-adm/floorController";
    import { mapGetters } from "vuex";

    export default {
        props: {
            isWidth: {
                type: Boolean,
                default: true
            },
            type: {
                type: String,
                default: "yes"
            }
        },
        computed: {
            ...mapGetters("layout", [
                "projectId",
                "secret",
                "userId"
            ])
        },
        data() {
            return {
                options: [],
                param: {
                    ProjId: '',
                    secret: ''
                },
                value: ['all']
            };
        },
        created() {
            this.param.ProjId = this.projectId
            this.param.secret = this.secret
            this.getData()
        },
        watch: {
            projectId() {
                this.value = ['all'];
                this.options = [];
                this.param.ProjId = this.projectId
                this.param.secret = this.secret
                this.getData()
                this.changeCascader(['all'])
            }
        },
        methods: {
            //设置默认选项
            setValue(val) {
                if (val && val.length) {
                    this.value = val
                }
            },
            //获取数据
            async getData() {
                let data, buildParams = {
                    pageNumber: 1,
                    pageSize: 1000,
                    orders: "localName asc",
                    projection: [
                        "id",
                        "localName"
                    ]
                }, floorParams = {
                    orders: "floorSequenceID desc",
                    pageNumber: 1,
                    pageSize: 1000
                }
                const values = await Promise.all([Builder.buildingQuery(buildParams), Floor.floorQuery(floorParams)])
                let builData = values[0].content, floorData = values[1].content
                data = builData.map(build => {
                    return {
                        value: build.id,
                        label: build.localName
                    }
                })
                data.unshift({
                    value: "all",
                    label: "全部"
                }, {
                    value: "noKnow",
                    label: "未明确建筑"
                })
                data.forEach(build => {
                    floorData.forEach(floor => {
                        if (build.value == floor.buildingId && floor.id && floor.localName && this.type == "yes") {
                            if (build.children) {
                                build.children.push({
                                    value: floor.id,
                                    label: floor.localName,
                                    FloorSequenceID: floor.floorSequenceID,
                                    infos: floor.infos || {},
                                    outline: floor.outline || null,
                                })
                            } else {
                                build.children = []
                                build.children.push({
                                    value: "all",
                                    label: "全部"
                                },{
                                    value: 'noKnow',
                                    label: "未明确楼层"
                                },{
                                    value: floor.id,
                                    label: floor.localName,
                                    FloorSequenceID: floor.floorSequenceID,
                                    infos: floor.infos || {},
                                    outline: floor.outline || null,
                                })
                            }
                        }
                    })
                })
                this.options = data
            },
            //改变item
            changeCascader(value) {
                let node = this.$refs.floorCascader.getCheckedNodes();
                node && this.$emit("change", value, node[0].data);
            }
        }
    };
</script>
<style lang="less">
    .el-cascader .el-input .el-input__inner {
        vertical-align: bottom;
    }
</style>
<style lang="less" scoped>
    #buildFloor {
        margin-left: 10px;
        float: left;
    }
    .buildFloor {
        color: #999999;
        font-size: 14px;
        margin-right: 12px;
    }
</style>
