<template>
  <view class="flex flex-col justify-center items-center h-full">
    <image class="flash-logo" src="/static/logo-plain.png" />
  </view>
</template>
<script setup>
import { storeToRefs } from "pinia";
import useGuard from "@/hooks/useGuard.js";
import useAccountStore from "@/store/account.js";
import api from '@/utils/api';
import { toast } from '@/utils/widget';

const { useLoad } = useGuard()
const { account, doctorInfo } = storeToRefs(useAccountStore());

async function getTeam(teamId) {
  if (teamId) {
    const res = await api('getTeamData', { teamId, corpId: account.value.corpId });
    if (res && res.data) {
      return toJoinTeam(teamId)
    } else {
      await toast(res?.message || '获取团队信息失败')
    }
  } else {
    await toast('获取团队信息失败')
  }
}

async function toJoinTeam(teamId) {
  //  已注册
  if (doctorInfo.value && doctorInfo.value.userid) {
    const data = {
      teamId,
      corpId: account.value.corpId,
      id: doctorInfo.value._id,
      userId: doctorInfo.value.userid
    }
    const res = await api('joinTheInvitedTeam', data);
    if (res && res.success) {
      await toast('加入团队成功');
      return uni.switchTab({
        url: '/pages/home/work-home'
      })
    } else {
      await toast(res?.message || '加入团队失败')
    }
  } else {
    uni.redirectTo({
      url: `/pages/work/profile?type=joinTeam&teamId=${teamId}`
    })
  }

}


useLoad(options => {
  const href = typeof options.q === 'string' ? decodeURIComponent(options.q) : '';
  const [, url = ''] = href.split('?');
  const data = url.split('&').reduce((acc, cur) => {
    const [key, value] = cur.split('=');
    acc[key] = value;
    return acc;
  }, {})
  if (data.type === 'inviteTeam') {
    getTeam(data.teamId)
  }
})
</script>
<style>
.flash-logo {
  width: 200rpx;
  height: 200rpx;
  animation: flash 2.5s infinite;
}

@keyframes flash {
  0% {
    opacity: 1;
  }

  50% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}
</style>