
<template>
  <a-dropdown v-model:open="isMenuOpen" :trigger="['click']">
    <a-button type="link" size="small" class="ant-dropdown-link" @click.prevent>
      {{ loginClientName }}
      <down-outlined />
    </a-button>
    <template #overlay>
      <a-menu v-if="clientCanAccessList.length > 0">
        <a-menu-item 
          v-for="clientCanAccess in clientCanAccessList"
          :key="clientCanAccess.id"
          @click="changeTokenClient(clientCanAccess.id)"
        >
          {{ clientCanAccess.name }}
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>

  <a-modal v-model:open="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';
import { DownOutlined } from '@ant-design/icons-vue';

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

  data: function () {
    return {
      clientCanAccessList: [],
      loginClientName:'',
      visible: false,
      isMenuOpen: 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>

<style scoped>
.ant-dropdown-link {
  color: rgba(0, 0, 0, 0.85);
  padding: 0 ;
}
</style>