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

.configaram {
    width: 100%;
    margin: auto;

    &__item {
        width: 100%;
        display: flex;

        &-left {
            flex: 1;
        }

        &-right {
            flex: none;
            width: 100px;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding-left: 15px;
            text-align: right;
        }
    }
}
</style>

<template>
    <div class="configaram">
        <app-search>
            <el-form :inline="true" :size="controlSize">
                <el-form-item>
                    <el-button type="success" @click="openAddDlg">新增基站参数</el-button>
                </el-form-item>
            </el-form>
        </app-search>
        <app-table pagination paginationAlign="right" table :tableData="tableData.listData"
            :handlePageSizeChange="handlePageSizeChange" :handlePageNumChange="handlePageNumChange">
            <template slot="table-column">
                <el-table-column label="操作" header-align="center" width="100" testalign="center">
                    <template slot-scope="scope">
                        <el-dropdown>
                            <span class="el-dropdown-link">
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </span>
                            <el-dropdown-menu>
                                <el-dropdown-item @click.native="openUpdateDlg(scope.row)">修改</el-dropdown-item>
                                <el-dropdown-item @click.native="deleteBaseParam(scope.row)">删除</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </template>
        </app-table>

        <el-dialog width="45%" :title="dialogData.editData.title" center :visible.sync="dialogData.editData.show"
            :modal-append-to-body="false" custom-class="editDataDialog" :close-on-click-modal="false"
            :close-on-press-escape="false">
            <base-param-dlg :isUpdate="dialogData.editData.isUpdate" :id="dialogData.editData.id" @get="hGetData"
                @close="hClose" v-if="dialogData.editData.show" />
        </el-dialog>
    </div>
</template>


<script>
import { deleteBaseParam, getBaseParamPageList } from "@/api/user"
import BaseParamDlg from "./component/baseparamdlg.vue"

export default {
    name: 'configaram',
    components: {
        BaseParamDlg,
    },
    data: function () {
        return {
            tableData: {
                listData: {
                    loading: false,
                    disabled: false,
                    totalCount: 0,
                    pageNum: 1,
                    pageSize: this.$store.getters.pageSize,
                    search: {
                        user_name: '',
                    },
                    columns: [
                        { key: 'base_id', name: '基站ID', width: 100 },
                        { key: 'carrier_id', name: '载波ID', width: 100 },
                        { key: 'timeslot', name: '时隙', width: 80 },
                        { key: 'bulid_id', name: '大楼', width: 100 },
                        { key: 'layer_id', name: '楼层', width: 80 },
                        { key: 'block_type_id', name: '类型', width: 100 },
                        { key: 'coord_x', name: 'X坐标', width: 100 },
                        { key: 'coord_y', name: 'X坐标', width: 100 },
                        { key: 'coord_z', name: 'X坐标', width: 100 },
                        { key: 'position', name: '楼层' },
                    ],
                    formatter: {
                    },
                    items: [],
                }
            },
            dialogData: {
                editData: {
                    show: false,
                    title: '新增基站参数',
                    isUpdate: false,
                    id: 0,
                },
            }
        }
    },
    watch: {
        updateTime(val, old) { //监听到改变后，重新初始化组件
            this.init()
        }
    },
    computed: {
        controlSize() {
            return this.$store.getters.controlSize
        }
    },
    beforeMount() {
    },
    mounted() {
        this.$nextTick(function () {
            this.init()
        })
    },
    methods: {
        init() {
            this.getBaseParamPageList()
        },
        hGetData() {
            this.getBaseParamPageList()
        },
        getBaseParamPageList() {//获得项目资料
            let pageNum = this.tableData.listData.pageNum
            let pageSize = this.tableData.listData.pageSize

            let param = { page_num: pageNum, page_size: pageSize, order_by: "id desc" }

            //查询数据
            this.tableData.listData.loading = true
            getBaseParamPageList(param).then((res) => {
                this.tableData.listData.totalCount = res.data.total_count
                this.tableData.listData.items = res.data.items
                this.tableData.listData.loading = false
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
                this.tableData.listData.loading = false
            })
        },
        handlePageSizeChange(val) { // 每页记录条数改变的事件
            //保存修改的PageSize
            this.$store.commit('app/setPageSize', val)

            //如果（每页记录条数改变后重新计算的总页数>=当前页），表示每页记录条数改变后，当前页不会改变，此处需要刷新table数据
            //如果（当前页*每页记录条数<=总记录条数），表示每页记录条数改变后，当前页不会改变，此处需要刷新table数据
            //如果（当前页*每页记录条数>总记录条数），表示每页记录条数改变后，当前页会重新计算并改变，handlePageNumChange会被调用，此处不需要刷新table数据
            var totalCount = this.tableData.listData.totalCount
            var pageNum = this.tableData.listData.pageNum
            var pageSize = this.tableData.listData.pageSize
            var pageCount = totalCount % pageSize === 0 ? totalCount / pageSize : Math.floor((totalCount / pageSize) + 1)
            if ((pageCount >= pageNum) || (pageNum * pageSize <= totalCount)) {
                this.getBaseParamPageList()
            }
        },
        handlePageNumChange(val) { // 当前页改变的事件，需要在此事件中刷新table数据
            this.tableData.listData.pageNum = val
            this.getBaseParamPageList()
        },
        hClose() {//关闭对话框
            this.dialogData.editData.show = false
        },
        openAddDlg() {//打开新增窗口
            this.dialogData.editData.isUpdate = false
            this.dialogData.editData.title = '新增基站参数'
            this.dialogData.editData.show = true
        },
        openUpdateDlg(item) {//打开修改窗口
            this.dialogData.editData.id = parseInt(item.id)

            this.dialogData.editData.isUpdate = true
            this.dialogData.editData.title = '修改基站参数'
            this.dialogData.editData.show = true
        },
        async deleteBaseParam(item) {//删除
            let paramData = {}

            const res = await this.$confirm("确定要删除吗", "提示", { type: "warning" }).catch(err => err)
            if (res != 'confirm') {
                return
            }

            paramData["id"] = parseInt(item.id)
            deleteBaseParam(paramData).then((res) => {
                this.$message.success('删除成功', '提示')
                this.getBaseParamPageList()
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        }
    }
}

</script>
