import {
  getDictCode,
  getDictTree
} from '@/api/dict'
import {
  getCulturalRelicList
} from '@/api/culturalRelic'

const state = {
  dicts: {}, //字典
  crList: [], //文物列表
  ltList: [] //文献列表
};

const mutations = {
  SET_DICTS(state, obj) {
    state.dicts[obj.label] = obj.value;
  },
  SET_CR_LIST(state, crList) {
    state.crList = crList
  }
};

const actions = {
  /**
   * 获取普通list结构(非树结构)的字典
   * @param {Array} data 字典的key集合
   * @returns {Promise}
   */
  getDictList({
    commit
  }, data) {
    let dicts = {}
    let requestDicts = []
    data.forEach(i => {
      if (!state.dicts[i]) {
        requestDicts.push(i)
      } else {
        dicts[i] = state.dicts[i]
      }
    });
    if (requestDicts.length > 0) {
      return new Promise((resolve, reject) => {
        getDictCode(requestDicts).then(res => {
          requestDicts.forEach(reqDict => {
            var obj = {}
            if (res.code == 0) {
              res.data.map(item => {
                if (item.dictType == reqDict) {
                  obj[item.value] = item.label
                }
              })
              dicts[reqDict] = Object.freeze(obj)
              commit('SET_DICTS', {
                label: reqDict,
                value: Object.freeze(obj)
              })
            }
          })
          resolve(dicts)
        }).catch(error => {
          reject(error)
        })
      });
    } else {
      return new Promise((resolve, reject) => {
        resolve(dicts)
      })
    }
  },

  /**
   * 获取树形结构的字典树，如文物年代、文物质地
   * @param {Array} data 字典的key集合
   * @returns {Promise}
   */
  getDictTree({
    commit
  }, data) {
    let dicts = {}
    let requestDicts = []
    data.forEach(i => {
      if (!state.dicts[i]) {
        requestDicts.push(i)
      } else {
        dicts[i] = state.dicts[i]
      }
    });
    if (requestDicts.length > 0) {
      return new Promise((resolve, reject) => {
        getDictTree(requestDicts).then(res => {
          let dictsObj = {}
          requestDicts.forEach(reqDict => {
            dictsObj[reqDict] = []
            res.data.map(item => {
              if (item.dictType == reqDict) {
                dictsObj[reqDict].push(item)
              }
            })
            commit("SET_DICTS", {
              label: reqDict,
              value: Object.freeze(dictsObj[reqDict]),
            });
          })
          resolve(dictsObj)
        }).catch(error => {
          reject(error)
        })
      });
    } else {
      return new Promise((resolve, reject) => {
        resolve(dicts)
      })
    }
  },

  /**
   * 获取文物列表
   * @param {Boolean} isReload 是否要重新加载
   * @param {params}  params 分页或一些查询参数
   * @returns {Promise}
   */
  getCrList({
    commit
  }, {
    isReload,
    params
  }) {
    if (!state.crList || state.crList.length == 0 || isReload) {
      return new Promise((resolve, reject) => {
        getCulturalRelicList(params).then(response => {
          const {
            data
          } = response
          commit('SET_CR_LIST', data.records)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    } else {
      return new Promise((resolve, reject) => {
        resolve(state.crList)
      })
    }
  },
}

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