<template>
  <router-view></router-view>
  <van-tabbar route>
    <van-tabbar-item replace to="/hospitalService" icon="home-o"> 医疗服务 </van-tabbar-item>
    <van-tabbar-item replace to="/mine" icon="contact"> 个人中心 </van-tabbar-item>
    <van-tabbar-item replace to="/hospitalIntroduction" icon="hotel-o"> 医院信息 </van-tabbar-item>
  </van-tabbar>
</template>

<script>
import Cookies from 'js-cookie'
import { getOpenId, getPatientIdByOpenId } from '../api/patient-id-cards'
import store from '../store'
import { useRouter } from 'vue-router'
import { computed, onMounted } from 'vue'
// import { initWebSocket } from '../utils/websocket'
export default {
  name: 'Home',
  setup() {
    const router = useRouter()
    const cardSize = computed(() => {
      return store.state.patientCards.length
    })
    const toFirstTab = () => {
      // initWebSocket()
      let to = router.currentRoute.value.params.to
      if (!to) {
        to = 'hospitalService'
      }
      if (router.currentRoute.value.path.startsWith('/wxService')) {
        let pathInfo = to.split('_')
        if (pathInfo.length === 1) {
          router.push('/' + to)
        } else {
          router.push('/' + pathInfo[0] + '/' + pathInfo[1])
        }
      }
    }
    onMounted(() => {
      if (cardSize.value === 0) {
        const code = router.currentRoute.value.params.code
        if (code) {
          getOpenId(code).then((res) => {
            Cookies.set('openId', res)
            localStorage.openId = res
            getPatientIdByOpenId(res).then((cards) => {
              if (cards.length > 0) {
                store.commit('SET_PATIENTCARDS', cards)
              }
              toFirstTab()
            })
          })
        } else {
          toFirstTab()
        }
      } else {
        toFirstTab()
      }
    })
    return {}
  },
}
</script>