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

.rtcdlg {
    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="rtcdlg">
        <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="rtc_date_time">
                <el-date-picker v-model="dialogData.editData.data.rtc_date_time" type="datetime" style="width:100%"
                    value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期和时间" />
            </el-form-item>
            <el-form-item label="定时开关" prop="sync_timer_switch">
                <el-select v-model="dialogData.editData.data.sync_timer_switch" clearable placeholder="请选择"
                    style="width:100%">
                    <el-option key=0 label="关闭" :value=0 />
                    <el-option key=1 label="打开" :value=1 />
                </el-select>
            </el-form-item>
            <el-form-item label="定时数量" prop="sync_timer_num">
                <el-input-number :precision="0" :step="1" :min="1" :max="2"
                    v-model.trim="dialogData.editData.data.sync_timer_num" auto-complete="off"
                    style="width:100%"></el-input-number>
            </el-form-item>
            <el-form-item label="定时1开始" prop="sync_timer1_start">
                <el-time-picker v-model="dialogData.editData.data.sync_timer1_start" style="width:100%" format="HH:mm"
                    value-format="HH:mm" :picker-options="{ selectableRange: '00:00:00 - 23:59:59' }"
                    placeholder="任意时间点" />
            </el-form-item>
            <el-form-item label="定时1结束" prop="sync_timer1_stop">
                <el-time-picker v-model="dialogData.editData.data.sync_timer1_stop" style="width:100%" format="HH:mm"
                    value-format="HH:mm" :picker-options="{ selectableRange: '00:00:00 - 23:59:59' }"
                    placeholder="任意时间点" />
            </el-form-item>
            <el-form-item label="定时2开始" prop="sync_timer2_start">
                <el-time-picker v-model="dialogData.editData.data.sync_timer2_start" style="width:100%" format="HH:mm"
                    value-format="HH:mm" :picker-options="{ selectableRange: '00:00:00 - 23:59:59' }"
                    placeholder="任意时间点" />
            </el-form-item>
            <el-form-item label="定时2结束" prop="sync_timer2_stop">
                <el-time-picker v-model="dialogData.editData.data.sync_timer2_stop" style="width:100%" format="HH:mm"
                    value-format="HH:mm" :picker-options="{ selectableRange: '00:00:00 - 23:59:59' }"
                    placeholder="任意时间点" />
            </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 { getBtsBaseParamById, updateBtsBaseParam, getBtsOamParamRtc, setBtsOamParamRtc } from "@/api/bparam"

export default {
    name: 'rtcdlg',
    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,

                        rtc_date_time: '',
                        sync_timer_switch: 0,
                        sync_timer_num: 1,
                        sync_timer1_start: 0,
                        sync_timer1_stop: 0,
                        sync_timer2_start: 0,
                        sync_timer2_stop: 0,
                    },
                    rules: {
                        rtc_date_time: [
                            { required: true, message: '请选择初始日期和时间', trigger: 'blur' },
                        ],
                        sync_timer_switch: [
                            { required: true, message: '请选择同步定时开关', trigger: 'blur' },
                        ],
                        sync_timer_num: [
                            { required: true, message: '请选择同步定时数量', trigger: 'blur' },
                        ],
                        sync_timer1_start: [
                            { required: true, message: '请选择同步定时1开始时间', trigger: 'blur' },
                        ],
                        sync_timer1_stop: [
                            { required: true, message: '请选择同步定时1结束时间', trigger: 'blur' },
                        ],
                        sync_timer2_start: [
                            { required: true, message: '请选择同步定时2开始时间', trigger: 'blur' },
                        ],
                        sync_timer2_stop: [
                            { required: true, message: '请选择同步定时2结束时间', trigger: 'blur' },
                        ]
                    }
                }
            },
            mainData: {
                btnEnable: true,
            }
        }
    },
    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()
            }
        },
        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
            }).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//基站编号
            getBtsOamParamRtc(paramData).then((res) => {
                this.$nextTick(() => {
                    this.dialogData.editData.data = res.data
                })
            }).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'] = parseInt(this.dialogData.editData.data.bts_id)

            paramData['rtc_date_time'] = this.dialogData.editData.data.rtc_date_time
            paramData['sync_timer_switch'] = this.dialogData.editData.data.sync_timer_switch
            paramData['sync_timer_num'] = this.dialogData.editData.data.sync_timer_num
            paramData['sync_timer1_start'] = this.dialogData.editData.data.sync_timer1_start
            paramData['sync_timer1_stop'] = this.dialogData.editData.data.sync_timer1_stop
            paramData['sync_timer2_start'] = this.dialogData.editData.data.sync_timer2_start
            paramData['sync_timer2_stop'] = this.dialogData.editData.data.sync_timer2_stop

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

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

            paramData['id'] = parseInt(this.id)//记录ID
            paramData['rtc_date_time'] = this.dialogData.editData.data.rtc_date_time
            paramData['sync_timer_switch'] = this.dialogData.editData.data.sync_timer_switch
            paramData['sync_timer_num'] = this.dialogData.editData.data.sync_timer_num
            paramData['sync_timer1_start'] = this.dialogData.editData.data.sync_timer1_start
            paramData['sync_timer1_stop'] = this.dialogData.editData.data.sync_timer1_stop
            paramData['sync_timer2_start'] = this.dialogData.editData.data.sync_timer2_start
            paramData['sync_timer2_stop'] = this.dialogData.editData.data.sync_timer2_stop

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

</script>
