<template>
    <div class="template-box">
        <div class="template-tools">
            <el-input
                style="width: 300px"
                placeholder="输入角色名进行查询"
                v-model="searchValue"
                clearable
                @clear="searchChange"
                @keyup.enter.native="searchChange"
            >
                <i slot="suffix" class="el-input__icon el-icon-search" @click="searchChange"></i>
            </el-input>
            <el-button style="float: right" @click="addRole">添加</el-button>
        </div>

        <div class="template-table">
            <el-table
                height="100%"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                :header-cell-style="{ background: '#d9d9d9', color: '#2b2b2b' }"
                v-loading="loading"
            >
                <el-table-column prop="name" label="角色名" header-align="center" align="center"></el-table-column>
                <el-table-column label="操作" width="150" header-align="center" class="btn">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit-outline" plain size="mini" @click="editRole(scope.row)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" plain size="mini" @click="delRole(scope.row)"></el-button>
                    </template>
                </el-table-column>
                <template slot="empty">
                    <div style="height: 60%; transform: translateY(50%)">
                        <i class="icon-wushuju iconfont"></i>
                        暂无用户数据
                    </div>
                </template>
            </el-table>
        </div>

        <div class="template-pagination">
            <my-pagination @change="getRoleList" :page="page" :key="pageKey"></my-pagination>
        </div>
        <role-dialog ref="userDialog" @refresh="getRoleList" :type="type"></role-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { queryRole, deleteRole } from "@/api/system/persagy-sso-server";
import myPagination from "@/components/ledger/lib/myPagination";
import roleDialog from "./dialog/roleDialog.vue";

export default {
    name: "userAdm",
    components: { myPagination, roleDialog },
    data() {
        return {
            searchValue: "",
            loading: false,
            type: "add",
            tableData: [],
            page: {
                size: 50,
                sizes: [10, 30, 50, 100, 150, 200],
                total: 0,
                currentPage: 1,
            },
            pageKey: new Date().getTime(),
        };
    },
    created() {
        this.init();
    },
    computed: {
        ...mapGetters("layout", ["projectId", "userId"]),
    },
    methods: {
        init() {
            this.getRoleList();
        },
        async getRoleList() {
            this.loading = true;
            const params = {
                pageNumber: this.page.currentPage,
                pageSize: this.page.size,
            };
            if (this.searchValue) params.filters = [{ ops: "CONTAIN", obj: "name", arg: this.searchValue }];
            const res = await queryRole(params);
            this.loading = false;
            if (res.code === "Success") {
                this.tableData = res.data || [];
                this.page.total = res.total;
            } else {
                this.$message.error(`${res.message}`);
            }
        },
        searchChange() {
            this.page.currentPage = 1;
            this.getRoleList();
        },
        addRole() {
            this.type = "add";
            this.$refs.userDialog.showDialog(this.type);
        },
        editRole(row) {
            this.type = "edit";
            this.$refs.userDialog.showDialog(this.type, row);
        },
        delRole(row) {
            this.$confirm("此操作将永久删除角色,删除角色后会影响用户登录平台 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(async () => {
                    const { id } = row;
                    const params = { data: { id } };
                    const res = await deleteRole(params);
                    if (res.code === "Success") {
                        this.$message.success("删除成功");
                        this.getRoleList();
                    } else {
                        this.$message.error(`删除失败，失败原因：${res.message}`);
                    }
                })
                .catch(() => {
                    this.$message({ type: "info", message: "已取消" });
                });
        },
    },
};
</script>

<style scoped lang='less'>
.template-box {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    flex-shrink: 1;
    background: #fff;
    .template-tools {
        padding: 10px;
    }
    .template-table {
        height: calc(100% - 105px);
    }
    .template-pagination {
        text-align: right;
        padding: 10px;
    }
}
</style>
