<template>
    <Scroller ref="scroller">    
        <DeptNode 
            v-for="(item, index) in depts"
            @handleExpand="handleExpand"
            @handleChecked="handleChecked"
            :data="item"
            :key="index"
            :isCustomDept="true"
            :withMembers="withMembers" />
        <Load 
            v-if="!root.deptList"
            :hasData="!!depts.length"
            :hasMore="hasMore"
			:loading="loading" 
			@on-load="handleLoad"/>
        <Empty v-if="!loading && !hasMore && !depts.length"/>
    </Scroller>
</template>
<script>
    /**
     * 组织
     *  */
    import DeptNode from './section-dept-node'
    import Section from '../mixins/section'
    import Empty from '../../../base/empty/'
    import Message from '../../../base/message/'
    import { t } from '../../../../locale/'
    import { deepCopy } from '../../../../utils/assist'

    export default {
        name: 'YYSectionDept',
        mixins: [Section],
        components: {
            DeptNode,
            Empty
        },
        inject: ['root'],
        props: {
            data: {
                type: Array,
                default: function() {
                    return []
                }
            },
            withMembers: {
                type: Boolean,
                default: false
            },
            onLoad: {
                type: Function
            }
        },
        computed: {
            isAllChecked() {
                return this.depts.length && this.depts.every(data => data.checked)
            }
        },
        data() {
            return {
                depts: deepCopy(this.data),
                loading: false,
                hasMore: false
            }
        },
        methods: {
            setDeptProperty(data) {
                if(!data.length) return []
                data.forEach((item) => {
                    item.checked = item.checked || false
                    item.type = 0

                    if(!item.memberId && (item.deptName || item.deptId)) {
                        item.children = []
                        item.expand = !!item.expand
                        item.loading = false
                        item.userNum = 10
                        item.haveSub = !!item.haveSub || true
                        item.type = item.type || 1
                    }

                    if(item.checked) {
                        this.handleChecked(item.checked, item)
                    }
                })
                return deepCopy(data)
            },
            handleExpand(expand, data) {
                if(!this.onLoad) return
                data.expand = expand
                if(data.children && data.children.length) return
                data.loading = true
                this.onLoad({
                    deptId: data.deptId,
                    callback: (res) => {
                        if(res && res.length) {
                            data.children = this.setDeptProperty(res)
                        }
                        data.loading = false
                    }
                })
            },
            handleLoad() {
                if(!this.onLoad) return
                this.loading = true
                this.onLoad({
                    callback: (res, hasMore) => {
                        this.depts = this.setDeptProperty(res)
                        this.loading = false
                        this.hasMore = !!hasMore
                    }
                })
            },
            handleChecked(checked, data) {
                if(data.loading) return false
                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
            },
            handleSelectAll(checked) {
                this.depts.every(data => {
                    return this.handleChecked(checked, data)
                })
            }
        },
        created() {
            this.handleLoad()
        }
    }
</script>
