<template>
    <div>
        <div class="top-box flex-align">
            <div>
                <a-button type="primary" style="margin-right: 24px;" @click="showModal('add')">创建角色</a-button>
                <!-- <a-button type="primary">删除</a-button> -->
            </div>
            <a-input-search style="width:400px;" placeholder="搜索角色" enter-button @search="onSearch" />
        </div>
        <a-table :columns="columns" :loading="loading" :dataSource="data" size="small" :rowKey="record => record.id"
            :pagination="pagination" @change="handleChange">
            <template slot="action" slot-scope="text,record">
                <a class="m-r8" @click="showModal('edit',record)">修改</a>
                <a class="m-r8" @click="_RoleDelete(record)">删除</a>
            </template>
        </a-table>
        <EditRole ref="EditRole" />
    </div>
</template>
<script>
    const columns = [
        {
            title: "角色名称",
            dataIndex: "name",
            key: "name",
            scopedSlots: { customRender: "name" },
            ellipsis: true,
            width: 180
        },
        {
            title: "显示名称",
            dataIndex: "displayName",
            key: "displayName",
            scopedSlots: { customRender: "displayName" },
            ellipsis: true,
            width: 100
        },
        {
            title: "操作",
            key: "action",
            dataIndex: "action",
            scopedSlots: { customRender: "action" },
            width: 160,
        },
    ]
    import { myPagination } from '@/mixins/myPagination.js'
    import EditRole from './EditRole.vue'
    import { RoleGetAll, RoleGetAllPermissions, RoleDelete } from '@/api/api'
    export default {
        mixins: [myPagination],
        components: {
            EditRole
        },
        data() {
            return {
                columns,
                loading: false,
                data: [],
                selectedRowKeys: [],
                KeyWord: '',
                IsActive: '',
                permissions: [],//所有权限
            }
        },
        mounted() {
            this.$store.dispatch('GetQuestionTypeList');
            this._RoleGetAll();
            this._RoleGetAllPermissions();
        },
        methods: {
            //删除角色
            _RoleDelete(record) {
                let _this = this;
                this.$confirm({
                    okText: '确认',
                    cancelText: '取消',
                    mask: false,
                    title: `您确认要删除"${record.name}"角色吗?`,
                    onOk() {
                        let params = {
                            Id: record.id,
                        };
                        console.log(params)
                        RoleDelete(params).then(res => {
                            _this.$message.success('删除成功！');
                            _this._RoleGetAll();
                        })
                    }
                });
            },
            //获取所有权限
            _RoleGetAllPermissions() {
                RoleGetAllPermissions().then(res => {
                    console.log(res, '所有权限');
                    this.permissions = res.items;
                })
            },
            getStr(obj, name) {
                let str = "";
                obj.forEach(item => {
                    str = str + item[name] + ' '
                })
                return str
            },
            //用户列表
            _RoleGetAll() {
                this.loading = true;
                let data = {
                    KeyWord: this.KeyWord,
                    SkipCount: (this.pagination.current - 1) * this.pagination.pageSize,
                    MaxResultCount: this.pagination.pageSize,
                }
                RoleGetAll(data).then(res => {
                    console.log(res, '角色列表')
                    this.data = res.items;
                    this.pagination.total = res.totalCount;
                    this.loading = false;
                }).catch(err => {
                    this.loading = false;
                })
            },
            showModal(type, record = {}) {
                this.$refs.EditRole.showModal(type, record)
            },
            onSearch(val) {
                this.KeyWord = val;
                const pager = { ...this.pagination };
                pager.current = 1;
                this.pagination = pager;
                this._RoleGetAll();
            },
            //表格选项选中
            onSelectChange(selectedRowKeys) {
                this.selectedRowKeys = selectedRowKeys;
            },
            //表格change
            handleChange(pagination, filters, sorter) {
                const pager = { ...this.pagination };
                pager.current = pagination.current;
                this.pagination = pager;
                this._RoleGetAll();
            },
        }
    }
</script>
<style scoped>
    .top-box {
        height: 64px;
        padding: 0 24px;
    }
</style>