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

.deviceparamdlg {
    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="deviceparamdlg">
        <el-form :model="dialogData.editData.data" :rules="dialogData.editData.rules" ref="editDataForm"
            label-width="90px" :size="controlSize">
            <el-form-item label="项目编号" prop="project_id">
                <el-input v-model="calProjectId" disabled auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="基站编号" prop="bts_id">
                <el-input-number :precision="0" :step="1" :min="1001" 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="serial_number">
                <el-input v-model.trim="dialogData.editData.data.serial_number" readonly auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="设备型号" prop="bts_model">
                <el-input v-model.trim="dialogData.editData.data.bts_model" readonly auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="软件版本" prop="soft_version">
                <el-input v-model.trim="dialogData.editData.data.soft_version" readonly auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="蓝牙名称" prop="ble_name" v-if="dialogData.editData.data.bts_type == 1">
                <el-input v-model.trim="dialogData.editData.data.ble_name" auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="success" @click.native="hQueryOamDeviceParam"
                :disabled="mainData.btnEnable == false">远程查询</el-button>
            <el-button type="danger" @click.native="hSaveOamParam"
                :disabled="mainData.btnEnable == false">保存至数据库</el-button>
            <el-button type="warning" @click.native="hClose">关闭</el-button>
        </div>
    </div>
</template>

<script>
import { getBtsBaseParamById, getBtsOamParamDeviceHan, updateBtsBaseParam } from "@/api/bparam"
import { genBtsTypeList } from "../basestaticdata.js"

export default {
    name: 'deviceparamdlg',
    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,
                        serial_number: '',
                        bts_model: '',
                        soft_version: '',
                        ble_name: ''
                    },
                    rules: {
                    }
                }
            },
            mainData: {
                btsTypeList: genBtsTypeList(),
                btnEnable: true,
            }
        }
    },
    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
            }).catch((err) => {
                console.log(err)
            })
        },
        hQueryOamDeviceParam() {
            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//基站编号
            getBtsOamParamDeviceHan(paramData).then((res) => {
                this.$nextTick(() => {//在DOM更新完成后再渲染数据
                    this.dialogData.editData.data = res.data
                })
            }).catch((err) => {
                console.log("***********", err)
                this.$message({ message: err, 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['bts_type'] = this.dialogData.editData.data.bts_type
            paramData['serial_number'] = this.dialogData.editData.data.serial_number
            paramData['bts_model'] = this.dialogData.editData.data.bts_model
            paramData['soft_version'] = this.dialogData.editData.data.soft_version
            paramData['ble_name'] = this.dialogData.editData.data.ble_name

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

</script>
