import { $api } from '@/api/api-config' // 接口配置
import request from '@/utils/request'
import Cookies from 'js-cookie'
const getDefaultState = () => {
  return {
    goodsID: Cookies.get('goodsID') ? Cookies.get('goodsID') : '',
    GoodsDetails: '',
    goodstate: Cookies.get('goodstate') ? Cookies.get('goodstate') : ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_GOODSID: (state, goodsID) => {
    state.goodsID = goodsID
  },
  SET_GOODSDETAILS: (state, GoodsDetails) => {
    state.GoodsDetails = GoodsDetails
  },
  SET_GOODSTATE: (state, goodstate) => {
    state.goodstate = goodstate
  }

}

const actions = {
  // 获取商品id
  addGoods({ commit }, params) {
    return new Promise((resolve, reject) => {
      request.post($api.addGoods, params).then(response => {
        commit('SET_GOODSID', response.goodsID)
        Cookies.set('goodsID', response.goodsID)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 编辑商品
  editGoods({ commit }, params) {
    return new Promise((resolve, reject) => {
      request.post($api.editGoods, params).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 商品列表获取goodid和商品state
  Editlist({ commit }, params) {
    commit('SET_GOODSID', params.goodsID)
    commit('SET_GOODSTATE', params.goodstate)
    Cookies.set('goodsID', params.goodsID)
    Cookies.set('goodstate', params.goodstate)
  },
  // 商品详情
  getGoodsDetails({ commit }, params) {
    commit('SET_GOODSDETAILS', params)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

