<template>
    <div class="system-role-authrole">
        <el-card v-show="showSearch" class="search-card">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="search-form">
                <el-form-item prop="userName">
                    <el-input v-model="queryParams.userName" :placeholder="$t('role.auth-user.876234-0')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item prop="phonenumber">
                    <el-input v-model="queryParams.phonenumber" :placeholder="$t('user.index.098976-4')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <div style="float: right">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('search') }}</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('reset') }}</el-button>
                </div>
            </el-form>
        </el-card>

        <el-card>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="small" @click="openSelectUser" v-hasPermi="['system:role:add']">{{ $t('role.auth-user.876234-1') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-circle-close" size="small" :disabled="multiple" @click="cancelAuthUserAll" v-hasPermi="['system:role:remove']">
                        {{ $t('role.auth-user.876234-2') }}
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-close" size="small" @click="handleClose">{{ $t('close') }}</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" :border="false">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column :label="$t('user.profile.index.894502-1')" prop="userName" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('user.index.098976-11')" prop="nickName" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('user.index.098976-19')" prop="email" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('user.index.098976-3')" prop="phonenumber" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('status')" align="center" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('creatTime')" align="center" prop="createTime" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('opation')" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-circle-close" @click="cancelAuthUser(scope.row)" v-hasPermi="['system:role:remove']">{{ $t('role.auth-user.876234-3') }}</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            <select-user ref="select" :roleId="queryParams.roleId" @ok="handleQuery" />
        </el-card>
    </div>
</template>

<script>
import { allocatedUserList, authUserCancel, authUserCancelAll } from '@/api/system/role';
import selectUser from './selectUser';

export default {
    name: 'AuthUser',
    dicts: ['sys_normal_disable'],
    components: { selectUser },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中用户组
            userIds: [],
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户表格数据
            userList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                roleId: undefined,
                userName: undefined,
                phonenumber: undefined,
            },
        };
    },
    created() {
        const roleId = this.$route.params && this.$route.params.roleId;
        if (roleId) {
            this.queryParams.roleId = roleId;
            this.getList();
        }
    },
    methods: {
        /** 查询授权用户列表 */
        getList() {
            this.loading = true;
            allocatedUserList(this.queryParams).then((response) => {
                this.userList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 返回按钮
        handleClose() {
            const obj = { path: '/enterprise/role' };
            this.$tab.closeOpenPage(obj);
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.userIds = selection.map((item) => item.userId);
            this.multiple = !selection.length;
        },
        /** 打开授权用户表弹窗 */
        openSelectUser() {
            this.$refs.select.show();
        },
        /** 取消授权按钮操作 */
        cancelAuthUser(row) {
            const roleId = this.queryParams.roleId;
            this.$modal
                .confirm(this.$t('role.auth-user.876234-4') + row.userName + this.$t('role.index.094567-37'))
                .then(function () {
                    return authUserCancel({ userId: row.userId, roleId: roleId });
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('role.auth-user.876234-5'));
                })
                .catch(() => {});
        },
        /** 批量取消授权按钮操作 */
        cancelAuthUserAll(row) {
            const roleId = this.queryParams.roleId;
            const userIds = this.userIds.join(',');
            this.$modal
                .confirm(this.$t('role.auth-user.876234-6'))
                .then(function () {
                    return authUserCancelAll({ roleId: roleId, userIds: userIds });
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('role.auth-user.876234-5'));
                })
                .catch(() => {});
        },
    },
};
</script>
<style lang="scss" scoped>
.system-role-authrole {
    padding: 20px;
}
// ----------- 搜索栏 -----------
.search-card {
    margin-bottom: 15px;
    height: 15%;
    width: 100%;
}
.search-form {
    margin-bottom: -18px;
    height: 10%;
}
</style>
