<template>
  <div class="firefly_user_info">
    <slot></slot>
    <div class="userinfo_container" ref="userInfoDom">
      <div class="username">
        {{ formatUserName(userInfo[username]) }}
      </div>
      <div class="compony_container">
        <div class="compony_name">
          {{ userInfo.tenantName ? userInfo.tenantName : '暂未加入任何企业' }}
        </div>
        <span class="user_type">{{ userInfo.isSuper != 1 ? '普通用户' : '超级管理员' }}</span>
      </div>
      <i :class="['el-icon-caret-bottom', { no_drop: isDropdown }]" v-if="dropdown"></i>
    </div>
    <!-- 点击的时候下拉的时候的div -->
    <div class="dropdown_container" v-if="isDropdown" ref="chooseTenantDom">
      <div class="dropdown_header">
        <div class="username">
          {{ formatUserName(userInfo[username]) }}
        </div>
        <span class="username_text">{{ userInfo[username] }}</span>
        <div class="compony_name">
          {{ userInfo.tenantName ? userInfo.tenantName : '暂未加入任何企业' }}
        </div>
      </div>
      <div class="select_container">
        <div class="select_item" @click="toUserCenter">
          <span class="base_text">个人中心</span>
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="select_item change_compony" @mouseenter="showTenantList = true" @mouseleave="mouseLeave" v-if="tenantList.length > 0">
          <span class="base_text">切换企业</span>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <div class="login_out_container">
        <div class="login_out" @click="logout">退出登录</div>
      </div>
      <!-- 选择租户 -->
      <div :class="['choose_tenant', { show_tenant: showTenantList }]" @mouseenter="enterTenant" @mouseleave="mouseLeave">
        <div class="tenant_item" v-for="tenant in tenantList" :key="tenant.tenantId" @click="changeTenantId(tenant.tenantId)">
          <div class="tenant_name">{{ tenant.tenantName.slice(0, 1) }}</div>
          <div class="compony_container">
            <div :class="['compony_name', 'other_compony_name', { active_name: tenantId == tenant.tenantId }]">
              {{ tenant.tenantName }}
            </div>
          </div>
          <i class="el-icon-check" v-if="tenantId == tenant.tenantId"></i>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'fireflyUserInfo',
  props: {
    userInfo: {
      type: Object,
      required: true,
      default: () => {
        return {};
      }
    },

    username: {
      type: String,
      default: 'nickName'
    },

    tenantList: {
      type: Array,
      required: true,
      default: () => []
    },

    tenantId: {
      type: [Number, String],
      required: true
    },

    dropdown: {
      type: Boolean,
      default: true
    }
  },

  data() {
    return {
      isDropdown: false,
      showTenantList: false,
      timer: null
    };
  },

  mounted() {
    document.addEventListener(
      'click',
      e => {
        if (!this.dropdown) return;
        const userInfoDom = this.$refs.userInfoDom;
        const chooseTenantDom = this.$refs.chooseTenantDom;
        const isSelf = this.$refs.userInfoDom && this.$refs.userInfoDom.contains(e.target);
        const isChoose = this.$refs.chooseTenantDom && this.$refs.chooseTenantDom.contains(e.target);
        if (userInfoDom && !chooseTenantDom && isSelf) {
          this.isDropdown = true;
        }
        if (userInfoDom && chooseTenantDom && !isChoose) {
          this.isDropdown = false;
        }
      },
      true
    );
  },

  methods: {
    mouseLeave() {
      this.timer = setTimeout(() => {
        this.showTenantList = false;
      }, 200);
    },

    enterTenant() {
      clearTimeout(this.timer);
    },

    formatUserName(username) {
      if (!username) return '暂无';
      // if (this.userInfo.isSuper == 1) return '超管';
      return username.charAt(username.length - 2) + username.charAt(username.length - 1);
    },

    // 切换租户
    changeTenantId(tenantId) {
      this.$emit('changeTenantId', tenantId);
    },

    // 退出登录
    logout() {
      this.$emit('logout');
    },

    // toUserCenter
    toUserCenter() {
      this.$emit('toUserCenter');
    }
  }
};
</script>
<style lang="scss" scoped>
@import '../../../css/fireflyUserInfo.scss';
</style>
