<template>
  <view>
    <view :style="{ height: statusBarHeight }" class="status-bar"></view>
    <view class="relative z-3 flex items-center px-15 py-12 header-bar">
      <view class="flex-shrink-0 mr-5" @click="toTeamDetail">
        <group-avatar classType="square" :size="120" :avatarList="currentTeam ? currentTeam.avatarList : []" />
      </view>
      <view class="w-0 flex-grow">
        <view class="flex items-center">
          <view class="w-0 flex-grow flex items-center mb-10">
            <view class="team-name truncate flex-shrink-0" :style="teamStyle">{{ team.name }}</view>
            <view v-if="teams.length > 1" class="flex-shrink-0 flex items-center switch-btn ml-10"
              @click="showDropDown = true">
              <image class="switch-icon" src="/static/home/switch-team.png" mode="aspectFit"></image>
            </view>
          </view>
          <view v-if="menuButtonInfo && menuButtonInfo.width > 0" class="flex-shrink-0" :style="{
            width: menuButtonInfo.width + 'px',
            height: menuButtonInfo.height + 'px',
          }">
          </view>
        </view>
        <div class="flex items-center">
          <view v-if="currentTeam" class="w-0 flex-grow mr-5text-base text-white truncate">
            {{ currentTeam.licenseHospitalName || currentTeam.leaderCorp || currentTeam.corpName || '' }}
          </view>
          <view v-if="team && team.supportPatientForward === 'YES'"
            class="relative flex-shrink-0 flex items-center bg-white rounded-sm px-10">
            <image class="icon-share mr-5" src="/static/home/icon-share.svg"></image>
            <view class="text-sm text-warning">推荐</view>
            <button open-type="share" class="absolute w-full h-full opacity-0"></button>
          </view>
        </div>
      </view>

    </view>

    <view class="relative">
      <view v-if="showDropDown" class="team-dropdown py-12 bg-white shadow-lg">
        <scroll-view scroll-y="true" style="max-height: 50vh">
          <view class="px-15">
            <view v-for="item in teams" :key="item.teamId" class="mb-10 p-10 flex items-center bg-gray rounded-sm"
              @click="select(item)">
              <view class="flex-shrink-0 mr-5 rounded-circle">
                <group-avatar :size="96" :avatarList="item.avatarList" />
              </view>
              <view class="w-0 flex-grow mr-5">
                <view class="mb-5 text-lg font-semibold text-dark">
                  {{ item.name }}
                </view>
                <view class="text-base text-gray leading-normal">
                  {{ item.licenseHospitalName || item.leaderCorp || item.corpName || '' }}
                </view>
              </view>
              <view class="flex">
                <image class="check-icon" :src="team && team.teamId === item.teamId
                  ? '/static/form/checked.svg'
                  : '/static/form/uncheck.svg'
                  ">
                </image>
              </view>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>
    <view v-if="team && team.teamTroduce" class="team-introduce-wrapper" @click="toTeamDetail">
      <view class="team-introduce flex items-center">
        <view class="team-introduce-border"></view>
        <view class="triangle-wrapper">
          <view class="team-triangle"></view>
        </view>
        <!-- <image class="laba-icon flex-shrink-0" src="/static/home/speaker-intro.png" mode="aspectFit"></image> -->
        <!-- <view class="introduce-text flex-grow line-clamp-3">
          团队介绍: {{ team.teamTroduce }}
        </view> -->
        <!-- <expandable-text textStyle="color:#333;font-size:26rpx" expandStyle="font-size:26rpx"
          :longText="team.teamTroduce + team.teamTroduce" :line="3" :lineHeight="36" expandText="展开" foldText="收起" /> -->
        <!-- <multi-lines-text :line="3" :text="team.teamTroduce + team.teamTroduce" :showButton="true" expandText="展开"
          collapseText="收起" buttonTextColor="#333" /> -->
        <zh-read-more :text="team.teamTroduce" :textStyle="{ color: '#333', 'font-size': '28rpx' }"
          :unfoldTextStyle="{ 'font-size': '28rpx', 'color': '#0074ff' }"
          :packupTextStyle="{ 'font-size': '28rpx', 'color': '#0074ff' }" />
      </view>
    </view>
    <!-- <view v-if="qrcode && qrcode.guide" class="team-introduce-wrapper">
      <view class="team-introduce flex items-center">
        <view class="team-introduce-border"></view>
        <view class="triangle-wrapper">
          <view class="team-triangle"></view>
        </view>
        <image class="laba-icon flex-shrink-0" src="/static/home/speaker-intro.png" mode="aspectFit"></image>
        <view class="introduce-text flex-grow line-clamp-2">
          {{ qrcode.guide }}
        </view>
      </view>
    </view> -->
  </view>
  <view v-if="showDropDown" class="mask" @click="showDropDown = false"></view>
</template>
<script setup>
import { computed, ref, onMounted, watch } from "vue";

import groupAvatar from "@/components/group-avatar.vue";
// import ExpandableText from "@/components/expandable-text/expandable-text.vue"
// import MultiLinesText from "@/components/multi-lines-text/multi-lines-text.vue";
import zhReadMore from "@/components/zh-readMore/zh-readMore.vue";
const statusBarHeight = ref("50px");
const menuButtonInfo = ref(null);
const showDropDown = ref(false);

