import { getReviewerSelectList, getRankDic } from '@/api/financials'
const getDefaultState = () => {
  return {
    reviewerList: {},
    rankList: {},
    financialsActiveName: 'MonthlyPayment',
    paymentQuery: null,
    paymentHistoryQuery: null,
    revenusQuery: null
  }
}
const state = getDefaultState()
const mutations = {
  RESET_FINANCIALS: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_REVIEWER: (state, reviewerList) => {
    state.reviewerList = reviewerList
  },
  SET_RANK: (state, rankList) => {
    state.rankList = rankList
  },
  SET_ACTIVENAME: (state, activeName) => {
    state.financialsActiveName = activeName
  },
  SET_PAYMENTQUERYPARAM: (state, query) => {
    state.paymentQuery = query
  },
  SET_PAYMENTHISTORYQUERYPARAM: (state, query) => {
    state.paymentHistoryQuery = query
  },
  SET_REVENUSQUERYPARAM: (state, query) => {
    state.revenusQuery = query
  }
}
const actions = {
  getReviewer({ commit, state }) {
    return new Promise((resolve, reject) => {
      getReviewerSelectList().then(response => {
        if (response.IsSuccess) {
          response.Result.forEach(element => {
            element.info = `${element.FullName}  (${element.Code})`
          })
          commit('SET_REVIEWER', response.Result)
          resolve()
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  },
  getRank({ commit, state }) {
    return new Promise((resolve, reject) => {
      getRankDic().then(response => {
        if (response.IsSuccess) {
          commit('SET_RANK', response.Result)
          resolve()
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  },
  setActiveName({ commit }, activeName) {
    commit('SET_ACTIVENAME', activeName)
  },
  resetState({ commit }) {
    commit('RESET_FINANCIALS')
  }
}

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