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

.role {
    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="role">
        <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>
                                <hr />
                                <el-dropdown-item @click.native="deleteRole(scope.row)">删除角色</el-dropdown-item>
                                <hr />
                                <el-dropdown-item @click.native="openAssignPmsDlg(scope.row)">分配权限</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </template>
        </app-table>

        <el-dialog width="40%" v-drag-dialog :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">
            <role-dlg :isUpdate="dialogData.editData.isUpdate" :id="dialogData.editData.id" @get="hGetData"
                @close="hClose" v-if="dialogData.editData.show" />
        </el-dialog>

        <el-dialog width="40%" v-drag-dialog :title="dialogData.assignDlg.title" center
            :visible.sync="dialogData.assignDlg.show" :modal-append-to-body="false" custom-class="editDataDialog"
            :close-on-click-modal="false" :close-on-press-escape="false">
            <assign-pms-dlg :id="dialogData.assignDlg.id" @closeAssign="hCloseAssign"
                v-if="dialogData.assignDlg.show" />
        </el-dialog>
    </div>
</template>


<script>
import { deleteRole, getRolePageList } from "@/api/user"
import RoleDlg from "./component/roledlg.vue"
import AssignPmsDlg from "./component/assignpmsdlg.vue"

export default {
    name: 'operator',
    components: {
        RoleDlg,
        AssignPmsDlg
    },
    data: function () {
        return {
            tableData: {
                listData: {
                    loading: false,
                    disabled: false,
                    totalCount: 0,
                    pageNum: 1,
                    pageSize: this.$store.getters.pageSize,
                    search: {
                        user_name: '',
                    },
                    columns: [
                        { key: 'name', name: '角色名称', width: 150 },
                        { key: 'desc', name: '角色描述' },
                    ],
                    formatter: {
                    },
                    items: [],
                }
            },
            dialogData: {
                editData: {
                    show: false,
                    title: '新增管理角色',
                    isUpdate: false,
                    id: 0,
                },
                assignDlg: {
                    show: false,
                    title: '为角色分配权限',
                    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.getRolePageList()
        },
        hGetData() {
            this.getRolePageList()
        },
        getRolePageList() {//获得项目资料
            let pageNum = this.tableData.listData.pageNum
            let pageSize = this.tableData.listData.pageSize

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

            //查询数据
            this.tableData.listData.loading = true
            getRolePageList(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.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.getRolePageList()
            }
        },
        handlePageNumChange(val) { // 当前页改变的事件，需要在此事件中刷新table数据
            this.tableData.listData.pageNum = val
            this.getRolePageList()
        },
        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 deleteRole(item) {//删除
            let paramData = {}

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

            paramData["id"] = parseInt(item.id)
            deleteRole(paramData).then((res) => {
                this.$message.success('删除成功', '提示')
                this.getRolePageList()
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        hCloseAssign() {
            this.dialogData.assignDlg.show = false
        },
        openAssignPmsDlg(item) {
            this.dialogData.assignDlg.id = parseInt(item.id)
            this.dialogData.assignDlg.title = "为【" + item.name + "】分配权限"
            this.dialogData.assignDlg.show = true
        }
    }
}

</script>
