/**
@author:guoguo
@date:2018.11.30
@info: 对象位置
**/
<template>
<el-cascader
    style="width: 249px;"
    placeholder="对象位置"
    :options="options"
    :value="value"
    @active-item-change="handleItemChange"
    @change="change"
    :props="configObj"
  ></el-cascader>
</template>
<script>
export default {
    name: "obj-location",
    data() {
        return {
            options: [{
                value: 'zhinan',
                label: '指南',
                children:[]
            }],
            configObj: {
                label: "name",
                value: "id",
            },
            value: []
        }
    },
    props:{
        projectId: {
            type: String,
            required: true
        }
    },
    methods: {
        //id=[建筑id|楼层Id]
        getList(type, id, BdId) {
            let params = {
                criteria:{
                    id: id || undefined,
                    type: [type]
                }
            }
            this.postJson(`/server/dataplatform/object/subquery?projectId=${this.projectId}`, params, resp => {
                if (resp.Result === 'success'  && resp.Count) {
                    if(type == "Bd") {
                        this.handleBd(resp.Content);
                    } else if ( type == "Fl") {
                        this.handleFl(resp.Content, id)
                    } else {
                        this.handleSp(resp.Content, BdId, id)
                    }                   
                } else { //如果是室外
                    if(type == "Sp") {
                        let arr = [this.allItem()];
                        this.options.forEach(item => {
                            item.children?item.children.forEach(ele => {
                                if(ele.id == id) {
                                    ele.children = arr;
                                }
                            }):""
                        })
                    }

                }
            })            
        },
        handleBd(content) {
            let arr = [];
            content.forEach(item=> {
                let obj = {
                    id: item.id,
                    name: item.infos.BuildLocalName,
                    children: []
                }
                arr.push(obj);
            })
            this.options = arr;
            this.options.unshift(this.allBdItem())
        },
        handleFl(content, id) {
            let arr = [];
            content.forEach(item=> {
                let obj = {
                    id: item.id,
                    name: item.infos.FloorLocalName,
                    children:[]
                }
                arr.push(obj);
            })
            this.options.forEach(item => {
                if(item.id == id) {
                    item.children = arr
                }
            })
        },
        handleSp(content, BdId, flId) {
            let arr = []
            content.forEach(item => {
                let obj = {
                    id: item.id,
                    name: item.infos.RoomLocalName,
                }
                arr.push(obj);
            });
            arr.unshift(this.allItem())
             this.options.forEach(item => {
                if(item.id == BdId) {
                    item.children.forEach(ele => {
                        if(ele.id == flId) {
                            ele.children = arr;
                        }
                    })
                }
            })
        },
        allBdItem() {
            return {id: "", name: "对象位置:全部"}
        },
        allItem() {
            return {id: "" , name: "全部"}
        },
        handleItemChange(val) {
            if(val.length == 1 && val[0]) {
                this.getList( "Fl", val[0]);
            } else if(val.length == 2){
                this.getList( "Sp", val[1], val[0]);
            }
        },
        change(val) {
            let id = val[val.length - 1];
            if(val.length > 1 && !val[val.length-1]){
                id = val[val.length-2];
            }
            this.$emit("change", id)
        }
    },
    watch: {
        projectId: {
            immediate: true,
            handler(val){
                this.getList("Bd")
            }
        }
    }

}
</script>