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

.btsparamdlg {
    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="btsparamdlg">
        <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="载波编号" v-if="mainData.bts_type == 1">
                <el-select v-model="dialogData.editData.data.carrier_id" clearable placeholder="请选择" style="width:100%">
                    <el-option v-for="(item, index) in mainData.carrierIdList" :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-select v-model="dialogData.editData.data.timeslot" clearable placeholder="请选择" style="width:100%">
                    <el-option v-for="(item, index) in mainData.timeslotIdList" :key="index" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="success" @click.native="hQueryOamBtsParam"
                :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, getBtsOamParamBts, setBtsOamParamBts } from "@/api/bparam"
import { genBtsTypeList } from "../basestaticdata.js"
export default {
    name: 'btsparamdlg',
    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,
                        carrier_id: 0,
                        timeslot: 0
                    },
                    rules: {
                    }
                }
            },
            mainData: {
                btsTypeList: genBtsTypeList(),
                carrierIdList: [{ label: '请选择', value: 0 }, { label: '1号载波', value: 1 }, { label: '2号载波', value: 2 }, { label: '3号载波', value: 3 }, { label: '4号载波', value: 4 }, { label: '5号载波', value: 5 }, { label: '6号载波', value: 6 }, { label: '7号载波', value: 7 }, { label: '8号载波', value: 8 }, { label: '9号载波', value: 9 }],
                timeslotIdList: [{ label: '请选择', value: 0 }, { label: '1号时隙', value: 1 }, { label: '2号时隙', value: 2 }, { label: '3号时隙', value: 3 }, { label: '4号时隙', value: 4 }],
                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.mainData.bts_type = this.dialogData.editData.data.bts_type
            }).catch((err) => {
                console.log(err)
            })
        },
        hQueryOamBtsParam() {
            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//基站编号
            getBtsOamParamBts(paramData).then((res) => {
                this.$nextTick(() => {//在DOM更新完成后再渲染数据
                    this.dialogData.editData.data = res.data
                    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['carrier_id'] = this.dialogData.editData.data.carrier_id
            paramData['timeslot'] = this.dialogData.editData.data.timeslot

            setBtsOamParamBts(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['carrier_id'] = this.dialogData.editData.data.carrier_id
            paramData['timeslot'] = this.dialogData.editData.data.timeslot

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

</script>
