<template>
  <div :class="classObj" class="app-wrapper" :style="{'--current-color': theme}">
    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside"/>
    <sidebar class="sidebar-container"/>
    <div :class="{hasTagsView:needTagsView}" class="main-container">
      <div :class="{'fixed-header':fixedHeader}">
        <navbar />
        <tags-view v-if="needTagsView" />
      </div>
      <app-main />
      <right-panel>
        <settings />
      </right-panel>

      <div class="qw-im" @click="() => {this.qw.open = !this.qw.open}">
        <el-badge :value="totalUnreadCount" :max="99" :hidden="totalUnreadCount < 1">
          <img alt="" class="qw-im-img" src="../assets/image/chat_icon44.png"/>
          <div class="qw-im-text">IM聊天</div>
        </el-badge>
      </div>
    </div>

    <el-dialog append-to-body
               :width="qw.isMaximized ? '100vw' : '90vw'"
               custom-class="im-dialog"
               :fullscreen="qw.isMaximized"
               :visible.sync="qw.open"
               :style="{visibility: !qw.initLoad ? 'hidden' : 'unset'}"
               :modal="qw.initLoad"
               :title="qw.title">
      <div class="qw-im-content">
        <QwIM :showQw="qw.open" :isMaximized="qw.isMaximized" @close="() => qw.open = false"/>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import RightPanel from '@/components/RightPanel'
import { AppMain, Navbar, Settings, Sidebar, TagsView } from './components'
import ResizeMixin from './mixin/ResizeHandler'
import { mapState } from 'vuex'
import variables from '@/assets/styles/variables.scss'
import QwIM from '@/views/qw/qwChat/qq.vue'

export default {
  name: 'Layout',
  components: {
    AppMain,
    Navbar,
    RightPanel,
    Settings,
    Sidebar,
    TagsView,
    QwIM
  },
  mixins: [ResizeMixin],
  computed: {
    ...mapState({
      theme: state => state.settings.theme,
      sideTheme: state => state.settings.sideTheme,
      sidebar: state => state.app.sidebar,
      device: state => state.app.device,
      needTagsView: state => state.settings.tagsView,
      fixedHeader: state => state.settings.fixedHeader,
      companyUser:state => state.user.user,
      totalUnreadCount: state => state.qwIm.totalUnreadCount
    }),
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    },
    variables() {
      return variables;
    }
  },
  data() {
    return {
      qw: {
        open: false,
        title: '',
        isMaximized: false,
        initLoad: false
      }
    }
  },
  mounted() {
    if (!this.qw.initLoad) {
      this.qw.open = true
      this.$nextTick(() => {
        this.qw.open = false
        setTimeout(() => {
          this.qw.initLoad = true
        },  500)
      })
    }
  },
  methods: {
    handleClickOutside() {
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import "~@/assets/styles/mixin.scss";
  @import "~@/assets/styles/variables.scss";

  .app-wrapper {
    @include clearfix;
    position: relative;
    height: 100%;
    width: 100%;

    &.mobile.openSidebar {
      position: fixed;
      top: 0;
    }
  }

  .drawer-bg {
    background: #000;
    opacity: 0.3;
    width: 100%;
    top: 0;
    height: 100%;
    position: absolute;
    z-index: 999;
  }

  .fixed-header {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 9;
    width: calc(100% - #{$base-sidebar-width});
    transition: width 0.28s;
  }

  .hideSidebar .fixed-header {
    width: calc(100% - 54px)
  }

  .mobile .fixed-header {
    width: 100%;
  }

  ::v-deep .qw-im {
    z-index: 2000;
    position: fixed;
    right: 30px;
    bottom: 30px;
   width: 108px;
   height: 108px;
   text-align: center;
   background: linear-gradient( 135deg, #006CFF 0%, #68B1FF 100%);
   border-radius: 54px 54px 54px 54px;
   display: flex;
       align-items: center;
       justify-content: center;
       flex-direction: column;
    .qw-im-img {
      width: 44px;
    }
    .qw-im-text{
     font-weight: 600;
     font-size: 16px;
     color: #FFFFFF;
    }
    .el-badge__content {
      border: unset;
    }
  }
  ::v-deep .im-dialog {
    max-width: 100vw;
    max-height: 100vh;

    .el-dialog__header {
      display: none;
    }
    .el-dialog__body {
      padding: 0 !important;
    }
     &:not(.is-fullscreen) {
      top: 50% !important;
        transform: translateY(-50%) !important;
        margin-top: 0 !important;
    }
  }

  .qw-im-content {
    width: 100%;
    height: 100%;
    /* 可选：让内容区自动撑满弹窗 */
    display: flex;
    flex-direction: column;
  }
</style>
