import { getAllDictionary, getHospitalList, getAllSponsorList, getAllCROList, getAllSiteList, getUserNoticeList } from '@/api/global'
const getDefaultState = () => {
  return {
    dictionaryList: {},
    hospitalList: {},
    sponsorList: {},
    croList: {},
    siteList: {},
    noticeList: JSON.parse(zzSessionStorage.getItem('noticeList'))
  }
}
const state = getDefaultState()
const mutations = {
  RESET_GLOBAL: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_DICTIONARY: (state, dictionaryList) => {
    state.dictionaryList = dictionaryList
  },
  SET_HOSPITAL: (state, hospitalList) => {
    state.hospitalList = hospitalList
  },
  SET_SPONSOR: (state, sponsorList) => {
    state.sponsorList = sponsorList
  },
  SET_CRO: (state, croList) => {
    state.croList = croList
  },
  SET_SITE: (state, siteList) => {
    state.siteList = siteList
  },
  SET_NOTICE: (state, noticeList = []) => {
    zzSessionStorage.setItem('noticeList', JSON.stringify(noticeList))
    state.noticeList = noticeList
  }
}
const actions = {
  getDictionary({ commit, state }) {
    return new Promise((resolve, reject) => {
      getAllDictionary().then(response => {
        if (response.IsSuccess) {
          commit('SET_DICTIONARY', response.Result.DicList)
          resolve(response.Result.DicList)
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  },
  setDictionary({ commit }, dictionary) {
    commit('SET_DICTIONARY', dictionary)
  },

  getHospital({ commit, state }) {
    return new Promise((resolve, reject) => {
      getHospitalList().then(response => {
        if (response.IsSuccess) {
          commit('SET_HOSPITAL', response.Result)
          resolve(response.Result)
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  },
  setHospital({ commit }, hospital) {
    commit('SET_HOSPITAL', hospital)
  },

  getSponsorList({ commit, state }) {
    return new Promise((resolve, reject) => {
      getAllSponsorList().then(response => {
        if (response.IsSuccess) {
          commit('SET_SPONSOR', response.Result)
          resolve(response.Result)
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  },
  setSponsor({ commit }, sponsor) {
    commit('SET_SPONSOR', sponsor)
  },
  getCROList({ commit, state }) {
    return new Promise((resolve, reject) => {
      getAllCROList().then(response => {
        if (response.IsSuccess) {
          commit('SET_CRO', response.Result)
          resolve(response.Result)
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  },
  setCRO({ commit }, cro) {
    commit('SET_CRO', cro)
  },
  getSiteList({ commit, state }) {
    return new Promise((resolve, reject) => {
      getAllSiteList().then(response => {
        if (response.IsSuccess) {
          commit('SET_SITE', response.Result)
          resolve(response.Result)
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  },
  setSite({ commit }, site) {
    commit('SET_SITE', site)
  },
  getNoticeList({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserNoticeList().then(response => {
        if (response.IsSuccess) {
          var noticeList = []
          response.Result.forEach(el => {
            noticeList.push({ Id: el.Id, Content: el.NoticeContent, NoticeTypeEnum: el.NoticeTypeEnum })
          })

          commit('SET_NOTICE', noticeList)
          resolve(noticeList)
        } else {
          reject(response.ErrorMessage)
        }
      }).catch(() => {
        reject()
      })
    })
  }
}

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