<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
      <keep-alive :include="cachedViews">
        <router-view :key="key" />
      </keep-alive>
    </transition>
    <span class="footer">
      Copyright © 2023 厦门市巨龙信息科技有限公司  建议使用1280*768以上分辨率
    </span>
    <section>
      <!-- <div class="">在线留言</div> -->
      <information ref="Information" />
    </section>
  </section>
</template>

<script>
import { fetchRealCount } from '@/api/message'
import { isNull } from '@/utils/convert'

import Information from '@/views/home/Information'

let sseClient

export default {
  name: 'AppMain',
  components: {
    Information
  },
  computed: {
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    },
    key() {
      return this.$route.path
    }
  },
  mounted() {
    this.getMessageCount()
    this.connect()
  },
  beforeDestroy() {
    if (sseClient) {
      sseClient.disconnect()
    }
  },
  methods: {
    getMessageCount() {
      fetchRealCount().then(res => {
        if (!isNull(res.data)) {
          this.$store.dispatch('message/setMessgeCount', res.data)
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '获取消息数量出错: ' + error.message
        })
      })
    },
    connect() {
      sseClient = this.$sse.create({
        url: process.env.VUE_APP_BASE_API + '/subscribe/' + this.$store.getters.idCard,
        format: 'json',
        withCredentials: false
        // forcePolyfill: true,
        // polyfillOptions: {
        //   headers: {
        //     'Authorization': 'Bearer ' + this.$store.getters.token
        //   }
        // }
      })
      sseClient.on('error', (e) => {
        console.error('lost connection or failed to parse!', e)

      // If this error is due to an unexpected disconnection, EventSource will
      // automatically attempt to reconnect indefinitely. You will _not_ need to
      // re-add your handlers.
      })

      sseClient.on('message', this.handleMessage)

      sseClient.connect()
        .then(sse => {
          console.log('We\'re connected!')
        })
        .catch((err) => {
        // When this error is caught, it means the initial connection to the
        // events server failed.  No automatic attempts to reconnect will be made.
          console.error('Failed to connect to server', err)
        })
    },
    handleMessage(message) {
      console.log(message)
      if (message && message.recordType) {
        this.$store.dispatch('message/addNewMessage', message)
        if (message.recordType === 1) {
          this.$refs['Information'].open(message)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";
.app-main {
  min-height: calc(100vh - #{$headerHeight});
  width: 100%;
  position: relative;
  // overflow: auto;
  padding-bottom: $footerHeight;

  &>div:not(.home-container) {
    height: calc(100vh - #{$layoutHeight});
    padding: 10px 0 0 10px;
  }
}

.hasTagsView {
  .app-main {
    min-height: calc(100vh - #{$headerHeight} - #{$tagHeight});
  }

  .fixed-header+.app-main {
    min-height: calc(100vh - #{$headerHeight});
    padding-top: $tagHeight;
  }
}

.footer {
  display: block;
  width: 100%;
  height: $footerHeight;
  line-height: $footerHeight;
  font-size: 12px;
  color: rgba(0,0,0,0.45);
  text-align: center;
  position: absolute;
  left: 0;
  bottom: 0;
}
</style>

<style lang="scss">
// fix css style bug in open el-dialog
.el-popup-parent--hidden {
  .fixed-header {
    padding-right: 15px;
  }
}
</style>