const emits = defineEmits(["changeTeam"]);
const props = defineProps({
  team: {
    type: Object,
    default: () => ({}),
  },
  customers: {
    type: Array,
    default: () => [],
  },
  teams: {
    type: Array,
    default: () => [],
  },
});

const currentTeam = computed(() =>
  props.teams.find((i) => props.team && i.teamId === props.team.teamId)
);

const qrcode = computed(() => {
  const qrcodes = props.team && Array.isArray(props.team.qrcodes) ? props.team.qrcodes : [];
  return qrcodes[0] || ''
})
const teamStyle = computed(() => `max-width:${props.teams.length ? 'calc(100% - 60rpx)' : '100%'}`)
const firstCustomer = computed(() => props.customers && props.customers[0] ? props.customers[0] : {})

function select(team) {
  emits("changeTeam", team);
  showDropDown.value = false;
}

function toTeamDetail() {
  if (props.team && props.team.teamId) {
    uni.navigateTo({
      url: `/pages/team/team-detail?firstCustomerId=${firstCustomer.value._id || ''}&teamId=${props.team.teamId}&corpId=${props.team.corpId}&corpName=${encodeURIComponent(props.team.corpName || '')}`
    });
  }
}

watch(
  () => props.teams,
  (teams) => {
    if (
      teams.length &&
      !(
        currentTeam.value &&
        teams.some((i) => i.teamId === currentTeam.value.teamId)
      )
    ) {
      emits("changeTeam", teams[0]);
    }
  }
);

onMounted(() => {
  const win = uni.getWindowInfo();
  if (win && win.statusBarHeight > 0) {
    statusBarHeight.value = win.statusBarHeight + "px";
  }
  menuButtonInfo.value = uni.getMenuButtonBoundingClientRect();
});
</script>
<style scoped>
.status-bar {
  background: transparent;
}

.header-bar {
  background: transparent;
}

.laba-icon {
  width: 48rpx;
  height: 48rpx;
  margin-left: 12rpx;
  margin-right: 12rpx;
}

.team-name {
  color: #ffffff;
  font-size: 38rpx;
  font-style: normal;
  font-weight: 600;
  line-height: normal;
}

.switch-icon {
  width: 40rpx;
  height: 40rpx;
}

.switch-btn {
  padding: 4rpx;
}

.ml-10 {
  margin-left: 12rpx;
}

.team-introduce-wrapper {
  padding: 0 30rpx;
  /* margin-top: 12rpx; */
  margin-bottom: 30rpx;
  position: relative;
  z-index: 2;
}

.team-introduce {
  width: 690rpx;
  min-height: 111rpx;
  box-sizing: border-box;
  background: linear-gradient(186deg,
      rgba(255, 255, 255, 0.4) 13.34%,
      rgba(255, 255, 255, 0.6) 99.17%);
  border-radius: 16rpx;
  padding: 20rpx;
  position: relative;
  overflow: visible;
}

.team-introduce-border {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 1px solid #fff;
  border-radius: 16rpx;
  pointer-events: none;
  -webkit-mask-image: linear-gradient(to right, #000 0, #000 50rpx, transparent 50rpx, transparent 70rpx, #000 70rpx),
    linear-gradient(#000, #000);
  mask-image: linear-gradient(to right, #000 0, #000 50rpx, transparent 50rpx, transparent 70rpx, #000 70rpx),
    linear-gradient(#000, #000);
  -webkit-mask-size: 100% 1px, 100% 100%;
  mask-size: 100% 1px, 100% 100%;
  -webkit-mask-position: 0 0, 0 1px;
  mask-position: 0 0, 0 1px;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
}

.triangle-wrapper {
  position: absolute;
  top: -12rpx;
  left: 60rpx;
  width: 32rpx;
  height: 12rpx;
  overflow: hidden;
  z-index: 10;
  transform: translateX(-50%);
}

.team-triangle {
  position: absolute;
  left: 50%;
  bottom: -9rpx;
  width: 16rpx;
  height: 16rpx;
  background: linear-gradient(186deg,
      rgba(255, 255, 255, 0.4) 13.34%,
      rgba(255, 255, 255, 0.6) 99.17%);
  transform: translateX(-50%) rotate(45deg);
  border-top: 1px solid #fff;
  border-left: 1px solid #fff;
  box-sizing: border-box;
}

.introduce-text {
  color: #333;
  font-size: 26rpx;
  font-style: normal;
  font-weight: 400;
  line-height: 36rpx;
}

.line-clamp-2 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
}

.leading-relaxed {
  line-height: 1.6;
}

.check-icon {
  width: 40rpx;
  height: 40rpx;
}

.mask {
  position: fixed;
  z-index: 99;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

.z-3 {
  z-index: 101;
}

.team-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  z-index: 100;
  border-bottom-left-radius: 16rpx;
  border-bottom-right-radius: 16rpx;
}

.rounded-circle {
  border-radius: 50%;
}

.icon-share {
  width: 24rpx;
  height: 24rpx;
}

.opacity-0 {
  opacity: 0;
}
</style>