<!--
    systemType 设备所属类型
-->
<template>
    <el-dialog title="所属建筑" :visible.sync="dialog.building" width="750px">
        <div style="height: 50px;">
            <el-select v-model="valueList" :disabled="onlyRead" multiple clearable filterable placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in buildingData" :key="item.id" :label="item.localName" :value="item.id"> </el-option>
            </el-select>
        </div>
        <span slot="footer" class="dialog-footer" v-if="!onlyRead">
            <el-button type="primary" @click="getChange">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import { buildingQuery, shaftLinkBuild } from "@/api/scan/request"
import { mapGetters } from 'vuex'

export default {
    props: {
        dialog: {
            type: Object,
            default: function () {
                return {
                    building: true
                };
            }
        },
        shaftId: {
            type: String,
            default: ""
        },//设备id
        onlyRead: {
            type: Boolean,
            default: false
        },
        buildingList: {
            type: [Array]
        }
    },
    data() {
        return {
            buildingData: [],
            valueList: [] //选中的建筑
        };
    },
    computed: {
        ...mapGetters("layout", ["projectId"])
    },
    created() {
        this.getBuildingData();
    },
    methods: {
        getBuildingData() {
            let param = {
                orders: "localName asc",
                pageNumber: 1,
                pageSize: 1000
            }
            buildingQuery(param, res => {
                if (res.result == 'success') {
                    this.buildingData = res.content;
                } else {
                    this.$message.error(res.message)
                }
            })
        },
        getLinkBuildingData(){
            this.valueList = this.buildingList.map(item => { return item.id })
        },
        getChange() {
            if (this.shaftId) { //有竖井id
                const params = {
                    shaftId: this.shaftId,
                    buildingIdList: this.valueList
                }
               shaftLinkBuild(params, res => {
                   if (res.result === 'success') {
                       this.$message.success("关联成功！");
                       const linkBuild = this.buildingData.filter(item => {
                            return this.valueList.find(id => {return item.id === id});
                        })
                        this.$emit("change", linkBuild);
                        this.dialog.building = false;
                        this.multipleSelection = [];
                   } else {
                       this.$message.error(this.message);
                   }
               })
            } else {
                const linkBuild = this.buildingData.filter(item => {
                    return this.valueList.find(id => {return item.id === id});
                })
                this.$emit("change", linkBuild);
                this.dialog.building = false;
                this.multipleSelection = [];
            }
        }
    },
    watch: {
        dialog: {
            deep: true,
            handler() {
                if (this.dialog.building) {
                    this.getLinkBuildingData()
                }
            }
        },
        projectId() {
            this.buildingData = [];
            this.valueList = [];
            this.getBuildingData();
        }
    }
};
</script>

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