<template>
    <el-dialog title='添加对应岗位' :visible.sync='dialogVisible' width='500px'>
        <div class='add-box'>
            <el-select v-model='value' placeholder='资产设备族对应的设备类' clearable style="width: 300px">
                <el-option v-for='item in options' :key='item.code' :label='item.name' :value='item.code'></el-option>
            </el-select>
        </div>
        <span slot='footer' class='dialog-footer'>
            <el-button type='primary' @click='save'>确定创建对应岗位</el-button>
        </span>
    </el-dialog>
</template>

<script>
import {
    createPost, //新建岗位
    upDateTableMain, //关联资产
    createComponent //新建岗位（部件）
} from '@/api/scan/request'
import { mapGetters } from 'vuex'
export default {
    name: 'saga-station',
    data() {
        return {
            dialogVisible: false,
            value: ''
        }
    },
    computed: {
        ...mapGetters('layout', ['projectId', 'secret', 'userId'])
    },
    props: {
        options: {
            default: []
        },
        itemObj: {
            default: {}
        }
    },
    methods: {
        show() {
            this.dialogVisible = true
        },
        save() {
            if (this.value) {
                let param = {
                    BuildId: this.itemObj.BuildId,
                    category: this.value,
                    secret: this.secret,
                    perjectId: this.projectId,
                    BIMID: this.itemObj.FloorId,
                    BIMLocation: (this.itemObj.X || 0) + ',' + (this.itemObj.Y || 0) + ',' + (this.itemObj.Z || 0)
                }
                if (param.category.length == 6) {
                    createComponent(param).then(res => {
                        if (res.data.Result == 'success') {
                            this.upDateTableMain(res.data.id)
                        } else {
                            this.$message({
                                message: res.data.ResultMsg,
                                type: 'warning'
                            })
                        }
                    })
                } else {
                    createPost(param).then(res => {
                        if (res.data.Result == 'success') {
                            this.upDateTableMain(res.data.id)
                        } else {
                            this.$message({
                                message: res.data.ResultMsg,
                                type: 'warning'
                            })
                        }
                    })
                }
            } else {
                this.$message({
                    message: '请选择设备类后再进行关联',
                    type: 'warning'
                })
            }
        },
        //关联资产
        upDateTableMain(id) {
            let param = {
                    ProjId: this.projectId,
                    UserId: this.userId
                },
                paramList = {}
            paramList.EquipmentId = id
            paramList.FmId = this.itemObj.FmId
            paramList.Family = this.itemObj.Family
            upDateTableMain(param, [paramList]).then(res => {
                if (res.data.Result == 'success') {
                    this.$message({
                        message: '保存成功',
                        type: 'success'
                    })
                    this.dialogVisible = false
                    this.$emit('refresh')
                } else {
                    this.$message.error('请求失败')
                }
            })
        }
    },
    watch: {
        options: {
            deep: true,
            handler(val) {
                this.value = ''
            }
        }
    }
}
</script>

<style scoped lang='less'>
.add-box {
    height: 100px;
}
</style>
