<template>
    <div class='page-table-template' style='display: flex;flex-direction: column;flex-grow: 1;flex-shrink: 1;'>
        <div class="btn-box">
            <el-button type='danger' @click='someDel'>批量删除</el-button>
        </div>
        <el-table
            :data='tableData'
            style='width: 100%'
            border
            class='data-table'
            v-loading='loading'
            @selection-change='handleSelectionChange'
        >
            <el-table-column type='selection' width='55' align='center'></el-table-column>
            <el-table-column header-align='center' align='center' prop='BuildName' label='建筑' width='150'></el-table-column>、
            <el-table-column header-align='center' align='center' prop='floorName' label='楼层' width='80'></el-table-column>
            <el-table-column header-align='center' align='center' prop='FamilyName' label='设备族'></el-table-column>
            <el-table-column header-align='center' align='center' label='本地编码' width='250'>
                <template slot-scope='scope'>
                    <span>{{scope.row.Infos.EquipLocalID || scope.row.Infos.EquipID || '--'}}</span>
                </template>
            </el-table-column>
            <el-table-column header-align='center' align='center' prop='FmName' label='本地名称'></el-table-column>
            <el-table-column header-align='center' align='center' prop='spaceName' label='所在元空间'></el-table-column>
            <el-table-column header-align='center' align='center' prop='eq' label='对应设备类'></el-table-column>
            <el-table-column header-align='center' align='center' prop='bimXY' label='BIM模型中坐标'></el-table-column>
            <el-table-column header-align='center' align='center' prop='PointName' label='对应的岗位' width='120'>
                <template slot-scope='scope'>
                    <el-button v-if="scope.row.EquipmentId" type='primary' plain size='mini' @click='post(scope.row)'>查看岗位详情</el-button>
                </template>
            </el-table-column>
            <el-table-column header-align='center' align='center' prop='PointName' label='操作' width='150'>
                <template slot-scope='scope'>
                    <el-button type='primary' plain size='mini' @click='look(scope.row)'>查看</el-button>
                    <el-button type='danger' plain size='mini' @click='del(scope.row)'>删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <base-pagination
            class='data-table-pagination'
            :currentPages='pageNum'
            :total='total'
            @pageChanged='pageChanged'
        ></base-pagination>
        <saga-dialog ref='dialog' :iframeSrc='iframeSrc'></saga-dialog>
    </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SagaDialog from './Frame'
