<template>

  <div class="page_home">
    <div>
      <router-view />
    </div>
    <div class="homeBottom">
      <van-tabbar class="tab" v-model="active">
        <router-link to="/homeIndex" class="link" replace>
          <van-tabbar-item>
            <i class="iconfont icon-zhuye iconOne"></i>
            <div>首页</div>
          </van-tabbar-item>
        </router-link>
        <router-link to="/newMy" replace>
          <van-tabbar-item>
            <i class="iconfont icon-wode iconOne"></i>
            <div>我的</div>
          </van-tabbar-item>
        </router-link>
      </van-tabbar>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import "../../styles/newStyle.scss";
import { setRem } from "@/libs/functionRem";
export default {
  data() {
    return {
      active: 0,
      value: "",
      hospitals: [
        {
          text: "",
          value: 0
        }
      ],
      searchVal: ""
    };
  },
  computed: {
    websocket() {
      return this.$store.state.user.websocket;
    }
  },
  methods: {
    getToken(orgId,adminUserId){
      axios.get('/api/index/gettoken/'+ orgId + '/'+ adminUserId
        ).then((res) => {
        // console.log('res',res.data)
        
        if(res.data.code == 0){
          let token = res.data.data.token
          localStorage.setItem("token",token)
          let http = 'wss://socket.sgjyun.com?token=' 
          // let http = 'ws://socket.szjkhd.com?token=' //测试
          // console.log(this.$store.state.user.websocket)
          this.$store.state.user.websocket = new ReconnectingWebSocket(
            http + token
          );
          this.$store.state.user.websocket.timeoutInterval = 3000;
          setInterval(() => {
            if (navigator.onLine == false) {
              this.showWsErr = true;
              this.wsErrMsg = "网络已断开，请检查网络后重新打开页面";
              // console.log('navigator',navigator)
              let http = 'wss://socket.sgjyun.com?token=' 
              // let http = 'ws://socket.szjkhd.com?token=' //测试
              let token = localStorage.getItem("token")
              this.$store.state.user.websocket = new ReconnectingWebSocket(
                http + token
              );
            } else if (
              navigator.onLine == true &&
              this.websocket.readyState == 1
            ) {
              this.showWsErr = false;
            }
            this.websocket.send("2");
          }, 30000);
        }
        
      }).catch(error => {
        console.log(error)
          // window.location.href = error.response.data.redirect;
        });
    }
  },
  created() {
    const id = this.$route.query.id;
    // this.getAllOrgName(id);
    setRem();
    let orgId = parseInt(sessionStorage.getItem("org_id"));
    let admin_user_id = parseInt(sessionStorage.getItem("admin_user_id"));
    if(this.$store.state.user.websocket == null){
      this.getToken(orgId,admin_user_id)
    }
    
  },
  mounted() {
    let currenttabbar = window.location.hash.split("/")[1];
    if (currenttabbar === "homeIndex") {
      this.active = 0;
    } else if (currenttabbar === "newMy") {
      this.active = 1;
    }

  }
};
</script>

<style lang="scss">
.page_home {
  height: 100%;
  display: flex;
  flex-direction: column;
  > div:first-child {
    flex: 1;
    overflow-y: auto;
  }
  .homeBottom {
    height: 3.125rem;
    padding-bottom: env(safe-area-inset-bottom);
  }
  ::-webkit-scrollbar {
    width: 0;
  }
  .van-tabbar-item {
    height: 100%;
  }
  .van-tabbar-item__text {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
  }
  .van-tabbar {
    display: flex;
    justify-content: space-around;
    border-top: 1px solid #e5e5e5;
  }
  .iconOne {
    font-size: 1.25rem;
  }
  .van-tabbar-item--active {
    .iconfont {
      color: #1989fa;
    }
  }
}
</style>
