import { getSignText } from '@/api/trials'
const getDefaultState = () => {
  return {
    trialDetailActiveName: '',
    trialQuery: null,
    subjectQuery: null,
    visitPlanQuery: null,
    visitPointQuery: null,
    studyListQuery: null
  }
}

const state = getDefaultState

const mutations = {
  RESET_TRIALS: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_ACTIVENAME: (state, activeName) => {
    state.trialDetailActiveName = activeName
  },
  SET_QUERYPARAM: (state, trialQuery) => {
    state.trialQuery = trialQuery
  },
  SET_SUBJECTPARAM: (state, subjectQuery) => {
    state.subjectQuery = subjectQuery
  },
  SET_VISITPLANPARAM: (state, visitPlanQuery) => {
    state.visitPlanQuery = visitPlanQuery
  },
  SET_VISITPOINTPARAM: (state, visitPointQuery) => {
    state.visitPointQuery = visitPointQuery
  },
  SET_STUDYLISTPARAM: (state, studyListQuery) => {
    state.studyListQuery = studyListQuery
  }
}

const actions = {
  setActiveName({ commit }, activeName) {
    commit('SET_ACTIVENAME', activeName)
  },
  resetState({ commit }) {
    commit('RESET_TRIALS')
  },
  getSignInfo({ commit }, signInfo) {
    const { signCode } = signInfo
    return new Promise((resolve, reject) => {
      getSignText(signCode).then(response => {
        if (response.IsSuccess) {
          resolve(response.Result)
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  }
}

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