//api
import {
    getErrAssets, //获取异常资产type不同不同tab
    delErrAssets,
    getSpaceId,
    getSpaceName
} from '@/api/scan/request'
export default {
    name: 'noverify',
    data() {
        return {
            loading: false,
            tableData: [],
            total: 0,
            pageNum: 1,
            pageSize: 10,
            iframeSrc: '',
            allChecked: [],
            //dict
            EquipmentObj: {}
        }
    },
    props: {
        buildVlaue: {
            default: ''
        },
        floorsObj: {
            default: {}
        },
        allFamilyObj: {},
        allEquipObj: {
            default: {}
        }
    },
    components: {
        SagaDialog
    },
    computed: {
        ...mapGetters('peojMess', ['projectId', 'userId', 'secret'])
    },
    methods: {
        look(row) {
            this.iframeSrc =
                process.env.BASE_URL +
                ':8889/#/details?perjectId=' +
                this.projectId +
                '&secret=' +
                this.secret +
                '&FmId=' +
                row.FmId +
                '&type=1&code=' +
                row.Family
            this.$refs['dialog'].show()
        },
        post(row) {
            this.iframeSrc =
                process.env.BASE_URL +
                ':8889/#/details?perjectId=' +
                this.projectId +
                '&secret=' +
                this.secret +
                '&FmId=' +
                row.FmId +
                '&type=1&code=' +
                row.eqCode
            this.$refs['dialog'].show()
        },
        handleSelectionChange(arr) {
            this.allChecked = arr.map(ele => ele.FmId)
        },
        someDel() {
            if (this.allChecked.length) {
                this.delOp(this.allChecked)
            } else {
                this.$message({
                    message: '请至少选择一条',
                    type: 'warning'
                })
            }
        },
        del(row) {
            let paramList = [row.FmId]
            this.delOp(paramList)
        },
        delOp(paramList) {
            this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    let param = {
                        ProjId: this.projectId,
                        UserId: this.userId
                    }
                    delErrAssets(param, paramList).then(result => {
                        if (result.data.Result == 'success') {
                            this.getAsset()
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            })
                        }
                    })
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
        },
        pageChanged(page, size) {
            this.pageNum = page
            this.pageSize = size
            this.getAsset()
        },
        getAsset() {
            this.loading = true
            let param = {
                BuildId: this.buildVlaue || undefined,
                PageNum: this.pageNum,
                PageSize: this.pageSize,
                Type: 4,
                ProjId: this.projectId,
                UserId: this.userId,
                CodeType: 1
            }
            getErrAssets(param).then( async res => {
                // this.tableData = list
                if (res.data.Result == 'success') {                    
                    let list = res.data.FmList
                    list.forEach((ele, index) => {
                        ele.floorName = this.floorsObj[ele.FloorId] || '--'
                        let x = ''
                        let y = ''
                        if (ele.X == 0) {
                            x = '空'
                        } else {
                            x = ele.X || ''
                        }
                        if (ele.Y == 0) {
                            y = '空'
                        } else {
                            y = ele.Y || ''
                        }
                        ele.bimXY = x + (x || y ? ',' : '-') + y
                    })
                    this.total = res.data.Count
                    this.tableData = list
                    await this.getEq(list)
                    await this.getSp(list)
                    this.loading = false
                }
            })
        },
        async getEq(list) {
            let param = {},
                idArr = [],
                i = 0,
                dataArr = list
            for (; i < dataArr.length; i++) {
                if (dataArr[i].EquipmentId) {
                    idArr.push({
                        id: dataArr[i].EquipmentId
                    })
                }
            }
            param = {
                perjectId: this.projectId,
                secret: this.secret,
                list: idArr
            }
            let ret = await this.batchQuery(param)
            ret.forEach(ele => {
                this.EquipmentObj[ele.id] = ele.category.slice(2)
            })
            list.forEach(ele => {
                if (ele.EquipmentId) {
                    ele.eq = this.allEquipObj[this.EquipmentObj[ele.EquipmentId]]
                    ele.eqCode = this.EquipmentObj[ele.EquipmentId]
                } else {
                    ele.eq = null
                }
            })
        },
        async getSp(list) {
            let param = {},
                idArr = [],
                i = 0,
                dataArr = list
            for (; i < dataArr.length; i++) {
                if (dataArr[i].EquipmentId) {
                    idArr.push({
                        from_id: dataArr[i].EquipmentId,
                        graph_id: 'GtEquipinSpace001',
                        rel_type: '1'
                    })
                }
            }
            param = {
                perjectId: this.projectId,
                secret: this.secret,
                list: idArr
            }
            let ret = await this.getSpId(param)
            let arr = []
            let eqObj = {}
            ret.forEach(ele => {
                if (ele.Content.length > 0) {
                    arr.push({ id: ele.Content[0].to_id })
                    eqObj[ele.Content[0].from_id] = ele.Content[0].to_id
                }
            })
            list.forEach(list1 => {
                if (eqObj[list1.EquipmentId]) {
                    list1.toSpace = eqObj[list1.EquipmentId]
                } else {
                    list1.toSpace = null
                }
            })
            let params = {
                perjectId: this.projectId,
                secret: this.secret,
                list: arr
            }
            let retu = await this.batchQuery(params)
            let spObj = {}
            retu.forEach(retu1 => {
                spObj[retu1.id] = retu1.infos
            })
            list.forEach(list2 => {
                if (spObj[list2.toSpace]) {
                    list2.spaceName = spObj[list2.toSpace].RoomLocalName
                } else {
                    list2.spaceName = null
                }
            })            
        },
        async batchQuery(param) {
            let res = await getSpaceName(param)
            let ret = res.data.Content
            if (res.data.Count > 0) {
                return ret
            }
            return []
        },
        async getSpId(param) {
            let res = await getSpaceId(param)
            let ret = res.data.Content
            if (res.data.Count > 0) {
                return ret
            }
            return []
        }
    },
    watch: {
        buildVlaue() {
            this.getAsset()
        }
    }
}
</script>

<style scoped lang='less'>
.btn-box {
    padding: 10px;
}
</style>
