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', {})
    },
    getReportList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.report.list,
          params: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch((err) => {
          resolve(err)
        })
      })
    },
    getReportCheckedList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.report.checkedlist,
          params: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch((err) => {
          resolve(err)
        })
      })
    },
    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)
          }
        })
      })
    },
    saveCheckedReport (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.report.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()
        })
      })
    },
    changeGoodsStatus (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.goods.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)
          }
        })
      })
    }
  }
}