<template>
  <!-- vue3.0配置 -->
  <div h-full>
    <!-- <loading v-if="isLoading" /> -->
    <router-view></router-view>
    <video-consult-dialog
      :visible="videoConsultStore.uiVisible"
      :order="videoConsultOrder"
      @close="videoConsultStore.closeUI()"
    />
  </div>
</template>
<script setup>
import { storeToRefs } from "pinia";
import { useScriptTag } from "@vueuse/core";
import { computed, defineAsyncComponent, onMounted, onUnmounted, onBeforeUnmount } from "vue";
import { memberStore } from "@/store/member";
import { isMobile } from "@/utils/common";
import { useImStore } from "@/store/im";
import { recordLogout } from "@/api/sendBeacon";
import { useVideoConsultStore } from "@/store/video-consult";
import videoConsultDialog from "@/views/hlw-hospital/chat-home/video-consult-dialog.vue";
const { imSDkInit, imLogin, kickedOutWatch, closeWindow, updateHlwDoctorOnlineStatus, onLogout, cancelKickedOutWatch } = useImStore();
const loading = defineAsyncComponent(() => import("@/views/loading/index.vue"));
const { logined, memberInfo } = storeToRefs(memberStore());

const videoConsultStore = useVideoConsultStore();
const videoConsultOrder = computed(() => videoConsultStore.currentOrder || videoConsultStore.uiOrder || {});
const isLoading = computed(() => {
  return isMobile() ? false : logined.value === "unlogin";
});

onMounted(async () => {
  if (!import.meta.env.DEV && import.meta.env.VITE_TCB_PATH === "yktPCDeploy") {
    const { scriptTag, load, unload } = useScriptTag("https://cdn.bootcdn.net/ajax/libs/vConsole/3.15.1/vconsole.min.js", () => new VConsole(), { manual: true });
    load();
  }
  imSDkInit();
  kickedOutWatch();
  window.recordExit = recordExit;
  window.onbeforeunload = function () {
    // recordExit()
    if (sessionStorage.getItem("doctorNo")) {
      updateHlwDoctorOnlineStatus("offline");
    }

  };
  if (sessionStorage.getItem("doctorNo")) {
    await imLogin();
    // 启动全局视频问诊信令监听（必须在 IM 登录成功后，确保医生切换页面后仍能处理患者拒绝等信令）
    videoConsultStore.startGlobalSignalWatch();
    window.onbeforeunload = function () {
      updateHlwDoctorOnlineStatus("offline");
    };
  }
  if (sessionStorage.getItem("onlineStatus") === "online") {
    updateHlwDoctorOnlineStatus("online");
  }
});

async function recordExit() {
  const data = {
    loginId: sessionStorage.getItem("loginId") || '69c37aa28337dc14236c3145',
    userId: memberInfo.value.userid || '68f83c313fe596619e37e644',
    corpId: sessionStorage.getItem("corpId"),
  }
  if (data.loginId && data.userId) {
    recordLogout(data, sessionStorage.getItem("accessToken"));
  }
  sessionStorage.clear();
}

onUnmounted(() => {
  cancelKickedOutWatch();
  // 停止全局视频问诊信令监听
  videoConsultStore.stopGlobalSignalWatch();
});
</script>
<style lang="scss">
@import "./styles/attr.css";
@import "./styles/common.scss";
@import "./styles/cover-element.css";
@import "./styles/var.css";

[p-15] {
  padding: 15px;
}

.w-100px {
  width: 100px;
}

.w-full {
  width: 100%;
}

.el-dialog__body {
  padding: 10px !important;
}

.selectMember .el-dialog__body {
  padding: 0px !important;
}

html,
body,
#app {
  margin: 0;
  height: auto;
}

#appView {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  height: 0;
  overflow: hidden;
  background-color: #f3f4f7;
  font-family: PingFang SC, Lantinghei SC, Microsoft YaHei, HanHei SC, Helvetica Neue, Open Sans, Arial, Hiragino Sans GB, 微软雅黑, STHeiti, WenQuanYi Micro Hei, SimSun, sans-serif;
  -webkit-tap-highlight-color: transparent;
  --el-text-color-regular: #666;

  .el-text--default {
    color: rgba(0, 0, 0, 0.9);
  }
}

[active-title-bar] {
  position: relative;
  margin-left: 10px;

  @at-root &::before {
    content: "";
    position: absolute;
    left: -10px;
    width: 4px;
    top: 4px;
    bottom: 3px;
    border-radius: 2px;
    background-color: #006eff;
  }
}
</style>
<style>
@import url(./assets/iconfont/icon.css);

.el-dialog-body-full-scroll {
  max-height: calc(85vh - 200px);
}

span.el-tag.el-tag--info {
  --el-tag-border-color: #bbb;
}

span.el-tag--large {
  padding: 0 20px;
  --el-tag-border-color: #006eff;
}

span.el-dialog__title {
  font-size: 18px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.9);
}

.el-dialog__header {
  border-bottom: 1px solid #eee;
}

footer.el-dialog__footer {
  padding-top: 15px;
  padding-bottom: 15px;
  box-shadow: 0 -1px 4px 0 rgba(54, 58, 80, 0.2);
}

.el-dialog {
  --el-dialog-border-radius: 8px !important;
}

.el-drawer__header {
  /* --el-drawer-padding-primary: 15px; */
  font-size: 16px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.9) !important;
  padding: 5px 15px !important;
  margin-bottom: 0 !important;
  border-bottom: 1px solid #eee;
}

.el-drawer__header .el-drawer__title {
  font-size: 15px;
}

body .el-table th.gutter {
  display: table-cell !important;
}

body .el-table colgroup.gutter {
  display: table-cell !important;
}

table {
  width: 100% !important;
}

/* table {
  width: 100% !important;
} */

.el-button.is-text:active {
  color: #fff;
}

svg {
  outline: none;
}

.el-form-item--autoheight .el-form-item__label {
  height: auto;
  font-size: 14px;
  padding-top: 9px;
  padding-bottom: 9px;
  line-height: 18px;
}

input::-webkit-credentials-auto-fill-button {
  display: none !important;
  visibility: hidden;
  pointer-events: none;
  position: absolute;
  /* 避免占用 input 元素额外的 padding，正常情况下存在 display: none!; 就可以了 */
  right: 0;
}
</style>
