import apiTools from '@/api'
import JSONPath from 'jsonpath'
export default {
    namespaced: true,
    state: {
        courseList: [],
        currentCourseIndex: 0
    },
    getters: {
        courseInfo(state) {
            return state.courseList[state.currentCourseIndex]
        }
    },
    mutations: {
        setCurrentCourseIndex(state, index) {
            state.currentCourseIndex = index
        },
        setCourseList(state, data) {
            state.courseList = data
        }
    },
    actions: {
        getCourseList(context) {
            return new Promise(
                (resolve, reject) => {
                    if (context.state.courseList.length == 0) {
                        apiTools.courseMgmt.findCourse({
                            scopeCode: context.rootState.userInfo.schoolCode
                        }).then(
                            res => {
                                if (res.error == null) {
                                    if (res.result.data.length > 0) {
                                        context.commit('setCourseList', res.result.data)
                                        resolve({
                                            code: 1,
                                            message: '数据请求成功'
                                        })
                                    } else {
                                        resolve({
                                            code: 2,
                                            message: '请求成功，数据为空'
                                        })
                                    }
                                } else {
                                    reject({
                                        code: 0,
                                        message: '请求失败,API error!'
                                    })
                                }
                            },
                            err => {
                                reject({
                                    code: 0,
                                    message: '请求失败,API error!'
                                })
                            }
                        )
                    } else {
                        resolve({
                            code: 3,
                            message: '数据已请求，无需重复请求'
                        })
                    }
                }
            )
        }
    }
}
