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

export default {
  namespaced: true,
  state: {
    carouselList: {},
    // detail: {}
  },
  mutations: {
    updateList (state, payload) {
      state.carouselList = payload
    },
    // updateDetail (state, payload) {
    //   state.detail = payload
    // }
  },
  actions: {
    // setDetailNull ({ commit }) {
    //   commit('updateDetail', {})
    // },
    getExtendContent ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.carouselFigure.list,
          params: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    handleExtendContent ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.carouselFigure.handle,
          urlData: { id: payload.contentId },
          params: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    updateExtendContent ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.carouselFigure.update,
          urlData: { id: payload.contentId },
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getByIdExtendContent({ commit }, payload) { // 根据Id查询
      return new Promise((resolve, reject) => {
        request({
          ...apis.carouselFigure.getById,
          urlData: { id: payload.contentId },
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    addExtendContent ({ commit }, payload) { // 添加
      return new Promise((resolve, reject) => {
        request({
          ...apis.carouselFigure.add,
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
  }
}