<template>
  <div>
    <component
      v-if="homeContent && showHomeContent"
      :is="homeContent"
      visible-key="showHomeContent"
      :parent-target="_self"
      @reload="$reloadHandle"
    ></component>
  </div>
</template>

<script>
import corejsConfig from "@/corejsConfig";
import settingConfig from "@/settings";

export default {
  name: 'home',
  components: {},
  data() {
    return {
      flag: 0,
      homeContent: null,
      showHomeContent: false,
      userInfo: {}
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.$http({
        url: USER_PREFIX + '/user/currentUser',
        method: 'post',
        success: res => {
          let userInfo = res.objx;
          this.userInfo = userInfo;
          let flag = userInfo.flag;
          this.initHome();
        }
      });
    },
    getIsDistributor(userInfo) {
      this.$http({
        url: USER_PREFIX + '/userb/getUserRole',
        method: `post`,
        data: {id: userInfo.id},
        success: res2 => {
          let rows = res2.objx || [];
          let codes = rows.map(row => row.roleCode);
          if (codes.includes('distributor')) {
            this.flag = 2;
          } else {
            this.initHome();
          }
        }
      });
    },
    initHome() {
      let url = null;
      let userInfo = this.userInfo;
      let flag = userInfo.flag;
      if (flag == 6 || flag == 7 || flag == 8) {
        let url = '/user/home/dev.vue'
        this.homeContent = require('@base/views' + url).default;
        this.showHomeContent = true;
        return
      }
      this.$http({
        url: USER_PREFIX + '/menu/currentList',
        data: {},
        method: 'post',
        success: res => {
          let menus = res.objx || [];
          let homeMenu = menus.find(menu => menu.type == 2 && menu.enabled);
          let menuUrl = homeMenu?.url;

          let userInfo = this.userInfo;
          let flag = userInfo.flag;

          let str1 = "@base/views";
          let str2 = "@/views";

          if (menuUrl && flag !== 1) {
            //超级管理员不使用角色首页
            url = menuUrl + '.vue';
          } else {
            let defaultHomePage = this.userInfo.defaultHomePage;
            url =  defaultHomePage || settingConfig.defaultHomePage || '@base/views/user/home/default.vue'
          }
          if (url.startsWith(str1)) {
            this.homeContent = require('@base/views' + url.slice(str1.length)).default;
          } else if (url.startsWith(str2)) {
            this.homeContent = require('@/views' + url.slice(str2.length)).default;
          } else {
            this.homeContent = require('@/views' + url).default;
          }

          this.showHomeContent = true;
          this.flag = 1;
        }
      });
    }

  }
};
</script>
