<style lang="scss">
@import '@/styles/index.scss'; //导入样式

.installparamdlg {
    width: 100%;
    margin: auto;

    &__item {
        width: 100%;
        display: flex;

        &-left {
            flex: 1;
        }

        &-right {
            flex: none;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding-left: 15px;
            text-align: right;
        }
    }
}
</style>

<template>
    <div class="installparamdlg">
        <el-form :model="dialogData.editData.data" :rules="dialogData.editData.rules" ref="editDataForm"
            label-width="100px" :size="controlSize">
            <el-form-item label="项目编号">
                <el-input v-model="calProjectId" disabled auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="基站编号">
                <el-input-number :precision="0" :step="1" :min="1000" disabled
                    v-model.trim="dialogData.editData.data.bts_id" auto-complete="off"
                    style="width:100%"></el-input-number>
            </el-form-item>
            <el-form-item label="设备类型" prop="bts_type">
                <el-select v-model="dialogData.editData.data.bts_type" disabled clearable placeholder="请选择"
                    style="width:100%">
                    <el-option v-for="(item, index) in mainData.btsTypeList" :key="index" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="大楼编号" prop="build_id">
                <el-select v-model="dialogData.editData.data.build_id" clearable placeholder="请选择" style="width:100%">
                    <el-option v-for="(item, index) in mainData.buildIdList" :key="index" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="楼层编号" prop="layer_id">
                <el-select v-model="dialogData.editData.data.layer_id" clearable placeholder="请选择" style="width:100%">
                    <el-option v-for="(item, index) in mainData.layerIdList" :key="index" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="分区编号" prop="layer_id" v-if="mainData.bts_type == 1">
                <el-select v-model="dialogData.editData.data.block_id" clearable placeholder="请选择" style="width:100%">
                    <el-option v-for="(item, index) in mainData.blockIdList" :key="index" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="分区描述" v-if="mainData.bts_type == 1">
                <el-input v-model="dialogData.editData.data.block_desc" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="X坐标" prop="coord_x" v-if="mainData.bts_type == 1">
                <div class="installparamdlg__item">
                    <div class="installparamdlg__item-left">
                        <el-input-number :precision="2" :step="1" :min="-10000"
                            v-model.trim="dialogData.editData.data.coord_x" auto-complete="off"
                            style="width:100%"></el-input-number>
                    </div>
                    <div class="installparamdlg__item-right">单位：米</div>
                </div>
            </el-form-item>
            <el-form-item label="Y坐标" prop="coord_y" v-if="mainData.bts_type == 1">
                <div class="installparamdlg__item">
                    <div class="installparamdlg__item-left">
                        <el-input-number :precision="2" :step="1" :min="-10000"
                            v-model.trim="dialogData.editData.data.coord_y" auto-complete="off"
                            style="width:100%"></el-input-number>
                    </div>
                    <div class="installparamdlg__item-right">单位：米</div>
                </div>
            </el-form-item>
            <el-form-item label="Z坐标" prop="coord_z" v-if="mainData.bts_type == 1">
                <div class="installparamdlg__item">
                    <div class="installparamdlg__item-left">
                        <el-input-number :precision="2" :step="1" :min="-10000"
                            v-model.trim="dialogData.editData.data.coord_z" auto-complete="off"
                            style="width:100%"></el-input-number>
                    </div>
                    <div class="installparamdlg__item-right">单位：米</div>
                </div>
            </el-form-item>
            <el-form-item label="安装位置" prop="position">
                <el-input v-model="dialogData.editData.data.position" auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="success" @click.native="hQueryOamInstallParam"
                :disabled="mainData.btnEnable == false">远程查询</el-button>
            <el-button type="primary" @click.native="hSetOamParam"
                :disabled="mainData.btnEnable == false">远程配置</el-button>
            <el-button type="danger" @click.native="hSaveOamParam"
                :disabled="mainData.btnEnable == false">保存至数据库</el-button>
            <el-button type="warning" @click="hClose">关闭</el-button>
        </div>
    </div>
</template>

<script>
import { getBtsBaseParamById, updateBtsBaseParam, getBtsOamParamInstall, setBtsOamParamInstall } from "@/api/bparam"
import { genBuildIdList, genLayerIdList, genBlockIdList, genBtsTypeList } from "../basestaticdata.js"

