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

export default {
  namespaced: true,
  state: {
    echartsInfo: {},
    newsUserCount:{},
    bizEvents: [],
    bizEventTypes: [],
    bizEventProperties: [],
  },
  mutations: {
    updateList (state, payload) {
      state.echartsInfo = payload
    },
    updateNewsUserCount (state, payload) {
      state.newsUserCount = payload
    },
    updateEvents (state, payload) {
      state.bizEvents = payload
    },
    updateEventTypes (state, payload) {
      state.bizEventTypes = payload
    },
    updateEventProperties (state, payload) {
      state.bizEventProperties = payload
    },
  },
  actions: {
    setDetailNull ({ commit }) {
      commit('updateList', {})
    },

    getBizEvents ({ state, commit }) {
      if (state.bizEvents && state.bizEvents.length) {
        return Promise.resolve(state.bizEvents)
      }

      return new Promise((resolve, reject) => {
        request({
          ...apis.bizEvent.list,
          params: { pageSize: 9999 }
        }).then((data) => {
          commit('updateEvents', data.records)
          resolve(data.records)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },

    getBizEventTypes ({ state, commit }) {
      if (state.bizEventTypes && state.bizEventTypes.length) {
        return Promise.resolve(state.bizEventTypes)
      }

      return new Promise((resolve, reject) => {
        request({
          ...apis.bizEvent.dict,
          params: { pageSize: 9999 }
        }).then((data) => {
          commit('updateEventTypes', data.records)
          resolve(data.records)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },

    getBizEventProperties ({ state, commit }, payload = {}) {
      if (state.bizEventProperties && state.bizEventProperties.length) {
        return Promise.resolve(state.bizEventProperties)
      }

      return new Promise((resolve, reject) => {
        request({
          ...apis.bizEvent.properties,
          params: { pageSize: 9999, ...payload }
        }).then((data) => {
          commit('updateEventProperties', data.records)
          resolve(data.records)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },

    getUserConversionRate(_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.userConversion,
          params: payload,
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },

    getIndexEcharts ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.list,
          params: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getNewsUserCount ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.newUser,
          params: payload,
        }).then((data) => {
          resolve(data.selectNewsUserCount)
        }).catch((err) => {
          const message = err.message || err.msg
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getUserResource({ commit }, payload) { // 用户来源 详情页
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.userResource,
          params: payload
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getUserBehaviorSummary({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.userBehavior.summary,
          params: payload
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getUserBehaviorProfile({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.userBehavior.profile,
          params: payload
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getIntentionUsers({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.intentionUsers,
          params: { pageSize: 9999, ...payload }
        }).then((data) => {
          resolve(data.records)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getUserSex({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.userSex,
          params: { pageSize: 9999, ...payload }
        }).then((data) => {
          resolve(data.selectSexUser)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getUserCity({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.userCity,
          params: { pageSize: 9999, ...payload }
        }).then((data) => {
          resolve(data.selectCityUser)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getUserActive({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.indexEcharts.userActive,
          params: { pageSize: 9999, ...payload }
        }).then((data) => {
          resolve(data.selectActiveUserCount)
        }).catch((err) => {
          reject(err)
        })
      })
    },
  }
}