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

.perssion {
    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="perssion">
        <app-search>
            <el-form :inline="true" :size="controlSize">
                <el-form-item>
                    <el-button type="success" @click="openAddPageDlg">新增页面权限</el-button>
                </el-form-item>
            </el-form>
        </app-search>
        <el-table :data="tableData.listData.items" :border="true" row-key="id" default-expand-all>
            <el-table-column label="权限名称" prop="name" width="150px"></el-table-column>
            <el-table-column label="权限标识符" prop="flag" width="150px"></el-table-column>
            <el-table-column label="权限描述" prop="desc"></el-table-column>
            <el-table-column label="操作" width="120px">
                <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="deletePermission(scope.row)">删除页面权限</el-dropdown-item>
                            <hr />
                            <el-dropdown-item @click.native="openAddBtnDlg(scope.row)"
                                v-if="scope.row.pid === 0">新增按钮权限</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-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">
            <permission-dlg :isUpdate="dialogData.editData.isUpdate" :typeId="dialogData.editData.typeId"
                :id="dialogData.editData.id" :pid="dialogData.editData.pid" @get="hGetData" @close="hClose"
                v-if="dialogData.editData.show" />
        </el-dialog>
    </div>
</template>


<script>
import { deletePermission, getPermissionList } from "@/api/user"
import { tranListToTreeData } from "@/utils"
import PermissionDlg from "./component/permissiondlg.vue"

export default {
    name: 'permission',
    components: {
        PermissionDlg
    },
    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: 'flag', name: '权限标识符', width: 200 },
                        { key: 'desc', name: '权限描述' },
                    ],
                    formatter: {
                    },
                    items: [],
                }
            },
            dialogData: {
                editData: {
                    show: false,
                    title: '新增管理角色',
                    isUpdate: false,
                    typeId: 1,
                    id: 0,
                    pid: 0
                }
            }
        }
    },
    watch: {
        updateTime(val, old) { //监听到改变后，重新初始化组件
            this.init()
        }
    },
    computed: {
        controlSize() {
            return this.$store.getters.controlSize
        }
    },
    beforeMount() {
    },
    mounted() {
        this.$nextTick(function () {
            this.init()
        })
    },
    methods: {
        init() {
            this.getPermissionList()
        },
        hGetData() {
            this.getPermissionList()
        },
        getPermissionList() {//获得项目资料
            //查询数据
            this.tableData.listData.loading = true
            getPermissionList().then((res) => {
                this.tableData.listData.totalCount = res.data.total_count
                this.tableData.listData.items = tranListToTreeData(res.data)
                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.getPermissionList()
            }
        },
        handlePageNumChange(val) { // 当前页改变的事件，需要在此事件中刷新table数据
            this.tableData.listData.pageNum = val
            this.getPermissionList()
        },
        hClose() {//关闭对话框
            this.dialogData.editData.show = false
        },
        openAddPageDlg() {//打开新增页面权限窗口
            this.dialogData.editData.pid = 0
            this.dialogData.editData.typeId = 1
            this.dialogData.editData.isUpdate = false
            this.dialogData.editData.title = '新增页面权限'
            this.dialogData.editData.show = true
        },
        openAddBtnDlg(item) {//打开新增按钮权限窗口
            this.dialogData.editData.pid = parseInt(item.id)
            this.dialogData.editData.typeId = 2
            this.dialogData.editData.isUpdate = false
            this.dialogData.editData.title = '新增按钮权限'
            this.dialogData.editData.show = true
        },
        openUpdateDlg(item) {//打开修改窗口
            this.dialogData.editData.pid = parseInt(item.pid)
            this.dialogData.editData.id = parseInt(item.id)
            this.dialogData.editData.typeId = parseInt(item.type_id)

            this.dialogData.editData.isUpdate = true
            if (this.dialogData.editData.typeId === 1) {
                this.dialogData.editData.title = '修改页面权限'
            } else if (this.dialogData.editData.typeId === 2) {
                this.dialogData.editData.title = '修改按钮权限'
            }
            this.dialogData.editData.show = true
        },
        async deletePermission(item) {//删除
            let paramData = {}

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

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

</script>
