import apiTools from '@/api'
import JSONPath from 'jsonpath'
export default {
    namespaced: true,
    state: {
        // "sokapp":"蘇格拉底議課APP" "remotcls":"遠距教室服務" "sokdesk":"蘇格拉底桌面" "sokrpt":"蘇格拉底報告" "sokvdo":"蘇格拉底影片" "ezs":"ezStation錄播系統"
        aprules: ["sokapp", "remotcls", "sokdesk", "sokrpt", "sokvdo", "ezs"],
        schoolBaseInfo: {
            period: []
        },
        classroomList: undefined,
        periods: [],
        classrooms: [],
        grades: [],
        students: []
    },
    getters: {
        getPeriods: state => state.periods,
        getClassrooms: state => state.classrooms,
        getGrades: state => state.srvAdr,
        getStudent: state => state.students,
        getSchoolInfo: state => state.schoolBaseInfo,
        getAprules: state => state.aprules
    },
    mutations: {
        setSchoolInfo(state, data) {
            state.schoolBaseInfo = data
        },
        setClassroomList(state, data) {
            state.classroomList = data
        },
        setStudents(state, data) {
            state.students = data
        },
        addStudents(state, data) {
            state.students.push(...data)
        },
        uptStudents(state, data) {
            data.forEach(function (item) {
                let objIndex = state.students.findIndex((obj => obj.id == item.id))
                state.students[objIndex] = item
            })
        },
        delStudents(state, data) {
            data.forEach(function (item) {
                let objIndex = state.students.findIndex((obj => obj.id == item));
                state.students.splice(objIndex, 1);
            })
        },
        importStudents(state, data) {
            data.forEach(function (item) {
                let objIndex = state.students.findIndex((obj => obj.id == item.id));
                if (objIndex >= 0) {
                    state.students[objIndex] = item
                } else {
                    state.students.unshift(item)
                }
            })
        }
    },
    actions: {
        setStudentsToState(context, data) {
            context.commit('setStudents', data)
        },
        addStudentsToState(context, data) {
            context.commit('addStudents', data)
        },
        updStudentsToState(context, data) {
            context.commit('uptStudents', data)
        },
        delStudentsToState(context, data) {
            context.commit('delStudents', data)
        },
        importStudentsToState(context, data) {
            context.commit('importStudents', data)
        }
    }
}
