import request from '@birt/funclib/AxiosUtil'
import { getResData,getDataAccess } from "@birt/api/user";
export default {
  methods:{
    getSchemeC(data, me) {
      return new Promise(function (resolve, reject) {
        getDataAccess(data).then(res => {
          if (res.status == 200) {
            var list = res.dataPack,
              list1 = [];
            for (var i = 0; i < list.length; i++) {
              if (list[i].dataAccess) {
                var list2 = list[i].dataAccess;
                for (var j = 0; j < list2.length; j++) {
                  list1.push({
                    funcUrl: list[i].funcUrl,
                    name: list2[j].desc,
                    value: list2[j].schemeid,
                    TYPE: list2[j].schemeid,
                    fdaguid: list2[j].schemeid
                  });
                }
              }
            }
            me.schemeData = list1;
            me.schemeData.forEach((v, i) => {
              v.isOther = i;
              var key = v.funcUrl;
              var data1 = me.rData.hasOwnProperty(key) ?
                me.rData[key] :
                new Object();
              data1[v.TYPE] = v;
              me.rData[key] = data1;
            });
            resolve();
          } else {
            reject();
          }
        });
      });
    },
    loadMethodC(store, loadCdion, option, resolve) {
        let me = this;
        var data = {
          modelFilePath: "/platf/dataaccess/entity/SysDataaccessEntity.xml"
        };
        me.getSchemeC(data, me).then(function (value) {
          getResData().then(res => {
            if (res.status == 200) {
              me.resData = res.dataPack;
              me.addOpetionData(me.resData, me.rData);
              console.log("resources 2222222", me.resData);
              option = option || {};
              resolve.call(store, me.resData, option);
            }
          });
        })
    },
    //  根据用户查询反查
    getPermissionByUer(params){
      return request({
        url: '/sys/auth/query-all-user',
        method: 'post',
        data:params
      })
    },
    //  根据功能查询反查
    getPermissionByResAll(params){
      return request({
        url: '/sys/auth/query-all-res',
        method: 'post',
        data:params
      })
    },
    arrayToJson(data, id, pid) {
      let result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      let map = {};
      data.forEach(item => {
        map[item[id]] = item;
      });
      data.forEach(item => {
        let parent = map[item[pid]];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },
    //  tree转数组
    jsonToArray(nodes) {
      var r = [];
      if (Array.isArray(nodes)) {
        for (var i = 0, l = nodes.length; i < l; i++) {
          r.push(nodes[i]); // 取每项数据放入一个新数组
          if (
            Array.isArray(nodes[i]["children"]) &&
            nodes[i]["children"].length > 0
          )
            // 若存在children则递归调用，把数据拼接到新数组中，并且删除该children
            r = r.concat(this.jsonToArray(nodes[i]["children"]));
          delete nodes[i]["children"];
        }
      }
      return r;
    },
    deepCopy(obj) {
      var object;
      // 深度复制数组
      if (Object.prototype.toString.call(obj) == "[object Array]") {
        object = [];
        // for (var i = 0; i < obj.length; i++) {
        //   object.push(deepCopy(obj[i]));
        // }
        for (var o of obj) {
          object.push(this.deepCopy(o));
        }
        return object;
      }
      // 深度复制对象
      if (Object.prototype.toString.call(obj) == "[object Object]") {
        object = {};
        for (var p in obj) {
          object[p] = obj[p];
        }
        return object;
      }
    },
    //  树形表格过滤
    // handleTreeData(treeData, searchValue) {
    //   debugger
    //   if (!treeData || treeData.length === 0) {
    //     return [];
    //   }
    //   const array = [];
    //   for (let i = 0; i < treeData.length; i += 1) {
    //     let match = false;
    //     for (let pro in treeData[i]) {
    //       if (typeof treeData[i][pro] == "string") {
    //         match |= treeData[i][pro].includes(searchValue);
    //         if (match) break;
    //       }
    //     }
    //     if (
    //       this.handleTreeData(treeData[i].children, searchValue).length > 0 ||
    //       match
    //     ) {
    //       array.push({
    //         ...treeData[i],
    //         children: this.handleTreeData(treeData[i].children, searchValue)
    //       });
    //     }
    //   }
    //   return array;
    // },
  }
}