<template>
    <Scroller ref="scroller">
        <GroupNode 
            v-for="(item, index) in list"
            @handleExpand="handleExpand"
            @handleChecked="handleChecked"
            :data="item"
            :key="index"
            :withMembers="withMembers"/>
        <Load 
            :hasData="!!list.length"
            :hasMore="hasMore"
            :loading="loading" 
            @on-load="getGroupListFn"/>
        <Empty v-if="!loading && !hasMore && !list.length"/>
    </Scroller>
</template>
<script>
    /**
     * 内部群
     *  */
    import GroupNode from './section-group-node'
    import Section from '../mixins/section'
    import Empty from '../../../base/empty/'
    import Message from '../../../base/message/'
    import { t } from '../../../../locale/'
    import axios from 'axios'
    import {getGroupList,getGroupMemberList} from '../utils'

    export default {
        name: 'YYSectionGroup',
        mixins: [Section],
        components: {
            GroupNode,
            Empty
        },
        inject: ['root'],
        props: {
            withMembers: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                size: 20
            }
        },
        methods: {
            getGroupListFn(deptId) { // 拉取当前内部群
                this.loading = true
                const { commonSelectMemberHost, qzId, language, token } = this.root
                getGroupList(commonSelectMemberHost,qzId,{
                    keyword: '',
                    page: this.page,
                    size: this.size,
                    language: language,
                    token: token
                }).then(res => {
                    let data = res && res.data || []
                    data.forEach(item => {
                        item.expand = false
                        item.checked = false
                        item.loading = false
                        item.children = []
                    })
                    this.hasMore = !(data.length < this.size)
                    this.page++
                    this.loading = false
                    return this.list = this.list.concat(data)
                }).catch(error => {
                    this.handleError()
                })
            },
            getMemberList(groupId, params, isSelect) { // 拉取当前内部群成员
                params && (params.loading = true)
                const {  commonSelectMemberHost, qzId, language, security, token } = this.root
                getGroupMemberList(commonSelectMemberHost,qzId,groupId,{
                    keyword: '',
                    page: 1,
                    size: 0,
                    language: language,
                    security: security,
                    tmp: new Date().valueOf(),
                    token: token,
                    includeDuty: true
                }).then(res => {
                    let data = res.data || []
                    data.forEach(item => {
                        item.checked = !!isSelect || false
                        if(isSelect) {
                            this.$emit('handleSelected', item)
                        }
                    })
                    params && (params.loading = false)
                    params && (params.children = data)
                }).catch(error => {
                    this.handleError(params)
                })
            },
            handleChecked(checked, data) {
                if(data.loading) return false
                if(!this.withMembers || data.type === 0) {
                    if(checked && 1 + this.root.selectedCount > this.root.limit && this.root.limit != 1) {
                        Message.warning(t('selectMember.maximum'))
                        return false
                    }
                    data.checked = checked
                    this.$emit('handleSelected', data)
                    return true
                } 
                if(checked && data.groupMemberCount + this.root.selectedCount > this.root.limit) {
                    Message.warning(t('selectMember.maximum'))
                    return false
                }
                if(data.children && data.children.length) {
                    data.checked = checked
                    data.children.forEach((item)=>{
                        item.checked = checked
                        this.$emit('handleSelected', item)
                    })
                    return true
                } 
                data.expand = true
                this.getMemberList(data.gid, data, true)
                return true
            },
            handleExpand(expand, data) {
                data.expand = expand
                if(data.children && data.children.length) return
                this.getMemberList(data.gid, data)
            }
        },
        created() {
            this.getGroupListFn()
        }
    }
</script>