export default {
    name: 'installparamdlg',
    props: {
        isUpdate: {
            type: Boolean,
            require: true,
            default: false
        },
        id: {
            type: Number,
            require: true,
            default: 1
        },
        pid: {
            type: Number,
            require: true,
            default: 0
        }
    },
    data: function () {
        return {
            dialogData: {
                editData: {
                    data: {
                        id: 0,
                        project_id: 0,
                        bts_id: 0,
                        bts_type: 0,

                        build_id: 0,
                        layer_id: 0,
                        block_id: 0,
                        block_desc: '',

                        position: '',
                        coord_x: 0,
                        coord_y: 0,
                        coord_z: 0,
                    },
                    rules: {
                    }
                }
            },
            mainData: {
                btsTypeList: genBtsTypeList(),
                buildIdList: genBuildIdList(),//大楼编号列表
                layerIdList: genLayerIdList(),//楼层列表
                blockIdList: genBlockIdList(),
                btnEnable: true,
                bts_type: 0,
            }
        }
    },
    watch: {
    },
    computed: {
        controlSize() {
            return this.$store.getters.controlSize
        },
        calProjectId() {
            let hexStr = this.dialogData.editData.data.project_id.toString(16).toUpperCase()
            return hexStr.padStart(8, '0')
        }
    },
    mounted() {
        this.$nextTick(function () {
            this.init()
        })
    },
    methods: {
        init() {
            if (this.isUpdate) {
                this.getBtsBaseParamById()
            }
        },
        hClose() {//关闭对话框
            this.$emit("close")
        },
        getData() {
            this.$emit("get")
        },
        getBtsBaseParamById() {
            let paramData = {}
            paramData.id = parseInt(this.id)
            getBtsBaseParamById(paramData).then((res) => {
                this.dialogData.editData.data = res.data
                this.dialogData.editData.data.coord_x = parseFloat(parseInt(res.data.coord_x) / 100)
                this.dialogData.editData.data.coord_y = parseFloat(parseInt(res.data.coord_y) / 100)
                this.dialogData.editData.data.coord_z = parseFloat(parseInt(res.data.coord_z) / 100)
                this.mainData.bts_type = this.dialogData.editData.data.bts_type
            }).catch((err) => {
                console.log(err)
            })
        },
        hQueryOamInstallParam() {
            let paramData = {}

            //禁用查询按钮
            this.mainData.btnEnable = false
            setTimeout(() => {
                this.mainData.btnEnable = true
            }, 1500)

            //查询数据
            const bts_id = parseInt(this.dialogData.editData.data.bts_id)
            if (bts_id < 1000) {
                this.$message({ message: '请输入正确的基站ID', type: 'error' })
                return
            }

            paramData['bts_id'] = bts_id//基站编号
            getBtsOamParamInstall(paramData).then((res) => {
                this.$nextTick(() => {
                    this.dialogData.editData.data = res.data
                    this.dialogData.editData.data.coord_x = parseFloat(parseInt(res.data.coord_x) / 100)
                    this.dialogData.editData.data.coord_y = parseFloat(parseInt(res.data.coord_y) / 100)
                    this.dialogData.editData.data.coord_z = parseFloat(parseInt(res.data.coord_z) / 100)
                    this.dialogData.editData.data.bts_type = this.mainData.bts_type
                })
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        hSetOamParam() {
            let paramData = {}

            //禁用查询按钮
            this.mainData.btnEnable = false
            setTimeout(() => {
                this.mainData.btnEnable = true
            }, 1500)

            //查询数据
            const bts_id = parseInt(this.dialogData.editData.data.bts_id)
            if (bts_id < 1000) {
                this.$message({ message: '请输入正确的基站ID', type: 'error' })
                return
            }

            paramData['id'] = parseInt(this.id)//记录ID
            paramData['bts_id'] = this.dialogData.editData.data.bts_id

            paramData['build_id'] = this.dialogData.editData.data.build_id
            paramData['layer_id'] = this.dialogData.editData.data.layer_id
            paramData['block_id'] = this.dialogData.editData.data.block_id
            paramData['block_desc'] = this.dialogData.editData.data.block_desc

            paramData['position'] = this.dialogData.editData.data.position
            paramData['coord_x'] = parseInt(parseFloat(this.dialogData.editData.data.coord_x) * 100)
            paramData['coord_y'] = parseInt(parseFloat(this.dialogData.editData.data.coord_y) * 100)
            paramData['coord_z'] = parseInt(parseFloat(this.dialogData.editData.data.coord_z) * 100)

            setBtsOamParamInstall(paramData).then((res) => {
                this.$message({ message: '远程设置参数成功', type: 'success' })
            }).catch((err) => {
                this.$message({ message: '远程设置参数失败', type: 'error' })
            })
        },
        hSaveOamParam() {
            let paramData = {}

            //禁用查询按钮
            this.mainData.btnEnable = false
            setTimeout(() => {
                this.mainData.btnEnable = true
            }, 1500)

            //查询数据
            const bts_id = parseInt(this.dialogData.editData.data.bts_id)
            if (bts_id < 1000) {
                this.$message({ message: '请输入正确的基站ID', type: 'error' })
                return
            }

            paramData['id'] = parseInt(this.id)//记录ID

            paramData['build_id'] = this.dialogData.editData.data.build_id
            paramData['layer_id'] = this.dialogData.editData.data.layer_id
            paramData['block_id'] = this.dialogData.editData.data.block_id
            paramData['block_desc'] = this.dialogData.editData.data.block_desc

            paramData['position'] = this.dialogData.editData.data.position
            paramData['coord_x'] = parseInt(parseFloat(this.dialogData.editData.data.coord_x) * 100)
            paramData['coord_y'] = parseInt(parseFloat(this.dialogData.editData.data.coord_y) * 100)
            paramData['coord_z'] = parseInt(parseFloat(this.dialogData.editData.data.coord_z) * 100)

            updateBtsBaseParam(paramData).then((res) => {
                this.getData()//刷新数据
                this.$message({ message: '保存参数成功', type: 'success' })
            }).catch(() => {
                this.$message({ message: '保存参数失败', type: 'error' })
            })
        }
    }
}

</script>
