<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
                    >
                </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="parentId">
                            <Select v-model="formAdd.parentId" transfer>
                                <Option :value="item.id" v-for="item in orgList" :key="item.id">{{item.nameCn}}</Option>
                            </Select>
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="部门类型" prop="deptType">
                            <Select v-model="formAdd.deptType">
                                <Option v-for="item in deptTypeList" :value="item.dictCode" :key="item.dictCode">{{ item.dictValue }}</Option>
                            </Select>
                        </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="归属机构">
                            <Select v-model="formAdd.parentId" transfer>
                                <Option :value="item.id" v-for="item in orgList" :key="item.id">{{item.nameCn}}</Option>
                            </Select>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="部门类型">
                            <Select v-model="formAdd.deptType">
                                <Option v-for="item in deptTypeList" :value="item.dictCode" :key="item.dictCode">{{ item.dictValue }}</Option>
                            </Select>
                        </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-modal-select>
    </div>
</template>
<script>
    import bingoList from '@/components/bingo-list';
    import Api from '@/api/system/department';
    import orgApi from '@/api/system/organization'
    import userApi from '@/api/system/user'
    import rbacApi from '@/api/system/rbac'
    import dictApi from '@/api/platform/dict';
    import module from '@/mixins/module';
    import util from '@/utils/tool'
    import bingoModalSelect from '@/components/bingo-modal-select'
    export default {
        name:'plat-dept',
        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: 'deptType'
                    },
                    {
                        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: 'deptType',
                        type: 'input'
                    }
                ],
                tableData: [],
                queryData: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    deptType: '',
                    parentId: '',
                    sn: ''
                },
                queryObj: {},
                isShowNewForm: false,
                formAdd: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    deptType: '',
                    parentId: '',
                    sn: ''
                },
                grid: {
                    xl: 12,
                    lg: 13,
                    md: 13,
                    sm: 24,
                    xs: 24
                },
                isAdd: true,
                orgList: [],
                datasUser: [],
                selectColumnsUser: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        title: '用户名称',
                        key: 'nameCn'
                    },
                    {
                        title: '账号',
                        key: 'account'
                    }
                ],
                userArr: {
                    nameCn: '',
                    accoutn: ''
                },
                selectedUsers: [],
                row: {},
                deptTypeList: []
            };
        },
        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));
            // },
            // 部门类型
            getTypeList () {
                let data = {
                    dictGroup: 'platform',
                    dictType: 'dept_type'
                }
                return new Promise(function (resolve, reject) {
                    dictApi.tableList(data).then(res => {
                        resolve(res.rows);
                    })
                })
            },
            // 机构列表
            getOrgList () {
                return new Promise(function (resolve, reject) {
                    orgApi.tableList(data).then(res => {
                        resolve(res.rows);
                    })
                })
            },
            // 弹出层查询
            querySubmit (obj) {
                if (obj) {
                    this.queryObj = obj;
                }
                this.$refs.bingoList.getTableData(this.queryObj);
            },
            // 分配用户
            allotUser (row) {
                this.row = row;
                let data = { sn: row.sn }
                userApi.getUsersByDept(data).then(res => {
                    if (res.rows.length > 0) {
                        this.selectedUsers = res.rows.filter(item => item.isCheck === 1);
                    }
                    this.getDataUser();
                })
            },
            // 获取用户列表
            getDataUser () {
                let data = this.userArr;
                userApi.tableList(data).then(res => {
                    this.datasUser = res.rows;
                    this.$refs.modalUser.show = true;
                })
            },
            // 选用户
            onSelectOkUser (areaData) {
                let userSns = [];
                if (areaData.length > 0) {
                    userSns = areaData.map(item => item.sn);
                }
                let data = {
                    userSns: userSns,
                    sn: this.row.sn
                }
                rbacApi.deptUsers(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();
                if (this.orgList.length === 0) {
                    this.orgList = await this.getOrgList();
                }
                if (this.deptTypeList.length === 0) {
                    this.deptTypeList = await this.getTypeList();
                }
                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();
                    })
                }
            },
            // 编辑
            async onEditBtn (id) {
                this.isAdd = false;
                if (this.orgList.length === 0) {
                    this.orgList = await this.getOrgList();
                }
                if (this.deptTypeList.length === 0) {
                    this.deptTypeList = await this.getTypeList();
                }
                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>
