import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import "@/router/permission";

Vue.use(ElementUI);

Vue.config.productionTip = false;

Vue.prototype.flattenedToTree = (data, parentId = 0, pidName = "parentId", idName = "deptId", childrenName = "children") => {
  if (!data) return [];
  // 遍历结果
  const result = [];
  // 存储id 唯一值
  const map = {};

  for (const iterator of data) {
    const id = iterator[idName];
    const pid = iterator[pidName];

    // 如果map对象不存在此id
    if (!map[id]) {
      map[id] = {
        [childrenName]: []
      };
    }

    map[id] = { ...iterator, [childrenName]: map[id][childrenName] };

    const treeItem = map[id];

    // 当前的pid 等于传进来的pid 处于顶级节点  
    if (pid === parentId) {
      result.push(treeItem);
    } else {
      // 是否存在pid为键名的对象 否创建对象 
      if (!map[pid]) {
        map[pid] = {
          [childrenName]: []
        };
      }
      // 处于子级 找到相应pid 对象下的children
      map[pid][childrenName].push(treeItem);
    }
  }

  return result;
};

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app');
