<template>
  <component :is="apps_info.theme"></component>
</template>

<script>
export default {
  components: {
    themeOne: resolve => require(["views/layout/theme/one"], resolve),
    themeTwo: resolve => require(["views/layout/theme/two"], resolve)
  },
  data() {
    return {};
  },
  watch: {
    $route(to, from) {
      this.init();
    },
    breadcrumb: {
      deep: true,
      handler(data) {
        // console.log(data);
        // 设置左侧菜单高亮和菜单展开项
        const mids = data.map(item => Number(item.id)).filter(id => id);
        this.$store.commit("SET_ACTIVES", this.deepClone(mids));
      }
    },
    tagnavs: {
      deep: true,
      handler(data) {
        // 设置缓存页面
        this.$store.dispatch("getCachPage");
      }
    }
  },
  created() {
    this.indexPermissions(); // 一进入页面获取菜单
  },
  methods: {
    init() {
      const mid = this.$route.query.mid;
      if (mid !== undefined) {
        this.menuChange(mid);
      }
      this.$store.dispatch("getCurrentNodes"); // 获取当前页面操作按钮
      this.getBreadcrumb();
    },

    /**
     * [indexPermissions 获取菜单列表]
     * @return {[type]} [description]
     */
    indexPermissions() {
      this.$store.dispatch("getSiteInfo").then(res => {
        if (res.code == 0) {
          this.$nextTick(() => {
            // 渲染页面数据
            this.init();
          });
        }
      });
    },

    /**
     * [menuChange 路由变化，将菜单添加到‘tagnavs’变量中]
     * @param  {[type]} mid [description]
     * @return {[type]}      [description]
     */
    menuChange(mid) {
      this.$nextTick(() => {
        const route = this.$route;
        const cur_permission =
          this.permissions_object && this.permissions_object[mid]
            ? this.permissions_object[mid]
            : null;
        let has = true;

        if (has) {
          for (let i = 0, len = this.tagnavs.length; i < len; i++) {
            if (this.tagnavs[i].id == mid) {
              has = false;
              break;
            }
          }
        }

        if (has) {
          let obj = {
            id: mid,
            path: route.path,
            name: route.name,
            query: this.deepClone(route.query),
            params: this.deepClone(route.params),
            title: cur_permission ? cur_permission.title : route.meta.title
          };
          this.$store.commit("SET_TAGNAVS", obj);
        }
      });
    },

    // 获取面包屑
    getBreadcrumb() {
      this.$store.dispatch("getBreadcrumb");
    }
  }
};
</script>
