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

export default {
  namespaced: true,
  state: {
    courseList: [],
    courseLister: [],
    courseInfo: {
      IsSelect: 0,
    },
    courseImgs: [],
  },
  mutations: {
    updateList (state, payload) {
      state.courseList = payload || []
    },
    updateLister (state, payload) {
      state.courseLister = payload.list || []
    },
    updateInfo (state, payload) {
      // console.log(payload)
      state.courseInfo = payload || { IsSelect: 0 }
    },
    updateImgList (state, payload) {
      state.courseImgs = payload
    }
  },
  actions: {
    getCourseLister ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.course.getCourseList.url, {
          method: api.course.getCourseList.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('updateLister', res)
          resolve(res)
        }).catch(reject)
      })
    },
    GetCourseList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.course.list.url, {
          method: api.course.list.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('updateList', res)
          resolve(res)
          if (payload.callback) {
            payload.callback()
          }
        }).catch(reject)
      })
    },
    GetCourseByID ({ commit }, { id }) {
      ajax(api.course.info.url, {
        method: api.course.info.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        commit('updateInfo', res)
      })
    },
    AddCourse ({ commit }, payload) {
      ajax(api.course.add.url, {
        method: api.course.add.method,
        data: {
          ...payload
        }
      }).then(res => {
        commit('updateInfo', res)
        if (payload.callback) {
          payload.callback()
        }
      })
    },
    UpdateCourse ({ commit }, payload) {
      ajax(api.course.update.url, {
        method: api.course.update.method,
        data: {
          ...payload
        },
        urlData: {
          id: payload.GoodsId,
        }
      }).then(res => {
        if (payload.callback) {
          payload.callback()
        }
      })
    },
    DelCourse ({ commit }, { id, callback }) {
      ajax(api.course.delete.url, {
        method: api.course.delete.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        if (callback) {
          callback()
        }
      })
    },
    Public ({ commit }, { id, callback }) {
      ajax(api.course.public.url, {
        method: api.course.public.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        if (callback) {
          callback()
        }
      })
    },
    UnPublic ({ commit }, { id, callback }) {
      ajax(api.course.unpublic.url, {
        method: api.course.unpublic.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        if (callback) {
          callback()
        }
      })
    },
    SetNull ({ commit }) {
      commit('updateInfo', { IsSelect: 0 })
    },
    UpdateInfo ({ commit }, payload) {
      commit('updateInfo', payload)
    },
    UpdateImgList ({ commit }, payload) {
      commit('updateImgList', payload)
    },
    GetCourseImgs ({ commit }, { id, callback }) {
      ajax(api.course.getimgs.url, {
        method: api.course.getimgs.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        commit('updateImgList', res)
        if (callback) {
          callback()
        }
      })
    },
    AddCourseImg ({ commit }, payload) {
      ajax(api.course.addimgs.url, {
        method: api.course.addimgs.method,
        data: {
          ...payload
        }
      }).then(res => {
        if (payload.callback) {
          payload.callback()
        }
      })
    },
    UpdateCourseImg ({ commit }, payload) {
      ajax(api.course.updateimgs.url, {
        method: api.course.updateimgs.method,
        data: {
          ...payload
        },
        urlData: {
          id: payload.id
        }
      }).then(res => {
        if (payload.callback) {
          payload.callback()
        }
      })
    },
    DelCourseImg ({ commit }, { id, callback }) {
      ajax(api.course.deleteimgs.url, {
        method: api.course.deleteimgs.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        if (callback) {
          callback()
        }
      })
    },
  }
}
