<template>
  <div class="layout">
    <empty-layout v-if="fullScreenState" />
    <div
      v-else
      class="eip-front-wrapper"
      :class="classObj"
      :style="{
        '--containerBgColor': variables[`${style}-container-background`],
        '--topBarBgColor': variables[`${style}-top-bar-background`],
      }"
    >
      <div
        v-if="'horizontal' === layout"
        class="layout-container-horizontal"
        :class="{
          fixed: header === 'fixed',
          'no-tabs-bar': tabsBar === 'false' || tabsBar === false,
        }"
      >
        <div :class="header === 'fixed' ? 'fixed-header' : ''">
          <ht-top-bar />
          <div
            v-if="tabsBar === 'true' || tabsBar === true"
            :class="{ 'tag-view-show': tabsBar }"
          >
            <div class="app-main">
              <ht-tabs-bar />
            </div>
          </div>
        </div>
        <div class="app-main main-padding horizontal-container">
          <ht-app-main />
        </div>
      </div>
      <div
        v-else
        class="layout-container-vertical"
        :class="{
          fixed: header === 'fixed',
          'no-tabs-bar': tabsBar === 'false' || tabsBar === false,
        }"
      >
        <div
          v-if="device === 'mobile' && collapse === false"
          @click="handleFoldSideBar"
        />
<!--                  class="mask"-->
        <ht-side-bar />
        <div class="app-main" :class="collapse ? 'is-collapse-main' : ''">
          <div :class="header === 'fixed' ? 'fixed-header' : ''">
            <ht-nav-bar />
            <ht-tabs-bar v-if="tabsBar === 'true' || tabsBar === true" />
          </div>
          <ht-app-main />
        </div>
      </div>
      <el-backtop />
    </div>
  </div>
</template>

