<template>
    <div class="section-team-node">
        <TreeNode 
            @handleExpand="handleAllExpand"
            @handleChecked="handleAllChecked"
            :showExpand="withMembers ? (!!data.count || (root.enableTeamDept && !!data.haveSub)) : false"
            :showExpandIcon="withMembers ? (!!data.count || (root.enableTeamDept && !!data.haveSub)) : false"
            :showChecked="withMembers ? (!!data.count || (root.enableTeamDept && !!data.haveSub)) && root.limit > 1 : true"
            :loading="data.loading"
            :expand="data.expand"
            :checked="checked">
            <template slot="content">{{data.teamName}}</template>
            <template 
                slot="children" 
                v-if="data.children">
                <template v-if="root.enableTeamDept && data.haveSub">
                    <WithDeptNode ref="deptAndMemberNode" @member-checked="handleMemberChecked" :data="data"/>
                </template>
                <template v-else>
                    <div v-for="(item, index) in data.children" :key="index">
                        <MemberNode 
                            :data="item"
                            :parent="data"
                            @handleChecked="handleChecked" />
                    </div>
                </template>
            </template>
        </TreeNode>
    </div>
</template>
<script>
    /**
     * 内部群节点
     *  */
    import WithDeptNode from './section-team-node-dept';
    import Collapse from '../../../base/collapse'
    import MemberNode from './section-member-node'
    import Locale from '../../../../mixins/locale'
    import Node from '../mixins/node'
    export default {
        name: 'YYTeamNode',
        mixins: [Node, Locale],
        components: {
            MemberNode,
            Collapse,
            WithDeptNode
        },
        inject: ['root'],
        props: {
            data: {
                type: Object,
                default () {
                    return {}
                }
            },
            withMembers: {
                type: Boolean
            }
        },
        computed: {
            checked() {
                if(this.withMembers) {
                    const memberChecked = !!(this.data.children && this.data.children.length &&  this.data.children.every(item => item.checked));
                    if (this.root.enableTeamDept && this.data.haveSub) {
                        const deptChecked = this.data.deptList && this.data.deptList.length && this.data.deptList.every(item => item.checked);
                        return this.data.count ? (memberChecked && deptChecked) : deptChecked;
                    }
                    return memberChecked
                }
                const data = this.root.selected.team['m' + this.data.id] || null
                return !!data
            }
        },
        methods: {
            handleMemberChecked(checked) {
                this.$emit('handleChecked', checked, this.data);
            },
            handleAllChecked(checked) {
                this.$emit('handleChecked', checked, this.data);
                if (this.root.enableTeamDept && this.data.haveSub) {
                    this.$refs.deptAndMemberNode.handleDeptChecked(checked);
                }
            },
            handleAllExpand(expand) {
                if (this.root.enableTeamDept && this.data.haveSub) {
                    this.$refs.deptAndMemberNode.handleDeptExpand(expand);
                }
                this.handleExpand(expand, this.data);
            }
        }
    }
</script>

