
<template>
  <ul class="nav navbar-nav navbar-right">
    <li class="dropdown">
      <a
        v-if="loginInfo"
        
        class="dropdown-toggle"
        data-toggle="dropdown"
        role="button"
        aria-haspopup="true"
        aria-expanded="false"
      >
        {{ loginClientName }}
        <span class="caret" />
      </a>
      <ul
        v-if="clientCanAccessList != null && clientCanAccessList.length > 0"
        class="dropdown-menu"
      >
        <li
          v-for="clientCanAccess in clientCanAccessList"
          :key="clientCanAccess.id"
        >
          <a @click="changeTokenClient(clientCanAccess.id)">{{ clientCanAccess.name }}</a>
        </li>
      </ul>
    </li>
  </ul>
  <a-modal v-model:visible="visible" title="新用户未设置所属部门或岗位">
    <p>
      尊敬的用户，
    </p>
    <p>
      您好,
      您的账号还未设置所属部门或岗位，因此您可能看不到任何数据。请联系您公司的管理员为您分配权限。
    </p>
    <template #footer>
      <a-button @click="visible = false">确定</a-button>
      <a-button @click="goHome">退出</a-button>
    </template>
  </a-modal>
</template>

<script>

import Common from '../common/Common.js';
import ClientResourceV2 from '../api/base/ClientResourceV2.js';
import TokenClientResource from '../api/base/TokenClientResource.js';
import { Notify, Uuid } from 'pc-component-v3';

export default {
  props: {
    loginInfo: {
      type: Object,
      default: function(){
        return null;
      },
    },
  },

  data: function () {
    return {
      clientCanAccessList: [],
      loginClientName:'',
      visible: false,
    };
  },


  mounted: function () {
    this.listClientCanAccess();
    this.queryTokenClient();
  },

  methods: {
    /**
     * 获取可以访问的公司
     */
    listClientCanAccess: function () {
      var _self = this;
      ClientResourceV2.listClientCanAccess().then(baseListResponse => {
        if (baseListResponse.errorCode == 0) {
          _self.clientCanAccessList = baseListResponse.datas;
        } else if(baseListResponse.errorCode == -1){
          _self.visible = true;
        } else {
          Notify.error(_self.$t('lang.Notify.error'), baseListResponse.errorMessage, false);
        }
      }, errorData => {
        Common.processException(errorData);
      });
    },

    /**
     * 切换公司
     */
    changeTokenClient: function (clientId) {
      var _self = this;
      console.log(_self.loginInfo);
      const tokenClientChangeRequest = {
        userId: _self.loginInfo.userId,
        clientId: clientId,
        token: _self.loginInfo.token,
      };

      TokenClientResource.changeTokenClient(tokenClientChangeRequest).then(baseObjectResponse => {
        if (baseObjectResponse.errorCode == 0) {
          // Notify.success('正确','公司切换成功',true);
          _self.queryTokenClient();
          window.location.reload();
        }else{
          Notify.error(_self.$t('lang.Notify.companySwitchFailed'), baseObjectResponse.errorMessage, false);
        }
      }, errorData => {
        Common.processException(errorData);
      });
    },

    /**
     * 查询设置的公司
     */
    queryTokenClient: function () {
      var _self = this;
      TokenClientResource.queryTokenClient().then(baseObjectResponse => {
        if (baseObjectResponse.errorCode == 0) {
          _self.loginClientName = baseObjectResponse.data.clientName;
        }else if (baseObjectResponse.errorCode == -1){
          // add by jack 2024-04-09
          // see http://wuzhixin.vip:8080/#/knowledge/1/markdown/viewer/1239
          _self.visible = true;
        }else{
          Notify.error(_self.$t('lang.Notify.error'), baseObjectResponse.errorMessage, false);
        }
      }, errorData => {
        Common.processException(errorData);
      });
    },

    goHome: function(){
      window.location.href = Common.getRootPath() + '/';
    },
  },
};
</script>

