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.push(item)
                }
            })
        }
    },
    actions: {
        getSchoolBaseData(context) {
            throw new Error("getSchoolBaseData方法已经弃用，请从schoolProfile读取基础数据")
            return 
            return new Promise(
                (resolve, reject) => {
                    if (context.state.schoolBaseInfo.period.length == 0) {
                        apiTools.schoolSetting.findSchoolSystem({
                            school_code: context.rootState.userInfo.schoolCode
                        }).then(
                            res => {
                                if (res) {
                                    console.log(res)
                                    if (res.schools.length > 0) {
                                        context.commit('setSchoolInfo', res.schools[0])
                                        resolve({
                                            code: 1,
                                            data: res.schools[0],
                                            message: '数据请求成功'
                                        })
                                    } else {
                                        resolve({
                                            code: 2,
                                            data: [],
                                            message: '请求成功，数据为空'
                                        })
                                    }
                                } else {
                                    reject({
                                        code: 0,
                                        data: [],
                                        message: '请求失败,API error!'
                                    })
                                }
                            },
                            err => {
                                reject({
                                    code: 0,
                                    data: [],
                                    message: '请求失败,API error!'
                                })
                            }
                        )
                    } else {
                        resolve({
                            code: 3,
                            data: context.state.schoolBaseInfo,
                            message: '数据已请求，无需重复请求'
                        })
                    }
                }
            )
        },
        getClassroom(context) {
            throw new Error("getClassroom方法已经弃用，请从schoolProfile读取班级数据")
            return 
            return new Promise(
                (resolve, reject) => {
                    if (!context.state.classroomList) {
                        apiTools.schoolSetting.findClassInfo({
                            'school_code': context.rootState.userInfo.schoolCode
                        }).then(
                            res => {
                                if (!res) {
                                    resolve({
                                        code: 0,
                                        message: '请求失败,API error!'
                                    })
                                } 
                                if (res.classrooms.length == 0) {
                                    resolve({
                                        code: 2,
                                        data:[],
                                        message: '请求成功，数据为空'
                                    })
                                }
                                let resData = res.classrooms
                                context.dispatch('getSchoolBaseData').then(
                                    (res) => {
                                        if (res.code == 1 || res.code == 3) {
                                            for (let index in resData) {
                                                let currentSchoolInfo = JSONPath.query(context.state.schoolBaseInfo, "$..period[?(@.periodCode=='" + resData[index].periodCode + "')]")
                                                if (currentSchoolInfo.length > 0) {
                                                    resData[index].periodName = currentSchoolInfo[0].periodName
                                                    resData[index].gradeName = JSONPath.query(context.state.schoolBaseInfo, "$..grades[?(@.gradeCode=='" + resData[index].gradeCode + "')]")[0].gradeName
                                                }
                                            }
                                            context.commit('setClassroomList', resData)
                                            resolve({
                                                code: 1,
                                                data: resData,
                                                message: '数据请求成功'
                                            })
                                        } else {
                                            console.log(res)
                                        }
                                    },
                                    (err) => {
                                        console.log(err)
                                    }
                                )
                            },
                            err => {
                                resolve({
                                    code: 0,
                                    message: '请求失败,API error!'
                                })
                            }
                        )
                    } else {
                        resolve({
                            code: 3,
                            data: context.state.classroomList,
                            message: '数据已请求，无需重复请求'
                        })
                    }
                }
            )
        },
        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)
        }
    }
}
