import Vue from 'vue'
import Vuex from 'vuex'
import courseMgmt from './module/courseMgmt'
import talMgmt from './module/talMgmt'
import authorization from './module/authorization'
import totalAnalysis from './module/totalAnalysis'
import schoolBaseInfo from './module/schoolBaseInfo'
import user from './module/user'
import config from './module/config'
import teachers from './module/teachers'
import studentWeb from './module/studentWeb'
import scboard from './module/scboard'
import serviceDriveAuth from './module/serviceDriveAuth'
import { GLOBAL } from '@/static/Global.js'
import spaceAuth from './module/spaceAuth'
import studentAclassOneAuth from './module/studentAclassOneAuth'
import answerSheet from './module/answerSheet'


Vue.use(Vuex)

// TYPES
const MAIN_SET_COUNTER = 'MAIN_SET_COUNTER'

// MUTATIONS
const mutations = {
    [MAIN_SET_COUNTER](state, obj) {
        state.counter = obj.counter
    },
    setPrivateSas(state, obj) {
        state.privateSas = obj
    },
    setSchoolSas(state, obj) {
        state.schoolSas = obj
    },
    setUserInfo(state, obj) { //obj还是原来的逻辑传参，没有变化。只是这里内部逻辑添加了判断是否为班主任，班主任对应的班级id，以及授课班级id三个字段的处理逻辑
        obj.schoolCode = obj.schoolCode || GLOBAL.DEFAULT_SCHOOL_CODE
        obj.hasSchool = obj.schoolCode !== GLOBAL.DEFAULT_SCHOOL_CODE
        obj.isHeadmaster = false      //默认不是班主任
        obj.mgtClasses = []           //班主任管理的班级id
        obj.teachClasses = []         //授课班级id
        let schoolStr = localStorage.getItem('school_profile')
        if (schoolStr) {
            schoolStr = decodeURIComponent(schoolStr, "utf-8")
            let schoolJson = JSON.parse(schoolStr)
            let mgtClasses = schoolJson.school_classes.filter(item=>{
                return item.teacher.id == obj.TEAMModelId
            })
            if(mgtClasses.length){
                obj.isHeadmaster = true //是班主任
                obj.mgtClasses = mgtClasses.map(item=>{
                    return item.id
                })
            }
            if(schoolJson.school_courses){
                schoolJson.school_courses.forEach(item => {
                    let classIds = item.classes.map(classItem =>{
                        return classItem.id
                    })
                    obj.teachClasses.push(...classIds)
                })
            }
        }
        state.userInfo = obj
    },
    setSchoolCode(state, obj) {
        state.userInfo.schoolCode = obj || GLOBAL.DEFAULT_SCHOOL_CODE
        state.userInfo.hasSchool = obj !== GLOBAL.DEFAULT_SCHOOL_CODE
    }
}

// ACTIONS
const actions = ({
    setCounter({ commit }, obj) {
        commit(MAIN_SET_COUNTER, obj)
    }
})

const getters = {
    radarData: state => state.totalAnalysis.radarData
}

export default new Vuex.Store({
    mutations,
    actions,
    getters,
    state: {
        userInfo: {
            TEAMModelId: '',
            name: '',
            schoolCode: '',
            hasSchool: false
        }
    },
    modules: {
        talMgmt,
        authorization,
        totalAnalysis,
        schoolBaseInfo,
        courseMgmt,
        user,
        config,
        teachers,
        studentWeb,
        scboard,
        serviceDriveAuth,
        spaceAuth,
        studentAclassOneAuth,
		answerSheet
    }
})
