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

// 请求数据
export default {
  namespaced: true,
  state: {
    projectInfo: {},
    courseDetail: {},
  },
  mutations: {
    setPageInfo (state, data) { // 设置页面信息
      state.projectInfo = data
    },
    setCourseDetailInfo (state, data) { // 设置课程详情信息
      state.courseDetail = data
    }
  },
  actions: {
    placeCourseOrderBefore (context, { order, customercouponid }) { // 课程预下单
      return new Promise((resolve, reject) => {
        Ajax(api.majorProjects.placeCourseOrderBefore.url, {
          method: api.majorProjects.placeCourseOrderBefore.method,
          data: {
            info: window.JSON.stringify(order),
            customercouponid,
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    placeOrderForCourse (context, { id, payload }) { // 课程下单
      return new Promise((resolve, reject) => {
        Ajax(api.majorProjects.placeCourseOrder.url, {
          method: api.majorProjects.placeCourseOrder.method,
          urlData: {
            id,
          },
          data: {
            customercouponid: payload.customercouponid
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getProjectInfo (context, { id }) { // 获取课程列表
      return new Promise((resolve) => {
        Ajax(api.majorProjects.getCourseList.url, {
          method: api.majorProjects.getCourseList.method,
          urlData: {
            id,
          }
        }).then(res => {
          context.commit('setPageInfo', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getCourseDetailInfo (context, { id }) { // 获取课程详情信息
      return new Promise((resolve) => {
        Ajax(api.majorProjects.getCourseDetail.url, {
          method: api.majorProjects.getCourseDetail.method,
          urlData: {
            id,
          }
        }).then(res => {
          context.commit('setCourseDetailInfo', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
  }
}
