import ajax from '../../util/ajax'
import api from '../../util/api'

export default {
  namespaced: true,
  state: {
    detailList: [],
    detailInfo: {},
    scheduleList: [],
  },
  mutations: {
    updateList (state, payload) {
      state.detailList = payload || []
    },
    updatesSheduleList (state, payload) {
      state.scheduleList = payload || []
    },
    updateInfo (state, payload) {
      state.detailInfo = payload || {}
    },
  },
  actions: {
    GetDetailList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.course.detail.url, {
          method: api.course.detail.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('updateList', res)
          resolve(res)
        }).catch(reject)
      })
    },
    GetScheduleList ({ commit }, payload) {
      ajax(api.course.schedule.url, {
        method: api.course.schedule.method,
        queryData: {
          ...payload,
        }
      }).then(res => {
        commit('updatesSheduleList', res)
      })
    },
    GetDetailInfo ({ commit }, { id }) {
      ajax(api.course.info.url, {
        method: api.course.info.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        commit('updateInfo', res)
      })
    },
    AddDetail ({ commit }, payload) {
      ajax(api.course.addDetail.url, {
        method: api.course.addDetail.method,
        data: {
          ...payload
        }
      }).then(res => {
        if (payload.callback) {
          payload.callback()
        }
      })
    },
    UpdateDetail ({ commit }, payload) {
      ajax(api.course.updateDetail.url, {
        method: api.course.updateDetail.method,
        data: {
          ...payload
        },
        urlData: {
          id: payload.DetailId,
        }
      }).then(res => {
        if (payload.callback) {
          payload.callback()
        }
      })
    },
    DelDetail ({ commit }, { id, callback }) {
      ajax(api.course.delDetail.url, {
        method: api.course.delDetail.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        if (callback) {
          callback()
        }
      })
    },
    SetListNull ({ commit }) {
      commit('updateList', [])
    }
  }
}
