import ajax from "@/libs/Ajax";

export default {
  /**
   * 获取字典表数据,返回的数据经过包装,只适用于下拉选择,通常会缓存到本地存储
   * @param {String} codeType  字典类型
   */
  async getAllCodes(codeType) {
    let params = {codeType};
    let data = await ajax.get(`/base/code/getAllCodes`, {params});
    data = data || [];
    let wrapData = [];
    for (let dataProp in data) {
      if (!data.hasOwnProperty(dataProp)) {
        continue;
      }
      let oneData = data[dataProp];
      let key = oneData['codekey'];
      let label = oneData['codekey'];
      let value = oneData['codeval'];
      let wrapValue = {
        label,
        key,
        value,
        index: Object.values(oneData).join(";")
      };
      wrapData.push(wrapValue);
      // 这里放个map 快速取值用
    }
    return wrapData;
  },

  /**
   * 查询字典树,懒加载
   * @param {String} treeKey  查询的树类型
   * @param {String} pid  父节点ID
   * @param {String} keyword  搜索关键字
   * @param {String} field  查询搜索字段
   */
  async getTree(treeKey, pid, keyword, field) {
    if (keyword) {
      let params = {treeKey, fieldKey: field, keyWordKey: keyword};
      let data = await ajax.get(`/base/tree/getTreeMatchingNodes`, {params});
      return data || [];
    }
    let params = {treeKey, pid, lazy: true};
    let data = await ajax.get(`/base/tree/getTreeNodes`, {params});
    return data || [];
  },

  /**
   * 查询对应的数据选择器数据
   * @param {String} dataType  数据类型
   * @param {Object} params  查询参数
   */
  async querySelectorData(dataType, params) {
    let data = await ajax.get(`/base/select/${dataType}`, {params, cache: false});
    return data || [];
  }
};
