<template>
    <Scroller ref="scroller">
        <Collapse v-if="publicList && publicList.length">
            <template slot="title">
                {{t('selectMember.publicTeam')}}
            </template>
            <template slot="content">
                <TeamNode 
                    v-for="(item, index) in publicList"
                    @handleExpand="handleExpand"
                    @handleChecked="handleChecked"
                    :data="item"
                    :key="index"
                    :withMembers="withMembers"/>
            </template>
        </Collapse>
        <Collapse v-if="privateList && privateList.length">
            <template slot="title">
                {{t('selectMember.privateTeam')}}
            </template>
            <template slot="content">
                <TeamNode 
                    v-for="(item, index) in privateList"
                    @handleExpand="handleExpand"
                    @handleChecked="handleChecked"
                    :data="item"
                    :key="index"
                    :withMembers="withMembers"/>
            </template>
        </Collapse>
        <Load 
            :hasData="!!privateList.length && !!publicList.length"
            :hasMore="hasMore"
            :loading="loading" 
            @on-load="getTeamList"/>
        <Empty v-if="!loading && !hasMore && !privateList.length && !publicList.length"/>
    </Scroller>
</template>
<script>
    /**
     * 内部群
     *  */
    import TeamNode from './section-team-node'
    import Section from '../mixins/section'
    import axios from 'axios'
    import Empty from '../../../base/empty/'
    import Collapse from '../../../base/collapse'
    import Message from '../../../base/message/'
    import Locale from '../../../../mixins/locale'

    export default {
        name: 'YYSectionTeam',
        mixins: [Section, Locale],
        components: {
            TeamNode,
            Collapse,
            Empty
        },
        inject: ['root'],
        props: {
            withMembers: {
                type: Boolean,
                default: false
            }
        },
        computed: {
            isAllChecked() {
                if (this.publicList.length && this.privateList.length) {
                    return this.publicList.every(data => data.checked) && this.privateList.every(data => data.checked)
                } else if (this.publicList.length) return this.publicList.every(data => data.checked);
                else if (this.privateList.length) return this.privateList.every(data => data.checked);
                return false
            },
        },
        data() {
            return {
                publicList: [],
                privateList: []
            }
        },
        methods: {
            getTeamList(deptId) { // 拉取当前组
                this.loading = true
                const { host, qzId, language, token, withSecurity } = this.root 
                axios.get(`${host}/cooperation/rest/v1/candidate/${qzId}/team/list`, {
                    params: {
                        language: language,
                        token: token,
                        withSecurity: withSecurity
                    },
                    withCredentials: true
                }).then(res => {
                    let data = res && res.data || []
                    data.forEach(item => {
                        item.type = 4
                        item.expand = false
                        item.checked = false
                        item.loading = false
                        item.children = []
                        if(item.teamType === 'PUBLIC') {
                            this.publicList.push(item)
                        }
                        if(item.teamType === 'PERSONAL') {
                            this.privateList.push(item)
                        }
                    })
                    this.hasMore = false
                    this.page++
                    this.loading = false
                }).catch(res => {
                    this.handleError()
                })
            },
            getMemberList(teamId, params, isSelect) { // 拉取当前组内成员
                if (!params.count) return;
                const { host, qzId, language, security, token } = this.root
                params && (params.loading = true)
                axios(`${host}/cooperation/rest/v1/candidate/${qzId}/team/${teamId}/members`, {
                    params: {
                        keyword: '',
                        page: 1,
                        size: 300,
                        language: language,
                        withSecurity: security,
                        tmp: new Date().valueOf(),
                        token: token,
                        includeDuty: true
                    },
                    withCredentials: true
                }).then(res => {
                    let data = res && res.data || []
                    data.forEach(item => {
                        item.checked = !!isSelect || false
                        item.type = 0
                        item.isIssueGroup = true
                        if (isSelect) {
                            this.$emit('handleSelected', item)
                        }
                    })
                    params && (params.loading = false)
                    params && (params.children = data)
                }).catch(res => {
                    this.handleError()
                })
            },
            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(this.t('selectMember.maximum'))
                        return false
                    }
                    data.checked = checked
                    this.$emit('handleSelected', data)
                    return true
                } 
                if(checked && data.count + this.root.selectedCount > this.root.limit) {
                    Message.warning(this.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.id, data, true)
                return true
            },
            handleExpand(expand, data) {
                data.expand = expand
                if(data.children && data.children.length) return
                this.getMemberList(data.id, data)
            },
            handleSelectAll(checked) {
                return this.publicList.every(data => this.handleChecked(checked, data)) && this.privateList.every(data => this.handleChecked(checked, data));
            }
        },
        created() {
            this.getTeamList()
        }
    }
</script>