<script>
  import { mapActions, mapGetters } from 'vuex'
  import EmptyLayout from './EmptyLayout.vue'
  import { tokenName } from '@/config'
  import variables from '@/styles/variables.scss'
  export default {
    name: 'Layout',
    components: { EmptyLayout },
    data() {
      return {
        oldLayout: '',
        portalCollapse: true,
      }
    },
    computed: {
      ...mapGetters({
        layout: 'settings/layout',
        tabsBar: 'settings/tabsBar',
        collapse: 'settings/collapse',
        header: 'settings/header',
        device: 'settings/device',
        fullScreenState: 'routes/fullScreenState',
        style: 'settings/style',
      }),
      classObj() {
        return {
          mobile: this.device === 'mobile',
        }
      },
      variables() {
        return variables
      },
    },
    beforeMount() {
      this.$root.$on('resize', this.handleResize)
    },
    // beforeDestroy() {
    // },
    created() {},
    mounted() {
      this.oldLayout = this.layout
      const userAgent = navigator.userAgent
      if (userAgent.includes('Juejin')) {
        this.$baseAlert('不支持在掘金内置浏览器演示，请更换其他浏览器。')
      }
      const isMobile = this.handleIsMobile()
      if (isMobile) {
        if (isMobile) {
          //横向布局时如果是手机端访问那么改成纵向版
          this.$store.dispatch('settings/changeLayout', 'vertical')
        } else {
          this.$store.dispatch('settings/changeLayout', this.oldLayout)
        }
        this.$store.dispatch('settings/toggleDevice', 'mobile')
        setTimeout(() => {
          this.$store.dispatch('settings/foldSideBar')
        }, 2000)
      } else {
        this.$store.dispatch('settings/openSideBar')
      }
      this.$nextTick(() => {
        window.addEventListener(
          'storage',
          (e) => {
            if (e.key === tokenName || e.key === null) window.location.reload()
            if (e.key === tokenName && e.value === null)
              window.location.reload()
          },
          false
        )
      })
    },
    methods: {
      ...mapActions({
        handleFoldSideBar: 'settings/foldSideBar',
      }),
      handleIsMobile() {
        return document.body.getBoundingClientRect().width - 1 < 992
      },
      handleResize() {
        if (!document.hidden) {
          const isMobile = this.handleIsMobile()
          if (isMobile) {
            //横向布局时如果是手机端访问那么改成纵向版
            this.$store.dispatch('settings/changeLayout', 'vertical')
          } else {
            this.$store.dispatch('settings/changeLayout', this.oldLayout)
          }

          this.$store.dispatch(
            'settings/toggleDevice',
            isMobile ? 'mobile' : 'desktop'
          )
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  @mixin fix-header {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: $base-z-index - 2;
    width: 100%;
    overflow: hidden;
  }
  .layout {
    height: 100%;
  }
  .eip-front-wrapper {
    position: relative;
    width: $base-full-width;
    height: $base-full-height;

    .layout-container-horizontal {
      position: relative;
      height: calc(100% - #{$base-top-bar-height} - #{$base-tabs-bar-height});
      // overflow-y: auto;
      &.fixed {
        padding-top: calc(#{$base-top-bar-height} + #{$base-tabs-bar-height});
      }

      &.fixed.no-tabs-bar {
        padding-top: $base-top-bar-height;
      }

      ::v-deep {
        .app-main {
          width: $base-full-width;
          .tabs-bar-container {
            padding-right: 0;
          }
        }

        .fixed-header {
          @include fix-header;
        }

        .tag-view-show {
          background: var(--topBarBgColor);
          margin-top: 1px;
          box-shadow: $base-box-shadow;
        }

        .nav-bar-container {
          .fold-unfold {
            display: none;
          }
        }

        .main-padding {
          .app-main-container {
            margin-top: $base-content-margin;
            background: var(--containerBgColor);
          }
        }
      }
    }

    .layout-container-vertical {
      position: relative;
      height: calc(100% - #{$base-top-bar-height} - #{$base-tabs-bar-height});
      .mask {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: $base-z-index - 1;
        width: 100%;
        height: 100vh;
        overflow: hidden;
        background: #000;
        opacity: 0.5;
      }

      &.fixed {
        padding-top: calc(#{$base-nav-bar-height} + #{$base-tabs-bar-height});
      }

      &.fixed.no-tabs-bar {
        padding-top: $base-nav-bar-height;
      }

      .app-main {
        position: relative;
        height: 100%;
        overflow: auto;
        margin-left: $base-left-menu-width;
        transition: $base-transition;
        //隐藏滚动条
        &::-webkit-scrollbar {
          width: 0;
          background-color: transparent;
        }
        /*火狐下隐藏滚动条*/
        scrollbar-width: none;
        ::v-deep {
          .fixed-header {
            @include fix-header;

            left: $base-left-menu-width;
            width: $base-right-content-width;
            box-shadow: $base-box-shadow;
            transition: $base-transition;
          }

          .nav-bar-container {
            position: relative;
            box-sizing: border-box;
          }

          .tabs-bar-container {
            box-sizing: border-box;
          }

          .app-main-container {
            width: calc(100% - #{$base-content-margin}* 2);
            height: calc(
              100vh - #{$base-top-bar-height} - #{$base-tabs-bar-height} - #{$base-content-margin}*
                2
            );
            overflow-y: auto;
            margin: $base-content-margin;
            background: var(--containerBgColor);
            border-radius: $base-border-radius;

            //隐藏滚动条
            &::-webkit-scrollbar {
              width: 0;
              background-color: transparent;
            }
            /*火狐下隐藏滚动条*/
            scrollbar-width: none;
          }
        }

        &.is-collapse-main {
          width: calc(100% - #{$base-left-menu-width-min});
          margin-left: $base-left-menu-width-min;

          ::v-deep {
            .fixed-header {
              left: $base-left-menu-width-min;
              width: calc(100% - 65px);
            }
          }
        }
      }
    }
    .horizontal-container {
      display: flex;
      height: 100%;
      .app-main-container {
        margin: auto;
        width: 92% !important;
        overflow-y: auto;
        height: calc(100% - #{$base-content-margin});
        //隐藏滚动条
        &::-webkit-scrollbar {
          width: 0;
          background-color: transparent;
        }
        /*火狐下隐藏滚动条*/
        scrollbar-width: none;
      }
    }

    ::v-deep .data_view__title {
      border-bottom: 1px solid #f2f2f2;
      padding-bottom: 5px;
      margin-top: 0;
    }
    .layout-container-vertical {
      .app-main {
        .app-main-container {
          // margin: 24px;
          ::v-deep .base-main {
            background-color: #f9fafc;
          }
        }
      }
    }
    ::v-deep .data_view {
      // height: 770px;
      height: 100%;
      section.el-container.data-preview-wrap {
        height: 100%;
        // padding: 5px;
      }
    }
    ::v-deep .template-preview-container.app-main-height {
      height: 90%;
    }

    /* 手机端开始 */
    &.mobile {
      ::v-deep {
        .el-pager,
        .el-pagination__jump {
          display: none;
        }

        .layout-container-vertical {
          .el-scrollbar.side-bar-container.is-collapse {
            width: 0;
          }

          .app-main {
            width: 100%;
            margin-left: 0;
          }
        }

        .app-main {
          .fixed-header {
            left: 0 !important;
            width: 100% !important;
          }
        }
      }
    }

    /* 手机端结束 */
  }
</style>
