<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="140px" :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="carrier_id" 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="时隙编号" prop="timeslot" 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-item label="定位类型" prop="location_type" v-if="mainData.bts_type == 1">
                <div class="btsparamdlg__item">
                    <div class="btsparamdlg__item-left">
                        <el-select v-model="dialogData.editData.data.location_type" clearable placeholder="请选择"
                            style="width:100%">
                            <el-option v-for="(item, index) in mainData.locationTypeIdList" :key="index"
                                :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="btsparamdlg__item-right">一维与融合定位，需要配置两站定位基站数量与列表
                    </div>
                </div>
            </el-form-item>
            <el-form-item label="两站定位基站数量" v-if="checkLctType && mainData.bts_type == 1">
                <div class="btsparamdlg__item">
                    <div class="btsparamdlg__item-left">
                        <el-input-number :precision="0" :step="1" :min="1"
                            v-model.trim="dialogData.editData.data.two_bts_num" auto-complete="off"
                            style="width:100%"></el-input-number>
                    </div>
                    <div class="btsparamdlg__item-right">该基站要与几个基站进行两站定位</div>
                </div>
            </el-form-item>
            <el-form-item label="两站定位基站列表" v-if="checkLctType && mainData.bts_type == 1">
                <el-select v-model="dialogData.editData.twoBtsList" multiple clearable placeholder="请选择"
                    style="width:100%">
                    <el-option v-for="(item, index) in organizeBtsIdList" :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, getBtsBaseParamPageList } from "@/api/bparam"
import { genBtsTypeList, genLocationTypeIdList } 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: {
                        build_id: 0,//大楼编号
                        layer_id: 0,//楼层编号
                        id: 0,
                        project_id: 0,
                        bts_type: 0,
                        bts_id: 0,
                        carrier_id: 0,
                        timeslot: 0,

                        location_type: 0,//定位类型
                        two_bts_num: 0,//两站定位基站数量
                        two_bts_list: '',//两站定位基站队列字符串，以逗号分隔
                    },
                    rules: {
                        carrier_id: [
                            { required: true, message: '请选择载波编号', trigger: 'blur' },
                        ],
                        timeslot: [
                            { required: true, message: '请选择载波时隙', trigger: 'blur' },
                        ],
                        location_type: [
                            { required: true, message: '请选择定位类型', trigger: 'blur' },
                        ]
                    },
                    twoBtsList: [],//两站定位基站队列
                }
            },
            mainData: {
                btsTypeList: genBtsTypeList(),
                locationTypeIdList: genLocationTypeIdList(),//定位类型
                btsList: [],//基站列表
                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
        },
        //组织接站ID列表
        organizeBtsIdList() {
            let dataList = []

            this.mainData.btsList.forEach(element => {
                let item = {}
                item['label'] = element.bts_id.toString()
                item['value'] = element.bts_id
                dataList.push(item)
            })

            return dataList
        },
        //检查定位类型
        checkLctType() {
            let oneDimensionFlag = false
            const locationType = this.dialogData.editData.data.location_type

            //1维定位或者融合定位
            if (locationType == 1 || locationType == 3) {
                oneDimensionFlag = true
            }

            return oneDimensionFlag
        },
        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

                //组织一维基站数据
                if (res.data.two_bts_list === null || typeof res.data.two_bts_list === 'undefined' || res.data.two_bts_list === "") {
                    this.dialogData.editData.twoBtsList = []
                } else {
                    this.dialogData.editData.twoBtsList = res.data.two_bts_list.split(",").map(Number)
                }

                //获得与该基站在同一大楼和楼层的基站
                this.getBtsBaseParamPageList()
            }).catch((err) => {
                console.log(err)
            })
        },
        //查询基站列表
        getBtsBaseParamPageList() {
            let search = {}
            search.project_id = this.dialogData.editData.data.project_id//项目编号
            search.build_id = this.dialogData.editData.data.build_id//大楼编号
            search.layer_id = this.dialogData.editData.data.layer_id//楼层编号

            //查询数据
            let param = { page_num: 1, page_size: 10000, search: search }
            getBtsBaseParamPageList(param).then((res) => {
                this.mainData.btsList = res.data.items
            }).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
                    //组织一维基站数据
                    if (res.data.two_bts_list === null || typeof res.data.two_bts_list === 'undefined' || res.data.two_bts_list === "") {
                        this.dialogData.editData.twoBtsList = []
                    } else {
                        this.dialogData.editData.twoBtsList = res.data.two_bts_list.split(",").map(Number)
                    }
                })
            }).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

            const locationType = parseInt(this.dialogData.editData.data.location_type)
            let twoBtsNum = parseInt(this.dialogData.editData.data.two_bts_num)
            let twoBtsList = this.dialogData.editData.twoBtsList.join(",")

            //一维或者融合定位
            if (locationType === 1 || locationType === 3) {
                if (twoBtsNum <= 0 || twoBtsNum > 6) {
                    this.$message({ message: '两站定位基站数量错误', type: 'error' })
                    return
                }

                if (twoBtsList.length <= 0 || twoBtsList.length > 48) {
                    this.$message({ message: '两站定位基站列表字符长度错误', type: 'error' })
                    return
                }
            } else {//二维定位
                twoBtsNum = 0
                twoBtsList = ""
            }

            paramData['location_type'] = locationType
            paramData['two_bts_num'] = twoBtsNum
            paramData['two_bts_list'] = twoBtsList

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

            const locationType = parseInt(this.dialogData.editData.data.location_type)
            let twoBtsNum = parseInt(this.dialogData.editData.data.two_bts_num)
            let twoBtsList = this.dialogData.editData.twoBtsList.join(",")

            //一维或者融合定位
            if (locationType === 1 || locationType === 3) {
                if (twoBtsNum <= 0 || twoBtsNum > 6) {
                    this.$message({ message: '两站定位基站数量错误', type: 'error' })
                    return
                }

                if (twoBtsList.length <= 0 || twoBtsList.length > 48) {
                    this.$message({ message: '两站定位基站列表字符长度错误', type: 'error' })
                    return
                }
            } else {//二维定位
                twoBtsNum = 0
                twoBtsList = ""
            }

            paramData['location_type'] = locationType
            paramData['two_bts_num'] = twoBtsNum
            paramData['two_bts_list'] = twoBtsList

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

</script>
