<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device === 'mobile' && sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <div></div>
    <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 v-if="showSettings">
        <settings />
      </right-panel>
    </div>
  </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'

export default {
  name: 'Layout',
  components: {
    AppMain,
    Navbar,
    RightPanel,
    Settings,
    Sidebar,
    TagsView
  },
  mixins: [ResizeMixin],
  computed: {
    ...mapState({
      userId: state => state.user.userId,
      sidebar: state => state.app.sidebar,
      device: state => state.app.device,
      showSettings: state => state.settings.showSettings,
      needTagsView: state => state.settings.tagsView,
      fixedHeader: state => state.settings.fixedHeader
    }),
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    }
  },
  watch: {
    $route: {
      handler(route) {
        const { meta, path } = route;
        if (meta && meta.menuInfo) {
          this.$store.dispatch('BtnPower', meta.menuInfo.childrenBtn)
        }
      },
      immediate: true
    },
  },
  methods: {
    handleClickOutside() {
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
    },
    canvas(dom, txt) {
      if (!dom) return false;
      let length = txt?.length * 50 > 247 ? txt?.length * 50 : 247; // 根据内容生成画布大小，20代表比例
      let canvas = document.createElement('canvas');
      canvas.id = 'myCanvas'
      canvas.width = length;
      canvas.id = 'myCanvas'
      canvas.height = 150;
      canvas.style.display = 'none';
      document.body.appendChild(canvas);
      let context = canvas.getContext('2d');
      canvas.style.letterSpacing = 3 + 'px';
      context.font = '30px "PingFangSC", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif';
      context.fillStyle = 'rgba(255,0,0,0.06)';
      context.rotate((-13 * Math.PI) / 80); // 画布里面文字的旋转角度
      context.fillText(txt, (length - txt.length * 13) / 2, length / 2 + 20); // 文字的位置
      dom.style.backgroundImage = `url(${canvas.toDataURL('image/png')})`;
      return;
    }
  },
  created() {
    // 刷新页面后重新从服务器加载用户信息
    this.$store.dispatch('GetInfo').then(res => {
      let div = document.createElement('div');
      div.style = `width: 100%; height: 100%; position: fixed; left: 0px; top: 0px; z-index: 10000; pointer-events: none; background-color:rgba(0,0,0,.03); background-repeat: round;`;
      this.$nextTick(() => {
        let main = document.getElementById('app');
        if (main) {
          main.appendChild(div);
          this.canvas(div, res.data.userInfo.nickname);
          let myCanvas = document.getElementById('myCanvas')
          document.body.removeChild(myCanvas)
        }
      })
    });
  }
}
</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% - #{$sideBarWidth});
  transition: width 0.28s;
}

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

.mobile .fixed-header {
  width: 100%;
}
</style>
