<template>
    <div class="section-dept-node">
         <MemberNode 
            v-if="data.type === 0"
            :data="data"
            @handleChecked="handleChecked" />
        <TreeNode 
            v-else
            @handleExpand="handleExpand"
            @handleChecked="handleChecked"
            :showExpand="showExpand"
            :showExpandIcon="withMembers ? !!data.userNum : !!data.haveSub"
            :showChecked="!withMembers && enableOrg"
            :showIndeterminate="showIndeterminate"
            :loading="data.loading"
            :expand="data.expand"
            :indeterminate="data.indeterminate"
            :checked="checked">
            <template slot="content">
                {{ (root.useShortName && data.shortName) || data.deptName }}
            </template>
            <template slot="operate">
                <span class="section-dept-node-contain" 
                    v-if="root.enableContainChild && !withMembers && data.haveSub && root.limit > 1" 
                    @click="getDeptChildList(data.deptId, data)">
                    <span>{{data.contain ? t('selectMember.cancelSubordinate') : t('selectMember.includingSubordinate')}}</span>
                </span>
                <span class="section-dept-node-contain" 
                    v-else-if="root.enableContainChildOrg && !withMembers && data.haveSub && root.limit > 1" 
                    @click="getDeptChildList(data.deptId, data, false)">
                    <span>{{data.contain ? t('selectMember.cancelSubordinateOrg') : t('selectMember.includingSubordinateOrg')}}</span>
                </span>
            </template>
            
            <div 
                class="section-dept-children"
                slot="children" 
                v-if="data.children">
                <div :class="{
                        'section-dept-children-item': item.type === 1,
                        'section-dept-children-item-member': item.type === 0,
                    }" 
                    v-for="(item, index) in data.children" :key="index">
                    <YYDeptNode 
                        v-if="item.type === 1"
                        :data="item"
                        :withMembers="withMembers"
                        :isCustomDept="isCustomDept"
                        @handleExpand="handleExpand"
                        @handleChecked="handleChecked"
                        @getDeptChildList="getDeptChildList" />
                    <MemberNode 
                        v-if="item.type === 0"
                        :data="item"
                        @handleChecked="handleChecked" />
                </div>
            </div>
        </TreeNode>
    </div>
</template>
<script>
    /**
     * 组织节点
     *  */
    import MemberNode from './section-member-node'
    import Node from '../mixins/node'
    import Locale from '../../../../mixins/locale'
    export default {
        name: 'YYDeptNode',
        mixins: [Node, Locale],
        components: {
            MemberNode,
        },
        inject: ['root'],
        props: {
            data: {
                type: Object,
                default () {
                    return {}
                }
            },
            withMembers: {
                type: Boolean
            },
            showExpand: {
                type: Boolean,
                default: true
            },
            isCustomDept: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                contain: false
            }
        },
        computed: {
            checked() {
                const checked = !!(this.root.selected.dept['m' + this.data.deptId] || null)
                if (!this.withMembers) this.data.checked = checked
                return checked
            },
            indeterminate() {
                return this.data.indeterminate
            },
            showIndeterminate() {
                if(this.isCustomDept) return !this.isCustomDept
                return !!this.withMembers && !!this.data.userNum && this.root.limit > 1
            },
            enableOrg() { // 组织是否可选
                const enableOrg = this.root.enableOrg
                if(!enableOrg) { // 禁止组织
                    return this.data.orgType === 0
                } 
                return enableOrg
            }
        },
        methods: {
            getDeptChildList(deptId, params, containDept = true) {
                this.$emit('getDeptChildList', deptId, params, containDept)
            }
        },
        mounted() {
        }
    }
</script>
