
<template>
  <div id="app">
    <div class="c-h100">
      <transition name="fade" appear>
        <keep-alive>
          <router-view></router-view>
        </keep-alive>
      </transition>
    </div>

    <!-- 弹出层 -->
    <modals-container/>
  </div>
</template>

<script>
import videoChat from "@/components/videoChat/video-chat";
import storage from "./assets/js/storage";
import ajaxUrl from "./assets/js/ajaxurl";
import SocketService from "./assets/js/apis/api-p2psocket";
import store from "./store";

let loginer = storage.getItem(storage.API_LOGIN_DOCTOR);
let userAgent = "";
let agentStr = "";
if (loginer) {
  userAgent = {
    uid: loginer.uid,
    id: loginer.id,
    token: loginer.token,
    platform: 4,
    imei: storage.getItem(storage.IMEI)
  };
}

export default {
  data() {
    return {
      // authLoginUrl: authLoginUrl,
      patient: null,
      teamCode: null,
      selectTapObj: null,
      signMsg: null,
      delSignMsg: null,
      isBook: false,
      isServer: false,
      isArticle: false,
      isSpecialMsg: false,
      isDelSign: false,
      isVideoChat: false,
      chatMembers: []
    };
  },
  mounted() {
    let socket2 = SocketService.loginLocalSocket();
    // let socket2 = new WebSocket("ws://localhost:30001/socket.io/?EIO=3&transport=websocket&sid=EP0Cejs68HHAXmcGn19z&t=MTIiRCK");

    let vm = this;

    //底部托盘消息提醒
    socket2.onmessage = function(message) {
      console.log(message);
    };
    socket2.on("showChatWindow", data => {
      console.log("local socket = ", data);
      if (!vm.isVideoChat) {
        vm.$root.bus.$emit("showChatWindow", {
          chatMembers: [],
          sessionId: data.sessionId
        });
      }
    });

    socket2.on("syncServerHost", data => {
      console.log("syncServerHost", data);
      ajaxUrl.socketUrl = "https://" + data.server_host + ":9000";
      ajaxUrl.imServer =
        "https://" + data.server_host + ":" + data.port + "/api/v2";
      ajaxUrl.videoScoketUrl = data.server_host + ":9000/groupcall";
      window.localStorage.setItem(
        "serverUrl",
        "https://" + data.server_host + ":" + data.port
      );
      vm.loginIM();
      this.$router.replace("/message");
    });

    // 错误提醒
    socket2.on("error", function(data) {
      console.log("socket2 Error");
    });
    // scoket连接成功
    socket2.on("ack", function(data) {});

    this.$root.bus.$on("app:imlogout", function() {
      socket.close();
    });
    this.$root.bus.$on("sel-tap", function(arg) {
      vm.patient = arg.info.code;
      vm.teamCode = arg.info.jtAdminTeam;
      vm.selectTapObj = arg.tapObj;
      vm.signCode = arg.info.signCode;
    });

    //视频聊天窗口弹出
    this.$root.bus.$on("showChatWindow", value => {
      this.chatMembers = value;
      this.isVideoChat = true;

      this.$modal.show(
        videoChat,
        {
          chatMembers: value.chatMembers,
          sessionId: value.sessionId,
          createChat: value.createChat || false
        },
        {
          draggable: true,
          clickToClose: false,
          width: "800px",
          height: "500px"
        }
      );
    });
  },
  methods: {
    loginIM() {
      let socket = SocketService.mesPushLogin(),
        str = "",
        vm = this;
      // 实时收到消息
      socket.on("message", data => {
        let type = null;
        switch (Number(data.session_type)) {
          case 0:
            type = "0";
            break;
          case 1:
          case 8:
            type = "1";
            break;
          case 2:
          case 3:
          case 4:
            type = "2";
            break;
          default:
            type = null;
        }
        try {
          str = JSON.parse(data.data).path;
        } catch (error) {
          str = data.data;
        }
        let msg = "";
        try {
          let res = str.split(","),
            arr,
            len;
          if (res.length > 2) {
            arr = res[1].split(".");
          } else {
            arr = res[0].split(".");
          }
          len = arr.length;
          switch (arr[len - 1]) {
            case "mp3":
              msg = "[语音]";
              break;
            case "mov":
            case "mp4":
            case "avi":
            case "wmv":
            case "asf":
            case "rm":
              msg = "[视频]";
              break;
            case "jpg":
            case "jpeg":
            case "png":
            case "gif":
            case "bmp":
              msg = "[图片]";
              break;
            default:
              msg = data.data;
          }
        } catch (error) {
          msg = data.data;
        }
        // this.$electron.ipcRenderer.send('app:flashFrame', true)
        // 托盘图标闪烁
        // this.$electron.ipcRenderer.send('mainWindow:flashTray', {tip: '您有新的消息'})
        this.$root.bus.$emit("msgRenovate");
        let notice = new Notification("您有新的消息", {
          dir: "auto",
          lang: "zh-CN",
          // tag: 'msgNotice',
          body: msg
        });
        notice.onclick = () => {
          // this.$electron.ipcRenderer.send('app:flashFrame', false)
          // 停止托盘图标闪烁
          // this.$electron.ipcRenderer.send('mainWindow:stopFlashTray', {tip: ''})
          // 如果通知消息被点击,通知窗口将被激活
          // this.$electron.ipcRenderer.send('mainWindow:focus')
          this.$root.bus.$emit("menuIndex", true);
          this.$root.bus.$emit("scrollCon", true);
          this.$router.push({ path: "message" });
          this.$root.bus.$emit("msgRenovate", type);
        };
      });
      // 错误提醒
      socket.on("error", function(data) {
        alert("Error");
      });
      // scoket连接成功
      socket.on("ack", function(data) {});
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
/* CSS */
@import "assets/sass/style.scss";
html,
body,
#app {
  height: 100%;
  min-width: 750px;
}
.c-h100 {
  height: 100%;
}
.c-width-50 {
  width: 50px;
}
.win-ctrl-wrapper {
  width: 120px;
  float: right;
}
.c-app-drag {
  -webkit-app-region: drag;
}
.notify .notify-alert h2.notice-title {
  padding: 0;
  text-align: center;
}
.mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #111;
  opacity: 0.1;
  z-index: 9;
}
.sel-tap {
  overflow-y: scroll;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 500px;
  height: 400px;
  margin: auto;
  border: 1px solid #e1e1e1;
  background-color: #fff;
  z-index: 10;
}
.sel-article {
  width: 80%;
  height: 80%;
}
.sel-tap:hover::-webkit-scrollbar-thumb {
  background-color: #dcdce1;
  border-radius: 5px;
}
.modify-tit {
  position: fixed;
  height: 40px;
  width: 460px;
  padding: 0 10px 0 30px;
  border-bottom: 1px solid #e1e1e1;
  line-height: 40px;
  background: #fff url("./assets/images/shezi_tankuang_icon.png") no-repeat 10px
    center;
}
.c-w80 {
  width: calc(80% - 40px);
}
.close {
  float: right;
  width: 12px;
  height: 25px;
  background: url("./assets/images/guanbi_icon.png") no-repeat 0 16px;
}
.w400 {
  width: 400px;
}
.w360 {
  width: 360px;
}
.h440 {
  height: 440px;
}
</style>
