<template>
    <Scroller ref="scroller">
        <Collapse v-for="(data, index) in list"
            :key="index">
            <template slot="title">
                {{data.name}}
            </template>
            <template slot="content">
                <OutNode 
                    v-for="(item, index) in data.children"
                    @handleExpand="handleExpand"
                    @handleChecked="handleChecked"
                    @getMemberList="getMemberList"
                    :data="item"
                    :key="index"
                    :withMembers="withMembers"/>
                <Load 
                    :showNoMoreTip="false"
                    :hasData="!!data.children.length"
                    :hasMore="data.hasMore"
                    :loading="data.loading" 
                    @on-load="getClassList(data.id, data)"/>
            </template>
        </Collapse>
        <Empty v-if="!loading && !list.length"/>
    </Scroller>
</template>
<script>
    /**
     * 外部
     *  */
    import OutNode from './section-out-node'
    import Section from '../mixins/section'
    import Collapse from '../../../base/collapse'
    import Empty from '../../../base/empty/'
    import Message from '../../../base/message/'
    import { t } from '../../../../locale/'
    import axios from 'axios'
    import {getType, getClass, getUsers} from '../utils'
    export default {
        name: 'YYSectionOut',
        mixins: [Section],
        components: {
            OutNode,
            Collapse,
            Empty
        },
        inject: ['root'],
        props: {
            withMembers: {
                type: Boolean,
                default: false
            }
        },
        methods: {
            getTypeList() { // 拉取类型列表
                this.loading = true
                const { commonSelectMemberHost, qzId, outUserTag, token } = this.root
                getType(commonSelectMemberHost,qzId,{
                    tag: outUserTag,
                    token: token
                }).then(res => {
                    let data = res && res.data || []
                    data.forEach(item => {
                        item.children = []
                        item.hasMore = true
                        item.page = 1
                        item.size = 20
                        item.loading = false
                        this.getClassList(item.id, item)
                    })
                    this.loading = false
                    return this.list = this.list.concat(data)
                }).catch(error => {
                    this.handleError()
                })
            },
            getClassList(typeId, params) { // 拉取分类列表
                params.loading = true
                const { commonSelectMemberHost, qzId, outUserTag, token } = this.root
                getClass(commonSelectMemberHost,qzId,{
                    type: typeId,
                    tag: outUserTag,
                    page: params.page,
                    size: params.size,
                    token: token
                }).then(res => {
                    let data = res && res.data || []
                    data.forEach(item => {
                        item.typeId = params.id // 外部类型id
                        item.type = 2
                        item.expand = false
                        item.checked = false
                        item.loading = false
                        item.children = []
                        item.hasMore = true
                        item.page = 1
                        item.size = 20
                    })
                    params.hasMore = !(data.length < params.size)
                    params.page++
                    params.loading = false
                    return params.children = params.children.concat(data)
                }).catch(error => {
                    this.handleError(params)
                })
            },
            getMemberList(classId, params, isSelect) { // 拉取人员列表
                params && (params.loading = true)
                const {  commonSelectMemberHost, qzId, outUserTag, token } = this.root
                getUsers(commonSelectMemberHost, qzId, classId, {
                    keyword: '',
                    type: params.typeId,
                    tag: outUserTag,
                    page: params.page,
                    size: params.size,
                    token: token
                }).then(res => {
                    let data = res && res.data || []
                    data.forEach(item => {
                        item.type = 0
                        item.checked = !!isSelect || false
                        item.userName = item.name
                        item.id = item.id ? item.id : item.memberId
                        if(isSelect) {
                            this.$emit('handleSelected', item)
                        }
                    })
                    params.hasMore = !(data.length < params.size)
                    params.page++
                    params.loading = false
                    return params.children = params.children.concat(data)
                }).catch(error => {
                    this.handleError(params)
                })
            },
            handleExpand(expand, data) {
                data.expand = expand
                if(data.children && data.children.length) return
                this.getMemberList(data.classId, data)
            },
            handleChecked(checked, data) {
                if(data.loading) return
                if(!this.withMembers || data.type === 0) {
                    if(checked && 1 + this.root.selectedCount > this.root.limit && this.root.limit != 1) {
                        return Message.warning(this.t('selectMember.maximum'))
                    }
                    data.checked = checked
                    return this.$emit('handleSelected', data)
                } 
                if(checked && data.count + this.root.selectedCount > this.root.limit) {
                    return Message.warning(this.t('selectMember.maximum'))
                }
                if(data.children && data.children.length) {
                    data.checked = checked
                    return data.children.forEach((item)=>{
                        item.checked = checked
                        this.$emit('handleSelected', item)
                    })
                } 
                data.expand = true
                return this.getMemberList(data.classId, data, true)
            }
        },
        created() {
            this.getTypeList()
        }
    }
</script>
