import storage from '@/utils/storage'
import constants from "@/utils/constants";
import Api from '@/api'

export default {
  state: {
    prefix: storage.getObj(constants.prefix),
    userinfo: storage.getObj(constants.userinfo),
    projectId: storage.getInt(constants.projectId),
    token: storage.getStr(constants.token),
    customField: [],
    patentField: [],
    commonData: {},
    currentNum: 0,
    record: [],
    webSocket: null,
  },

  mutations: {
    SET_PREFIX: (state, prefix) => {
      state.prefix = prefix
    },
    SET_USERINFO: (state, userinfo) => {
      state.userinfo = userinfo
      storage.setObj(constants.userinfo, userinfo)
    },
    SET_TOKEN: (state, token) => {
      state.token = token
      storage.setStr(constants.token, token)
      storage.setCookie(constants.tokenHeader, token)
    },
    SET_PROJECT_ID: (state, projectId) => {
      state.projectId = projectId
      storage.setStr(constants.projectId, projectId)
    },
    SET_CUSTOM_FIELD: (state, customField) => {
      state.customField = customField
    },
    SET_SYSTEM_FIELD: (state, patentField) => {
      state.patentField = patentField
    },
    SET_COMMON_DATA: (state, commonData) => {
      state.commonData = commonData
    },
    SET_CURRENT_NUM: (state, currentNum) => {
      state.currentNum = currentNum
    },
    SET_RECORD: (state, record) => {
      state.record = record
    },
    SET_WEB_SOCKET: (state, webSocket) => {
      state.webSocket = webSocket
    },
  },

  actions: {
    getCurrentGenFileNum({ commit }) {
      return Api.getCurrentGenFileNum().then(response => {
        commit('SET_CURRENT_NUM', response.data)
      })
    },
    getReportDownloadRecord({ commit }, id) {
      return Api.getReportDownloadRecord({ id }).then(response => {
        commit('SET_RECORD', [response.data])
      })
    },
    getCommonData({ commit }) {
      return Api.getCommonData().then(response => {
        commit('SET_COMMON_DATA', response.data)
      })
    },
    getSystemField({ commit }, projectId) {
      return Api.getSystemField({ projectId }).then(response => {
        commit('SET_SYSTEM_FIELD', response.data)
      })
    },
    getCustomField({ commit }, projectId) {
      return Api.getCustomField({ projectId }).then(response => {
        commit('SET_CUSTOM_FIELD', response.data)
      })
    }
  }
}
