<template>
    <div class="user">
        <Card :bordered="false" dis-hover>
            <bingo-list
                :columns="columns"
                :pageSetting="pageSetting"
                :page="true"
                :isAdvanceTable="false"
                :showBtnType="'all'"
                :showBtnOutNum="2"
                :toolbarNew="true"
                ref="bingoList"
                :searchList="searchList"
                :dataApi="Api"
                :queryData="queryData"
                :isSearch="true"
                :isSearchAdvance="true"
                :showSwitchBtn="false"
                :processData="processData"
                @on-query-submit="querySubmit"
                @on-add-new="onAddNew"
                @on-edit-btn="onEditBtn"
                :showNewBtn="auth.add!==0"
                :isEditBtn="auth.edit!==0"
                :isDeleteBtn="auth.delete!==0"
                v-if="auth.table!==0"
            >
                <template slot="action" slot-scope="{ row, index }">
                    <Button
                        class="btn-more"
                        size="small"
                        @click="allotUser(row)"
                        v-if="auth.allotUser!==0"
                    >
                    <i class="icon iconfont iconyonghuguanli"></i>
                    <span class="font">分配用户</span></Button
                    >
                    <Button
                        class="btn-more"
                        size="small"
                        @click="allotRole(row)"
                        v-if="auth.allotRole!==0"
                    >
                    <i class="icon iconfont iconjiaoseguanli"></i>
                    <span class="font">分配角色</span></Button
                    >
                </template>
            <!-- 高级搜索 -->
                <template slot="queryForm">
                    <bingo-grid-item>
                        <bingo-form-item label="群组名称" prop="nameCn">
                            <Input
                                v-model="queryData.nameCn"
                                placeholder="请输入群组名称"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="评论" prop="remark">
                            <Input
                                v-model="queryData.remark"
                                placeholder="请输入评论"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                </template>
            </bingo-list>
        </Card>
        <Modal v-model="isShowNewForm" :title="isAdd==true?'新建':'修改'" @on-ok="onOk" :width="900">
            <Form ref="formAdd" :model="formAdd" label-position="right" :label-width="90">
                <Row>
                    <Col :span="12">
                        <bingo-form-item label="群组名称">
                            <Input type="text" v-model="formAdd.nameCn" placeholder="请输入群组名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="德文名称">
                            <Input type="text" v-model="formAdd.nameDe" placeholder="请输入德文名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="英文名称">
                            <Input type="text" v-model="formAdd.nameEn" placeholder="请输入英文名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="评论">
                            <Input type="text" v-model="formAdd.remark" placeholder="请输入评论">
                            </Input>
                        </bingo-form-item>
                    </Col>
                </Row>
            </Form>
        </Modal>
        <bingo-modal-select
        ref="modalUser"
        :title="'分配用户'"
        :datas="datasUser"
        :show-input="false"
        :columns="selectColumnsUser"
        :field="'nameCn'"
        :value="selectedUsers"
        :keyId="'sn'"
        @on-ok="onSelectOkUser"
        @getData="getDataUser">
                    <bingo-grid-item>
                        <FormItem label="用户名称" prop="nameCn">
                            <Input v-model="userArr.nameCn" placeholder=""/>
                        </FormItem>
                    </bingo-grid-item>
        </bingo-modal-select>
        <bingo-modal-select
        ref="modal"
        :title="'分配角色'"
        :datas="datas"
        :show-input="false"
        :columns="selectColumns"
        :field="'nameCn'"
        :value="selectedRoles"
        :keyId="'sn'"
        @on-ok="onSelectOk"
        @getData="getData">
                    <bingo-grid-item>
                        <FormItem label="角色名称" prop="nameCn">
                            <Input v-model="roleArr.nameCn" placeholder=""/>
                        </FormItem>
                    </bingo-grid-item>
        </bingo-modal-select>
    </div>
