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', {})
    },
    getExchanges ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.exchange.list,
          params: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getGoodsDetail ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.goods.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)
          }
        })
      })
    },
    editGoods (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.goods.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()
        })
      })
    },
    changeExchangeStatus (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.exchange.change,
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    verifyById (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.exchange.update,
          urlData: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          reject(err)
        })
      })
    }
  }
}