<template>
    <div class="tree_wrapper scrollbar">
        <el-tree
            :data="classData"
            :props="defaultProps"
            node-key="id"
            :default-expanded-keys="['']"
            :current-node-key="nodeChecked"
            :expand-on-click-node="false"
            @node-click="handleNodeClick">
        </el-tree>
    </div>
</template>

<script>
import classSelectorMixin from './classSelectorMixin'
export default {
    name: 'ClassSelector',
    mixins: [classSelectorMixin],
    data () {
        return {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            classData: [],
            nodeChecked: ''
        }
    },
    watch: {
        // defaultChecked: {
        //     deep: true,
        //     handler (data) {
        //         if (data && Object.keys(data).length) {
        //             this.nodeChecked = data.id
        //         }
        //     }
        // }
    },
    created () {
        this.getGradeClassList()
    },
    methods: {
        // 获取班级年级
        getGradeClassList () {
            let { isSelectAll, isResponseTree, roleCode } = this
            this.$api.commonApi.getCurrentUserClassInfo({
                isSelectAll,
                isResponseTree,
                roleCode
            }).then(res => {
                let data = res.data
                if (data.Status) {
                    let gradeClassList = data.Data.map(item => {
                        let obj = {}
                        obj.category = 'grade'
                        obj.id = item.GradeId
                        obj.name = item.GradeName
                        obj.children = item.ClassInfos.map(item2 => {
                            let child = {}
                            child.category = 'class'
                            child.id = item2.ClassId
                            child.name = item2.ClassName
                            child.classCode = item2.ClassCode
                            child.gradeNo = item2.GradeNo
                            child.gradeId = item2.GradeId
                            child.gradeName = item2.GradeName
                            child.headTeacherId = item2.HeadTeacherId
                            child.headTeacherName = item2.HeadTeacherName
                            return child
                        })
                        return obj
                    })
                    this.classData = [{ category: 'all', id: '', name: '全校', children: gradeClassList }]
                }
                this.$emit('hasGetGroup', this.classData) // 已获取班级架构
            }).catch(err => {
                console.log(err)
            })
        },
        // 点击节点
        handleNodeClick (data) {
            let { category } = data
            let { nodeClickAll, nodeClickGrade } = this
            if (!nodeClickAll && category === 'all') {
                return false
            }
            if (!nodeClickGrade && category === 'grade') {
                return false
            }
            this.$emit('nodeClick', data)
        }
    }
}
</script>

<style lang="less" scoped>
.tree_wrapper {
    height: calc(100% - 35px);
    overflow-y: auto;
}
</style>
