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

.syncparamdlg {
    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="syncparamdlg">
        <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="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="中心频率">
                <div class="syncparamdlg__item">
                    <el-select v-model="dialogData.editData.data.center_freq" clearable placeholder="请选择"
                        style="width:100%">
                        <el-option v-for="(item, index) in mainData.centerFreqList" :key="index" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                    <div class="syncparamdlg__item-right">mHz</div>
                </div>
            </el-form-item>
            <el-form-item label="发射功率" v-if="mainData.bts_type == 2 || mainData.bts_type == 3">
                <div class="syncparamdlg__item">
                    <div class="syncparamdlg__item-left">
                        <el-select v-model="dialogData.editData.data.tx_power" clearable placeholder="请选择"
                            style="width:100%">
                            <el-option v-for="(item, index) in mainData.txPowerList" :key="index" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="syncparamdlg__item-right">dBm</div>
                </div>
            </el-form-item>
            <el-form-item label="接收电平" v-if="mainData.bts_type == 1">
                <div class="syncparamdlg__item">
                    <div class="syncparamdlg__item-left">
                        <el-input-number :precision="0" :step="1" :min="-128" disabled
                            v-model.trim="dialogData.editData.data.rssi" auto-complete="off"
                            style="width:100%"></el-input-number>
                    </div>
                    <div class="syncparamdlg__item-right">dBm</div>
                </div>
            </el-form-item>
        </el-form>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="success" @click.native="hQueryOamSyncParam"
                :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, getBtsOamParamSync, setBtsOamParamSync } from "@/api/bparam"
import { genSyncTxPowerList, genCenterFreqList, genBtsTypeList } from "../basestaticdata.js"
export default {
    name: 'syncparamdlg',
    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,
                        center_freq: 0,
                        tx_power: 0,
                        rssi: -128,//接收电平
                    },
                    rules: {
                    }
                }
            },
            mainData: {
                btsTypeList: genBtsTypeList(),
                txPowerList: genSyncTxPowerList(),
                centerFreqList: genCenterFreqList(),
                btnEnable: true,
                center_freq: 0,
                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)
            })
        },
        hQueryOamSyncParam() {
            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//基站编号
            getBtsOamParamSync(paramData).then((res) => {
                this.$nextTick(() => {
                    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['center_freq'] = this.dialogData.editData.data.center_freq
            paramData['tx_power'] = this.dialogData.editData.data.tx_power

            setBtsOamParamSync(paramData).then((res) => {
                this.$message({ message: '远程设置参数成功', type: 'success' })
            }).catch(() => {
                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['center_freq'] = this.dialogData.editData.data.center_freq
            paramData['tx_power'] = this.dialogData.editData.data.tx_power

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

</script>
