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

export default {
  namespaced: true,
  state: {
    location: [],
    locationAll: []
  },
  mutations: {
    init (state, data) { // 这里的state对应着上面这个state
      state.location = data
    },
    initAll (state, data) { // 这里的state对应着上面这个state
      state.locationAll = data
    },
  },
  actions: {
    updateLocationInfo ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.cms.location.url, {
          method: api.cms.location.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('init', res)
          resolve(res)
        }).catch(reject)
      })
    },
    updateLocationAllInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        ajax(api.cms.location.url, {
          method: api.cms.location.method,
        }).then(res => {
          commit('initAll', res)
          resolve(res)
        }).catch(reject)
      })
    },
    getCourseLink ({ commit }) {
      return new Promise((resolve, reject) => {
        ajax(api.cms.getCourseLink.url, {
          method: api.cms.getCourseLink.method,
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
  }
}
