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

.netparamdlg {
    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: 10px;
            text-align: right;
            font-size: 12px;
        }
    }
}
</style>

<template>
    <div class="netparamdlg">
        <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" auto-complete="off" disabled></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="网管使能">
                <el-select v-model="dialogData.editData.data.network_switch" clearable placeholder="请选择"
                    style="width:100%">
                    <el-option v-for="(item, index) in mainData.switchList" :key="index" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="wifi名称">
                <el-input v-model="dialogData.editData.data.wifi_name" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="wifi密码">
                <el-input v-model="dialogData.editData.data.wifi_pwd" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="管理中心URL">
                <div class="netparamdlg">
                    <div class="netparamdlg__item">
                        <div class="netparamdlg__item-left">
                            <el-input v-model="dialogData.editData.data.service_ip" auto-complete="off"
                                placeholder="www.xxx.com或者ip地址"></el-input>
                        </div>
                        <div class="netparamdlg__item-right">管理中心的域名或者IP地址</div>
                    </div>
                </div>
            </el-form-item>
            <el-form-item label="管理中心端口">
                <div class="netparamdlg">
                    <div class="netparamdlg__item">
                        <div class="netparamdlg__item-left">
                            <el-input-number :precision="0" :step="1" :min="0"
                                v-model.trim="dialogData.editData.data.service_port" auto-complete="off"
                                style="width:100%"></el-input-number>
                        </div>
                        <div class="netparamdlg__item-right">管理中心在此端口监听链接</div>
                    </div>
                </div>
            </el-form-item>
        </el-form>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="success" @click.native="hQueryOamNetworkParam"
                :disabled="mainData.btnEnable == false">远程查询</el-button>
            <el-button type="primary" @click.native="hSetOamNetworkParam"
                :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, getBtsOamParamNetwork, setBtsOamParamNetwork } from "@/api/bparam"
import { genBtsTypeList } from "../basestaticdata.js"
export default {
    name: 'netparamdlg',
    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,
                        network_switch: null,
                        wifi_name: '',
                        wifi_pwd: '',
                        service_ip: '',
                        service_port: 80
                    },
                    rules: {
                    }
                }
            },
            mainData: {
                btsTypeList: genBtsTypeList(),
                switchList: [{ label: '关闭', value: 0 }, { label: '打开', value: 1 }],
                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)
            })
        },
        //查询网管参数
        hQueryOamNetworkParam() {
            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//基站编号
            getBtsOamParamNetwork(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' })
            })
        },
        hSetOamNetworkParam() {
            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['wifi_name'] = this.dialogData.editData.data.wifi_name
            paramData['wifi_pwd'] = this.dialogData.editData.data.wifi_pwd
            paramData['service_ip'] = this.dialogData.editData.data.service_ip
            paramData['service_port'] = this.dialogData.editData.data.service_port
            paramData['network_switch'] = this.dialogData.editData.data.network_switch

            setBtsOamParamNetwork(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['wifi_name'] = this.dialogData.editData.data.wifi_name
            paramData['wifi_pwd'] = this.dialogData.editData.data.wifi_pwd
            paramData['service_ip'] = this.dialogData.editData.data.service_ip
            paramData['service_port'] = this.dialogData.editData.data.service_port
            paramData['network_switch'] = this.dialogData.editData.data.network_switch

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

</script>
