import apis from '../../config/api'
import request from '../../utils/request'

export default {
  namespaced: true,
  state: {
    list: [],
    detail: {},
  },
  mutations: {},
  actions: {
    getActivityList (context, payload) {
      return request({
        ...apis.activity.list,
        params: payload,
      })
    },
    addActivity({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.activity.activityAdd,
          data: payload,
        }).then((data) => {
          window.console.log(data)
          commit('updateList', { ...data, list: data.records})
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    activityDetails (context, payload) {
      return request({
        ...apis.activity.activityDetails,
        params: payload,
      })
    },
    updateActivity({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.activity.updateActivity,
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    // 活动报名列表
    getSignList (context, payload) {
      return request({
        ...apis.activity.getSignList,
        params: payload,
      })
    },
    // 结束报名
    finishActivity({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.activity.finishActivity,
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    // 置顶
    updateWeight(_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.activity.setWeight,
          params: payload,
        }).then((data) => {
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    }
  },
}