import apiTools from '@/api'
export default {
    namespaced: true,
    state: {
        teacherList: [],
    },
    getters: {

    },
    mutations: {
        setTeacherList(state, data) {
            state.teacherList = data
        }
    },
    actions: {
        getTeacherList(context) {
            return new Promise(
                (resolve, reject) => {
                    if (context.state.teacherList.length == 0) {
                        apiTools.courseMgmt.getSchoolTeacher({
                            'school_code': context.rootState.userInfo.schoolCode
                        }).then(
                            (res) => {
                                if (res) {
                                    context.commit('setTeacherList', res.teachers)
                                    resolve({
                                        data: res.teachers,
                                        message: '数据请求成功'
                                    })
                                } else {
                                    reject({
                                        code: 0,
                                        message: '请求失败,API error!'
                                    })
                                }
                            },
                            (err) => {
                                reject({
                                    code: 0,
                                    message: '请求失败,API error!'
                                })
                            }
                        )
                    } else {
                        resolve({
                            data: context.state.teacherList,
                            message: '数据已请求，无需重复请求'
                        })
                    }
                }
                // (resolve, reject) => {
                //     if (context.state.teacherList.length == 0) {
                //         apiTools.courseMgmt.getSchoolTeacher({
                //             'school_code': context.rootState.userInfo.schoolCode 
                //         }).then(
                //             (res) => {
                //                 if (res) {
                //                     if (res.teachers.length > 0) {
                //                         context.commit('setTeacherList', res.teachers)
                //                         resolve({
                //                             code: 1,
                //                             message: '数据请求成功'
                //                         })
                //                     } else {
                //                         resolve({
                //                             code: 2,
                //                             message: '请求成功，数据为空'
                //                         })
                //                     }
                //                 } else {
                //                     resolve({
                //                         code: 0,
                //                         message: '请求失败,API error!'
                //                     })
                //                 }
                //             },
                //             (err) => {
                //                 resolve({
                //                     code: 0,
                //                     message: '请求失败,API error!'
                //                 })
                //             }
                //         )
                //     } else {
                //         resolve({
                //             code: 3,
                //             message: '数据已请求，无需重复请求'
                //         })
                //     }
                // }
            )
        }
    }
}
