<template>
    <el-dialog
        :visible.sync="isShow.dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
        :width="width"
    >
        <span slot="title">{{title}}</span>
        <!-- <slot name="body"></slot> -->
        <el-tree :props="treeArr" :data="data" @node-click="floorClick" :highlight-current="true"></el-tree>
        <span slot="footer" class="dialog-footer my">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import {
    getBuildSelect, //根据项目ID获得建筑列表
    getSpaceFloor //根据建筑id获取楼层id
} from "@/api/scan/request";
import {mapGetters, mapActions} from "vuex";
import tools from '@/utils/scan/tools'
export default {
    props: {
        width: {
            type: String,
            default: "40%"
        },
        title: {
            type: String,
            default: "头部信息"
        },
        isShow: {
            type: Object
        },
        param: {
            type: Object
        }
    },
    data() {
        return {
            treeArr: {
                label: "name", //label的名字
                children: "children", //如果有下一级children和isLeaf存在
            },
            data: null,
            proj: null,
            mess: null
        };
    },
    created() {
        this.param.ProjId = this.projectId
        this.param.secret = this.secret
        this.param.UserId = this.userId
        // this.getNodes()
    },
    computed: {
        ...mapGetters("layout", [
            "projectId",
            "secret",
            "userId"
        ])
    },
    methods: {
        //确定
        confirm() {
            if (!!this.mess) {
                this.isShow.dialogVisible = false;
                this.$emit("close", this.mess);
            } else {
                this.$message("请选择楼层");
            }
        },
        //取消
        close() {
            if (!!this.mess) {
                this.isShow.dialogVisible = false;
            } else {
                this.$message("请选择楼层");
            }
        },
        changeArr(arr) {
            return arr.map(item => {
                if (item.floors && item.floors.length) {
                    return {
                        code: item.id,
                        name: item.infos.BuildLocalName,
                        children: item.floors.map(i => {
                            return {
                                code: i.id,
                                name: (i.infos.FloorLocalName || i.infos.FloorName || "未知") + this.myMess(i),
                                map: i.infos.FloorMap || null,
                                affected: i.affected,
                                isChilren: 2,
                                buildCode: item.id,
                                buildName: item.infos.BuildLocalName,
                                shunxu: i.infos.FloorSequenceID || 0
                            }
                        })
                    }
                } else {
                    return {
                        code: item.id,
                        name: item.infos.BuildLocalName,
                        children: null,
                        isChilren: 1,
                    }
                }
            })
        },

        myMess(i) {
            if (i.affected) {
                return "(该业务空间受元空间变化影响)"
            } else {
                if (i.infos.FloorMap) {
                    return ''
                } else {
                    return "(请初始化平面图)"
                }
            }
        },
        //点击节点懒加载
        // getNodes() { // 物理世界 已无接口
        //     let param = {
        //         ProjId: this.param.ProjId,
        //         UserId: this.param.UserId,
        //         secret: this.param.secret
        //     };
        //     getSpaceFloor(param).then(res => {
        //         if (res.data.Result == 'success') {
        //             let data = this.changeArr(res.data.Content).map(item => {
        //                 if (item.children && item.children.length) {
        //                     item.children = tools.sortArr(item.children, "shunxu", false)
        //                 }
        //                 return item
        //             })
        //             this.data = data
        //         } else {
        //             this.$message.error(res.data.ResultMsg)
        //         }
        //     }).catch(() => {
        //         this.$message.error("请求出错")
        //     })
        // },

        //子节点被点击
        floorClick(data) {
            if (data.isChilren == 2) {
                this.mess = data
            } else { }
        }
    }
};
</script>

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