<template>
  <div class="group" v-for="group in userGroupInfo" :key="group.userGroupId">
    <span class="group-name">
      {{ group.name }}
    </span>
    <span class="group-info">
      共
      <span style="color: #1777ff">{{ group.total }}</span>
      人
    </span>
    <div class="user-info" :class="[group.isExpand ? 'expanded' : 'hidden']" ref="userInfoRefs">
      <div class="user-info--left">
        <el-tag type="primary" v-for="user in group.userList" :key="user.id">
          {{ user.username }}-{{ user.realname }}
        </el-tag>
      </div>
      <div class="user-info--right" v-if="group.isHidden">
        <span class="user-info--btn" @click="toggleExpand(group)">
          {{ group.isExpand ? '收起' : '展开' }}
          <el-icon><component :is="group.isExpand ? ArrowUp : ArrowDown" /></el-icon>
        </span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import type { UserGroupInfo } from '@/types/push-object';
  import { ArrowUp, ArrowDown } from '@element-plus/icons-vue';
  const userInfoRefs = ref<Record<string, HTMLElement>>({});
  const userGroupInfo = ref<UserGroupInfo[]>();
  const toggleExpand = (group: UserGroupInfo) => {
    group.isExpand = !group.isExpand;
  };
  const props = defineProps<{
    userGroupInfo?: UserGroupInfo[];
  }>();
  watch(
    () => props.userGroupInfo,
    (newVal) => {
      userGroupInfo.value = newVal;
    },
  );
  watch(
    () => userInfoRefs.value,
    (newRefs) => {
      if (newRefs) {
        Object.keys(newRefs).forEach((key) => {
          const el = newRefs[key];
          if (el.offsetHeight >= 86) {
            userGroupInfo.value![Number(key)].isHidden = true;
          }
        });
      }
    },
  );
</script>

<style lang="scss" scoped>
  .group {
    font-size: 12px;
    font-weight: 400;
    color: rgba($text-color, 0.88);
  }
  .group-name {
    font-size: 16px;
    line-height: 22px;
  }
  .group-info {
    margin-left: 4px;
    line-height: 17px;
  }
  .user-info {
    display: flex;
    &.hidden {
      max-height: 86px;
      overflow-y: hidden;
    }
    &.expanded {
      max-height: 100%;
      overflow-y: auto;
    }
    &--left,
    &--right {
      margin-top: 20px;
    }
    &--left {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      flex: 1;
    }
    &--right {
      width: 50px;
    }
    &--btn {
      @include flex-center;
      gap: 4px;
      cursor: pointer;
    }
  }
  .el-tag {
    font-weight: 500;
    font-size: 12px;
    color: $primary-color;
    line-height: 20px;
  }
</style>
