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

.baseparamdlg {
    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="baseparamdlg">
        <el-form :model="dialogData.editData.data" :rules="dialogData.editData.rules" ref="editDataForm"
            label-width="90px" :size="controlSize">
            <el-form-item label="项目编号" prop="project_id">
                <div class="baseparamdlg__item">
                    <div class="baseparamdlg__item-left">
                        <el-input v-model="dialogData.editData.data.project_id" auto-complete="off"
                            v-if="!isUpdate"></el-input>
                        <el-input v-model="calProjectId" auto-complete="off" areadonly v-else></el-input>
                    </div>
                    <div class="baseparamdlg__item-right"><span style="font-size:12px"><span
                                style="color:#ff0000">注意：</span>项目编号由系统管理员分配</span></div>
                </div>
            </el-form-item>
            <el-form-item label="基站类型" prop="bts_type">
                <el-select v-model="dialogData.editData.data.bts_type" clearable placeholder="请选择" style="width:100%">
                    <el-option key=1 label="声波基站" :value=1></el-option>
                    <el-option key=2 label="同步基站" :value=2></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="基站编号" prop="bts_id">
                <el-input-number :precision="0" :step="1" :min="10000" 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="enable_flag">
                <el-select v-model="dialogData.editData.data.enable_flag" clearable placeholder="请选择"
                    style="width:100%">
                    <el-option key=0 label="停用" :value=0></el-option>
                    <el-option key=1 label="启用" :value=1></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="success" @click="hSubmit">确定</el-button>
            <el-button type="warning" @click="hClose">取消</el-button>
        </div>
    </div>
</template>


<script>
import { addBtsBaseParam, updateBtsBaseParam, getBtsBaseParamById } from "@/api/bparam"

export default {
    name: 'baseparamdlg',
    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_type: 1,//基站类型
                        bts_id: 0,
                        enable_flag: null,
                    },
                    rules: {
                        project_id: [
                            { required: true, message: '请输入项目编号', trigger: 'blur' },
                        ],
                        bts_type: [
                            { required: true, message: '请选择基站类型', trigger: 'blur' },
                        ],
                        bts_id: [
                            { required: true, message: '请输入基站编号', trigger: 'blur' },
                        ],
                        enable_flag: [
                            { required: true, message: '请选择使能标识', trigger: 'blur' },
                        ]
                    }
                }
            }
        }
    },
    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")
        },
        hSubmit() { //对话框确定按钮响应函数
            this.$refs.editDataForm.validate((valid) => {
                if (valid) {
                    if (!this.isUpdate) {
                        this.addBtsBaseParam()
                    } else {
                        this.updateBtsBaseParam()
                    }
                }
            })
        },
        addBtsBaseParam() {//新增
            let paramData = {}

            //项目编号
            const projectId = parseInt(this.dialogData.editData.data.project_id, 16)
            if (projectId <= 0) {
                this.$message.success('项目编号错误，请与系统管理员联系', '提示')
                return
            }
            paramData.project_id = projectId

            paramData.bts_type = parseInt(this.dialogData.editData.data.bts_type);
            paramData.bts_id = parseInt(this.dialogData.editData.data.bts_id)
            paramData.enable_flag = parseInt(this.dialogData.editData.data.enable_flag)

            //增加基站基本参数
            addBtsBaseParam(paramData).then((res) => {
                this.dialogData.editData.data.id = parseInt(res.data)
                this.$message.success('新增成功', '提示')
                this.getData()
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        updateBtsBaseParam() {//修改
            let paramData = {}

            paramData.id = parseInt(this.dialogData.editData.data.id)
            paramData.bts_type = parseInt(this.dialogData.editData.data.bts_type);
            paramData.enable_flag = parseInt(this.dialogData.editData.data.enable_flag)

            //修改基站基本参数
            updateBtsBaseParam(paramData).then(() => {
                this.$message.success('修改成功', '提示')
                this.getData()
                this.hClose()
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        getBtsBaseParamById() {
            let paramData = {}
            paramData.id = parseInt(this.id)
            getBtsBaseParamById(paramData).then((res) => {
                this.dialogData.editData.data = res.data
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        }
    }
}

</script>
