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

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

          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    addDynamics (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.dynamic.add, 
          data: payload.detail,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    editDynamics (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.dynamic.edit,
          data: payload.detail,
        }).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()
        })
      })
    },
    publicDynamic (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.dynamic.public,
          urlData: 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)
          }
        })
      })
    }
  }
}