<template>
  <div id="app" class="dm-reset-style">
    <router-view></router-view>
  </div>
</template>

<script>
import { mapState } from 'vuex';

export default {
  name: 'App',

  computed: {
    ...mapState([
      'uniqueId'
    ]),
  },

  created() {
    this.baseUrl = window.location.origin.indexOf('localhost') > -1 ? 'http://www.gicdev.com' : window.location.origin;
    this._getUserInfo();
    this.changeTab();
    this.isEditClique();
  },

  methods: {
    isEditClique() {
      this.axios.get(`${this.baseUrl}/api-member/allow-edit-clique-grade?requestProject=member`).then((res) => {
        if (res.data.errorCode == 0) {
          let showEditClique = res.data.result;
          this.$store.commit('setEditClique', showEditClique);
        }
      });
    },
    laout() {
      this.axios.get(`${this.baseUrl}/api-auth/do-logout?requestProject=member`);
    },
    _getUserInfo() {
      this.axios.get(`${this.baseUrl}/api-auth/get-login-user-info?requestProject=member`).then((res) => {
        if (res.data.errorCode == 0) {
          const uId = res.data.result && res.data.result.userId;
          this.setStore(res.data.result || {});
          if (this.uniqueId === '') {
            this.$store.commit('changeUniqueId', uId);
          }
        }
      });
    },
    changeTab() {
      document.addEventListener("visibilitychange", () => {
        // 表示切换过来去查uid跟当前uid对比
        if (document.visibilityState === 'visible') {
          this.axios.get(`${this.baseUrl}/api-auth/get-login-user-info?requestProject=member`).then(res => {
            if (res.data.errorCode == 0) {
              const uId = res.data.result && res.data.result.userId;
              // 两次的uId不等 表示账号有冲突
              if (this.uniqueId !== uId) {
                this.$store.commit('changeUniqueId', uId);
                this.setStore(res.data.result || {});
                // 切换账号后，不缓存权限数据、菜单数据、面包屑数据
                window.notCache = true;
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请重新登录！', '登录账号变更提示', {
                  confirmButtonText: '重新登录',
                  cancelButtonText: '刷新页面',
                  type: 'error'
                })
                  .then(() => {
                    window.location.href = '/gic-web/#/login';
                  })
                  .catch(() => {
                    window.location.href = '/report';
                  });
              }
            }
          });
        }
      });
    },
    setStore(data) {
      this.$store.commit('checkAdmin', data);
      // // 如果是巴拉的子账号，不显示积分相关的操作按钮(废弃)
      // const { enterpriseId, superAdmin } = data;
      // const ids = [
      //   'ff8080816dd0385e016ddca436d01fe1', // 生产巴拉商户
      //   'ff8080816a36326c016a53380d8b5f52', // 生产金华达摩
      //   'ff8080815dacd3a2015dacd3ef5c0000', // 测试金华达摩
      // ]
      // this.$store.commit('showHandleScoreBtn', !(ids.includes(enterpriseId) && superAdmin == 0));
    }
  }
}
</script>
