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

.locationdlg {
    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;
            font-size: 12px;
        }
    }
}
</style>

<template>
    <div class="locationdlg">
        <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="location_type">
                <div class="locationdlg__item">
                    <div class="locationdlg__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="locationdlg__item-right">1维与融合定位，需要配置1维基站数量及列表
                    </div>
                </div>
            </el-form-item>
            <el-form-item label="1维基站数量" v-if="checkLctType">
                <div class="locationdlg__item">
                    <div class="locationdlg__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="locationdlg__item-right">该基站要与几个基站进行一维定位</div>
                </div>
            </el-form-item>
            <el-form-item label="1维基站列表" v-if="checkLctType">
                <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="hQueryOamParam"
                :disabled="mainData.btnEnable == false">远程查询</el-button>
            <el-button type="primary" @click.native="hSetOamParam"
                :disabled="mainData.btnEnable == false">远程配置</el-button>
            <el-button type="danger" @click="hSaveOamParam">保存至数据库</el-button>
            <el-button type="warning" @click="hClose">关闭</el-button>
        </div>
    </div>
</template>


<script>
import { updateBtsBaseParam, getBtsBaseParamById, getBtsBaseParamPageList, getBtsOamParamLocationType, setBtsOamParamLocationType } from "@/api/bparam"
import { genLocationTypeIdList } from "../basestaticdata.js"

export default {
    name: 'locationdlg',
    props: {
        isUpdate: {
            type: Boolean,
            require: true,
            default: false
        },
        typeId: {
            type: Number,
            require: true,
            default: 1
        },
        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,

                        location_type: 0,
                        two_bts_num: 0,//1维基站数量
                        two_bts_list: '',//一维定位基站队列字符串，以逗号分隔
                    },
                    twoBtsList: [],//一维定位基站队列
                    rules: {
                        project_id: [
                            { required: true, message: '请输入项目编号', trigger: 'blur' },
                        ],
                        bts_id: [
                            { required: true, message: '请输入基站编号', trigger: 'blur' },
                        ],
                        location_type: [
                            { required: true, message: '请选择定位类型', trigger: 'blur' },
                        ]
                    }
                }
            },
            mainData: {
                locationTypeIdList: genLocationTypeIdList(),
                btsList: [],//基站列表
                btnEnable: true,
            }
        }
    },
    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()
            }
        },
        getData() {
            this.$emit("get")
        },
        hClose() {//关闭对话框
            this.$emit("close")
        },
        getBtsBaseParamById() {
            let paramData = {}
            paramData.id = parseInt(this.id)
            getBtsBaseParamById(paramData).then((res) => {
                this.dialogData.editData.data = res.data

                //组织一维基站数据
                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)
            })
        },
        hQueryOamParam() {
            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//基站编号
            getBtsOamParamLocationType(paramData).then((res) => {
                this.$nextTick(() => {
                    this.dialogData.editData.data = res.data
                    //组织一维基站数据
                    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
            }

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

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

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

            paramData['id'] = parseInt(this.id)//记录ID
            paramData['bts_id'] = parseInt(this.dialogData.editData.data.bts_id)
            paramData['location_type'] = locationType
            paramData['two_bts_num'] = twoBtsNum
            paramData['two_bts_list'] = twoBtsList

            setBtsOamParamLocationType(paramData).then((res) => {
                this.$message({ message: '远程设置参数成功', type: 'success' })
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        hSaveOamParam() {
            let paramData = {}
            let twoBtsNumTmp = 0
            let twoBtsListTmp = []

            if (parseInt(this.dialogData.editData.data.project_id) < 1000) {
                this.$message.success('项目编号不能为空，请与系统管理员联系', '提示')
                return
            }

            paramData['id'] = parseInt(this.id)//记录ID

            //定位类型
            paramData.location_type = parseInt(this.dialogData.editData.data.location_type)

            //1维定位或者融合定位
            if (paramData.location_type === 1 || paramData.location_type === 3) {
                twoBtsNumTmp = parseInt(this.dialogData.editData.data.two_bts_num)
                twoBtsListTmp = this.dialogData.editData.twoBtsList
                if (twoBtsListTmp.length != twoBtsNumTmp) {
                    this.$message.success('一维基站数量与一维基站列表不符', '提示')
                    return
                } else {
                    paramData.two_bts_num = twoBtsNumTmp;
                    paramData.two_bts_list = twoBtsListTmp.join(",")
                }


            } else {
                this.dialogData.editData.data.two_bts_num = 0
                this.dialogData.editData.twoBtsList = []
                paramData.two_bts_num = 0;
                paramData.two_bts_list = ''
            }

            //修改基站基本参数
            updateBtsBaseParam(paramData).then(() => {
                this.getData()//刷新数据
                this.$message.success('保存参数成功', '提示')
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        }
    }
}

</script>
