<template>
  <div :class="classObj" class="app-wrapper" :style="{ '--current-color': theme }">
    <div :class="{ hasTagsView: needTagsView, sidebarHide: sidebar.hide }" class="main-container">
      <div :class="{ 'fixed-header': fixedHeader }">
        <navbar />
        <sidebar ref="sidebar" class="sidebar-container" v-if="true" :style="{
            width: sidebar.opened
              ? '0px'
              : childrenType
              ? childrenWidth
              : '128px',
          }" @openChildren="openChildren" @changeWidth="changeWidth" />
        <!-- <tags-view v-if="needTagsView" /> -->
      </div>
      <app-main />
      <right-panel>
        <settings />
      </right-panel>
    </div>
  </div>
</template>

<script>
  import router from "@/router";
  import RightPanel from "@/components/RightPanel";
  import notFound from "@/views/error/404";
  import {
    AppMain,
    Navbar,
    Settings,
    Sidebar,
    TagsView
  } from "./components";
  import ResizeMixin from "./mixin/ResizeHandler";
  import {
    mapState
  } from "vuex";
  import variables from "@/assets/styles/variables.scss";

  export default {
    name: "Layout",
    components: {
      AppMain,
      Navbar,
      RightPanel,
      Settings,
      Sidebar,
      TagsView,
      notFound,
    },
    mixins: [ResizeMixin],
    computed: {
      ...mapState({
        theme: (state) => state.settings.theme,
        sideTheme: (state) => state.settings.sideTheme,
        sidebar: (state) => state.app.sidebar,
        device: (state) => state.app.device,
        notFound: (state) => state.app.notFound,
        conciseMenu: (state) => state.settings.conciseMenu,
        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",
        };
      },
      variables() {
        return variables;
      },
    },
    data() {
      return {
        childrenType: false,
        childrenWidth: "1200px",
      };
    },
    watch: {
      sidebar: {
        handler: function (val, oldVal) {
          console.log(val,this.childrenType);

          if (!val.opened && this.childrenType) {
            this.$nextTick(() => {
              let newWidth = this.$refs.sidebar.childrenWidth + 128 + "px";
              this.childrenWidth = newWidth;
            });
          }
        },
        deep: true,
      },
    },
    mounted() {
      this.getMenu();
      this.childrenWidth = this.$refs.sidebar.childrenWidth;
      // console.log(this.childrenWidth )
    },
    methods: {
      openChildren(val) {
        let newWidth = this.$refs.sidebar.childrenWidth + 128 + "px";
        this.childrenWidth = newWidth;
        this.childrenType = val;
      },
      changeWidth(width) {
        this.childrenWidth = width + 128 + "px";
      },
      handleClickOutside() {
        this.$store.dispatch("app/closeSideBar", {
          withoutAnimation: false,
        });
      },
      getMenu() {
        //动态路由注释
        // this.$store.dispatch("GenerateRoutes").then((accessRoutes) => {
        //   // console.log(accessRoutes)
        //   // 根据roles权限生成可访问的路由表
        //   for (let i = 0, length = accessRoutes.length; i < length; i += 1) {
        //     const element = accessRoutes[i];
        //     router.addRoutes(accessRoutes); // 动态添加可访问路由表
        //   }
        //   localStorage.setItem("routeList", JSON.stringify(accessRoutes));
        // });
      },
    },
  };

</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%;
    padding: 0 8px;
    background: #fafafa;

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

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

  .defaultMuneStyle {
    width: calc(100% - 160px);
    margin-left: 160px;
    height: 100%;
    // padding:10px;
  }

  .conciseMenuStyle {
    width: calc(100% - 70px);
    margin-left: 70px;
    padding: 10px;
    height: 100%;
  }

  .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);
  // }

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

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

  .webConcat {
    position: fixed;
    top: 50px;
    height: calc(100vh - 50px);
    width: 100%;
  }

</style>
