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

export default {
  namespaced: true,
  state: {
    dynamics: {},
    detail: {}
  },
  mutations: {
    updateList (state, payload) {
      state.dynamics = payload
    },
    updateDetail (state, payload) {
      state.detail = payload
    }
  },
  actions: {
    setDetailNull ({ commit }) {
      commit('updateDetail', {})
    },
    getPointsRules ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.points.list,
          params: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getPointsDetail ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.points.detail,
          urlData: payload,
        }).then((data) => {
          commit('updateDetail', data)
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    addGoods (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.goods.add, 
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    editPoints (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.points.edit,
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    deleteDynamics (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.dynamic.delete,
          ...payload
        }).then(() => {
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },
    changePointsRulesStatus (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.points.change,
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    cancelDynamic (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.dynamic.cancel,
          urlData: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    }
  }
}