import {
    EducationCategoryGetList,
    DisciplineGetList,
    GetQuestionTypeList,
    GetDifficulty,
    GradeGetList
} from '@/api/api'
import { groupArr } from '@/utils/tools'
const baseInfo = {
    state: {
        LearningSection: [],//所有学段
        LearningArr: [],//学段分组
        Subjects: [],//学科
        SubjectsGroup: {},//学科庚戌学段分组
        SubjectsArr: [],
        QuestionType: [],//试题类型
        Difficulty: [],//难度
        Grades:[], //年级
    },
    getters: {},
    mutations: {
        setLearningSection(state, val) {
            state.LearningSection = val;
        },
        setSubjects(state, val) {
            state.Subjects = val;
        },
        setSubjectsGroup(state, val) {
            state.SubjectsGroup = val;
        },
        setLearningArr(state, val) {
            state.LearningArr = val;
        },
        setSubjectsArr(state, val) {
            state.SubjectsArr = val;
        },
        setQuestionType(state, val) {
            state.QuestionType = val;
        },
        setDifficulty(state, val) {
            state.Difficulty = val;
        },
        setGrades(state, val) {
            state.Grades = val;
        },
    },
    actions: {
        EducationCategoryGetList(context, params = false) {
            if (!params && context.state.LearningSection.length > 0) { return; }
            let data = {
                IsActive: true,
                KeyWord: '',
                SkipCount: 0,
                MaxResultCount: 99999,
            }
            EducationCategoryGetList(data).then(res => {
                //console.log(res, 'vuex 学段列表')
                context.commit('setLearningSection', res.items)
            })
        },
        GradeGetList(context, params = false) {
            if (!params && context.state.Grades.length > 0) { return; }
            let data = {
                KeyWord: '',
                SkipCount: 0,
                MaxResultCount: 99999,
            }
            GradeGetList(data).then(res => {
                //console.log(res, 'vuex 年级列表')
                context.commit('setGrades', res.items)
            })
        },
        DisciplineGetList(context, params = false) {
            if (!params && context.state.Subjects.length > 0) { return; }
            let data = {
                EducationCategoryId: '',
                KeyWord: '',
                SkipCount: 0,
                MaxResultCount: 99999,
            }
            return new Promise((resolve, reject) => {
                DisciplineGetList(data).then(res => {
                    //console.log(res, 'vuex 学科')
                    context.commit('setSubjects', res.items)
                    let obj = {}
                    res.items.forEach(item => {
                        if (!obj[item.gradeName]) { // devil educationCategory
                            obj[item.gradeName] = []
                        }
                        obj[item.gradeName].push(item)
                    })
                    //console.log(obj, 'obj')
                    let arr = []
                    for (var key in obj) {
                        arr.push({
                            title: key,
                            data: obj[key]
                        })
                    }
                    //console.log('SubjectsArr', arr)
                    context.commit('setSubjectsGroup', obj)
                    context.commit('setSubjectsArr', arr)
                    context.commit('setLearningArr', groupArr(res.items, 'gradeId', 'gradeName'))
                    resolve(arr)
                })
            })
        },
        GetQuestionTypeList(context, params = false) {
            if (!params && context.state.QuestionType.length > 0) { return; }
            GetQuestionTypeList().then(res => {
                //console.log(res, 'vuex 试题类型列表')
                context.commit('setQuestionType', res)
            })
        },
        GetDifficulty(context, params = false) {
            if (!params && context.state.Difficulty.length > 0) { return; }
            return new Promise((resolve, reject) => {
                GetDifficulty().then(res => {
                    //console.log(res, 'vuex 难度列表')
                    context.commit('setDifficulty', res)
                    resolve(res)
                })
            })

        },
    },
}
export default baseInfo