</template>
<script>
    import bingoList from '@/components/bingo-list';
    import Api from '@/api/system/group';
    import userApi from '@/api/system/user';
    import roleApi from '@/api/system/role'
    import rbacApi from '@/api/system/rbac'
    import module from '@/mixins/module';
    import bingoModalSelect from '@/components/bingo-modal-select'
    import util from '@/utils/tool'
    export default {
        name:'plat-group',
        data () {
            return {
                columns: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        title: '群组名称',
                        key: 'nameCn'
                    },
                    {
                        title: '评论',
                        key: 'remark'
                    },
                    {
                        title: '创建人',
                        key: 'creator'
                    },
                    {
                        title: '创建时间',
                        key: 'createTime'
                    },
                    {
                        title: '修改人',
                        key: 'modifier'
                    },
                    {
                        title: '修改时间',
                        key: 'modifyTime'
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        align: 'center',
                        width: 200
                    }
                ],
                Api: Api,
                pageSetting: {
                    pageNum: 1,
                    pageSize: 20,
                    showSizer: true
                },
                // 搜索的字段
                searchList: [
                    {
                        label: '群组名称',
                        value: 'nameCn',
                        type: 'input'
                    },
                    {
                        label: '评论',
                        value: 'remark',
                        type: 'input'
                    }
                ],
                tableData: [],
                queryData: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    remark: '',
                    sn: ''
                },
                queryObj: {},
                isShowNewForm: false,
                formAdd: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    remark: '',
                    sn: ''
                },
                grid: {
                    xl: 12,
                    lg: 13,
                    md: 13,
                    sm: 24,
                    xs: 24
                },
                isAdd: true,
                datas: [],
                datasUser: [],
                selectColumns: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        title: '角色名称',
                        key: 'nameCn'
                    }
                ],
                selectColumnsUser: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        title: '用户名称',
                        key: 'nameCn'
                    },
                    {
                        title: '账号',
                        key: 'account'
                    }
                ],
                roleArr: {
                    nameCn: ''
                },
                userArr: {
                    nameCn: '',
                    accoutn: ''
                },
                row: {},
                selectedRoles: [],
                selectedUsers: []
            };
        },
        created () {
            // this.getAuth();
            this.auth = JSON.parse(localStorage.getItem('auth'));
        },
        mixins: [module],
        methods: {
            // 获取权限
            // async getAuth () {
            //     let auth = await this.getPermission();
            //     this.auth = JSON.parse(JSON.stringfy(auth));
            // },
            // 弹出层查询
            querySubmit (obj) {
                if (obj) {
                    this.queryObj = obj;
                }
                this.$refs.bingoList.getTableData(this.queryObj);
            },
            // 分配用户
            allotUser (row) {
                this.row = row;
                let data = { sn: row.sn }
                userApi.getUsersByGroup(data).then(res => {
                    if (res.rows.length > 0) {
                        this.selectedUsers = res.rows.filter(item => item.isCheck === 1);
                    }
                    this.getDataUser();
                })
            },
            // 分配角色
            allotRole (row) {
                this.row = row;
                let data = { sn: row.sn }
                roleApi.getRolesByGroup(data).then(res => {
                    if (res.rows.length > 0) {
                        this.selectedRoles = res.rows.filter(item => item.isCheck === 1);
                    }
                    this.getData();
                })
            },
            // 获取用户列表
            getDataUser () {
                let data = this.userArr;
                userApi.tableList(data).then(res => {
                    this.datasUser = res.rows;
                    this.$refs.modalUser.show = true;
                })
            },
            // 获取角色列表
            getData () {
                let data = this.roleArr;
                roleApi.tableList(data).then(res => {
                    this.datas = res.rows;
                    this.$refs.modal.show = true;
                })
            },
            // 选角色
            onSelectOk (areaData) {
                let roleSns = [];
                if (areaData.length > 0) {
                    roleSns = areaData.map(item => item.sn);
                }
                let data = {
                    roleSns: roleSns,
                    sn: this.row.sn
                }
                rbacApi.groupRoles(data).then(res => {
                    this.$Message.success('分配角色成功');
                })
            },
            // 选用户
            onSelectOkUser (areaData) {
                let userSns = [];
                if (areaData.length > 0) {
                    userSns = areaData.map(item => item.sn);
                }
                let data = {
                    userSns: userSns,
                    sn: this.row.sn
                }
                rbacApi.groupUsers(data).then(res => {
                    this.$Message.success('分配用户成功');
                })
            },
            // 新建
            async onAddNew () {
                this.isAdd = true;
                for (let i in this.formAdd) {
                    if (Array.isArray(this.formAdd[i])) {
                        this.formAdd[i] = [];
                    } else {
                        this.formAdd[i] = '';
                    }
                }
                this.formAdd.sn = await this.getSn();
                this.isShowNewForm = true;
            },
            // 新建确定
            onOk () {
                if (this.isAdd) {
                    Api.addTable(this.formAdd).then(res => {
                        this.$Message.success('新建成功');
                        this.$refs.bingoList.getTableData();
                    })
                } else {
                    Api.editTable(this.formAdd).then(res => {
                        this.$Message.success('修改成功');
                        this.$refs.bingoList.getTableData();
                    })
                }
            },
            // 编辑
            onEditBtn (id) {
                this.isAdd = false;
                Api.getTableById(id).then(res => {
                    this.formAdd = res;
                    this.isShowNewForm = true;
                })
            },
            // 处理数据
            processData (data) {
                data.forEach(item => {
                    if (item.createTime) {
                        item.createTime = util.format(new Date(item.createTime), 'yyyy-MM-dd');
                    }
                    if (item.modifyTime) {
                        item.modifyTime = util.format(new Date(item.modifyTime), 'yyyy-MM-dd')
                    }
                });
            }
        },
        components: { bingoList, bingoModalSelect }
    };
</script>
