<template>
  <a-layout :class="[device]" class="layout">
    <template v-if="layoutMode === 'sidemenu'">
      <a-drawer
        :closable="false"
        :visible="collapsed"
        :wrapClassName="'drawer-sider ' + navTheme"
        @close="() => (this.collapsed = false)"
        placement="left"
        v-show="device === 'mobile'"
        width="213"
        class="drawerSty"
      >
        <side-menu
          :collapsed="false"
          :collapsible="true"
          :company="company"
          :menus="menus"
          :theme="navTheme"
          @menuSelect="menuSelect"
          mode="inline"
        ></side-menu>
      </a-drawer>

      <side-menu
        :collapsed="collapsed"
        :collapsible="true"
        :company="company"
        :menus="menus"
        :theme="navTheme"
        mode="inline"
        v-show="device === 'desktop'"
      ></side-menu>
    </template>
    <!-- 下次优化这些代码 -->
    <template v-else>
      <a-drawer
        :closable="false"
        :visible="collapsed"
        :wrapClassName="'drawer-sider ' + navTheme"
        @close="() => (this.collapsed = false)"
        placement="left"
        v-if="device === 'mobile'"
        width="213"
      >
        <side-menu
          :collapsed="false"
          :collapsible="true"
          :company="company"
          :menus="menus"
          :theme="navTheme"
          @menuSelect="menuSelect"
          mode="inline"
        ></side-menu>
      </a-drawer>
    </template>

    <a-layout
      :class="[layoutMode, `content-width-${contentWidth}`]"
      :style="{
        paddingLeft:
          fixSiderbar && isDesktop() ? `${sidebarOpened ? 213 : 80}px` : '0'
      }"
    >
      <!-- layout header -->
      <global-header
        :collapsed="collapsed"
        :device="device"
        :company="company"
        :menus="menus"
        :mode="layoutMode"
        :theme="navTheme"
        @toggle="toggle"
      />

      <!-- layout content -->
      <a-layout-content
        :style="{
          marginLeft: ' 16px',
          height: 'calc(100vh - 65px)',
          scrollBehavior: 'smooth',
          marginRight: '16px',
          overflow: 'hidden',
          marginTop: fixedHeader ? '64px' : '14px'
        }"
      >
        <slot></slot>
      </a-layout-content>

      <!-- layout footer -->
      <a-layout-footer style="padding: 0px"></a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script>
import SideMenu from '@/components/menu/SideMenu'
import GlobalHeader from '@/components/page/GlobalHeader'
import { triggerWindowResizeEvent } from '@/util/util'
import { mapActions, mapState } from 'vuex'
import { mixin, mixinDevice } from '@/util/mixin.js'

export default {
  name: 'GlobalLayout',
  components: {
    SideMenu,
    GlobalHeader
  },
  mixins: [mixin, mixinDevice],
  data() {
    return {
      collapsed: false,
      menus: []
    }
  },
  computed: {
    ...mapState({
      // 主路由
      mainMenu: (state) => state.permission.addRouters,
      company: (state) => state.user.company
    })
  },
  watch: {
    sidebarOpened(val) {
      this.collapsed = !val
    }
  },
  created() {
    const root = this.mainMenu.find((item) => item.path === '/')
    this.menus = this.filterHidenRouter(root.children)
    console.info('main menu', this.mainMenu, this.menus)
    console.info('root', root)
  },
  methods: {
    ...mapActions(['setSidebar']),
    toggle() {
      this.collapsed = !this.collapsed
      this.setSidebar(!this.collapsed)
      triggerWindowResizeEvent()
    },
    menuSelect() {
      if (!this.isDesktop()) {
        this.collapsed = false
      }
    },
    filterHidenRouter(routes, init = []) {
      
      for (let i = 0; i < routes.length; i++) {
        let item = routes[i]
        if (item.hidden == true) {
          continue
        }
        let routerItem = {
          meta: {}
        }
        routerItem.path = item.path
        routerItem.name = item.name
        routerItem.meta.title = item.meta.title
        if (item.meta.icon) {
          routerItem.meta.icon = item.meta.icon
        }

        if (item.children) {
          routerItem.children = []
          this.filterHidenRouter(item.children, routerItem.children)
        }
        init.push(routerItem)
      }
      return init
    }
  }
}
</script>

<style lang="less">
body {
  // 打开滚动条固定显示
  // overflow-y: scroll;

  &.colorWeak {
    filter: invert(80%);
  }
}

//修改antd header 行高
.ant-layout-header {
  height: 50px !important;
  line-height: 50px !important;
}

.ant-layout.ant-layout-has-sider > .ant-layout,
.ant-layout.ant-layout-has-sider > .ant-layout-content {
  overflow: hidden;
}
.ant-layout-content {
  overflow: auto;
  .ant-spin-container {
    // min-width: 400px;
  }
}

.ant-card-body {
  padding: 5px !important;
}

//列表页样式调整
.ant-advanced-search-form {
  margin-bottom: 15px !important;
  padding: 10px 20px 10px 5px !important;
  background: #ffffff;
}

.ant-table-selection-column {
  min-width: 45px !important;
  width: 45px !important;
}

.cust-table-operator {
  float: right;
}

.cust-pagination {
  float: right;
  margin-top: -6px !important;
}

//列表页样式
.cust-list-cart {
  margin: 0px 0px 0px 0px !important;
}

.cust-list-cart .ant-table-footer {
  padding: 0px 10px;
  height: 37px;
  line-height: 37px;
}

.cust-list-cart .ant-row .ant-form-item {
  margin-bottom: 5px !important;
}

.cust-list-cart .ant-table-title {
  padding: 0px 10px 0px 10px !important;
  height: 45px;
  line-height: 45px;
  border-bottom: 0px !important;
  background: #fbfbfb;
}

.cust-list-cart .ant-table-content {
  clear: both;
}

//自定义卡片样式
.cust-normal-card {
  margin: 5px !important;
}

.cust-normal-card .ant-card-head-title {
  padding: 0px;
  font-weight: bold !important;
  font-size: 15px;
  line-height: 40px;
}

.cust-normal-card .ant-card-extra {
  padding: 0px;
  line-height: 40px;
}

.cust-normal-card .ant-card-head {
  background: #f8f8ff;
  min-height: 30px;
  padding: 0px 10px !important;
}

.cust-normal-card .ant-card-body {
  padding: 5px !important;
}

//自定义form input 提示样式
.cust-normal-card .ant-form-explain {
  font-size: 12px;
  line-height: 14px;
}

.cust-normal-card .ant-form-item-with-help {
  margin-bottom: 0px !important;
}

//自定义弹出dialog样式
.cust-modal {
  .ant-modal-body {
    padding: 10px;
  }
  .ant-form-explain {
    font-size: 12px;
    line-height: 16px;
    height: 16px;
  }

  .ant-form-item-with-help {
    margin-bottom: 0px !important;
  }

  .ant-form-item {
    margin-bottom: 14px;
  }
}

//自定义明细form
.cust-detail-form {
  padding-top: 45px !important;
}

.cust-card-table {
  padding-bottom: 10px;
}
//原有样式
.layout {
  min-height: 100vh;
  overflow-x: hidden;

  &.mobile {
    .ant-layout-content {
      .content {
        margin: 24px 0 0;
      }
    }

    /**
       * ant-table-wrapper
       * 覆盖的表格手机模式样式，如果想修改在手机上表格最低宽度，可以在这里改动
       */
    .ant-table-wrapper {
      .ant-table-content {
        overflow-y: auto;
      }

      .ant-table-body {
        min-width: 800px;
      }
    }

    .sidemenu {
      .ant-header-fixedHeader {
        &.ant-header-side-opened,
        &.ant-header-side-closed {
          width: 100%;
        }
      }
    }

    .topmenu {
      /* 必须为 topmenu  才能启用流式布局 */
      &.content-width-Fluid {
        .header-index-wide {
          margin-left: 0;
        }
      }
    }
  }

  &.ant-layout-has-sider {
    flex-direction: row;
  }

  .trigger {
    font-size: 20px;
    line-height: 50px;
    padding: 0 24px;
    cursor: pointer;
    transition: color 0.3s;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .sidemenu {
    .ant-header-fixedHeader {
      position: fixed;
      top: 0;
      right: 0;
      z-index: 9;
      width: 100%;
      transition: width 0.2s;

      &.ant-header-side-opened {
        width: calc(100% - 213px);
      }

      &.ant-header-side-closed {
        width: calc(100% - 80px);
      }
    }
  }

  .header {
    height: 50px;
    padding: 0 12px 0 0;
    background: #fff;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    position: relative;
  }

  .header,
  .top-nav-header-index {
    .breadcrumb {
      float: left;
      line-height: 50px;
    }

    .user-wrapper {
      float: right;
      height: 100%;

      .action {
        cursor: pointer;
        padding: 0 12px;
        display: inline-block;
        transition: all 0.3s;
        height: 100%;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }

        .avatar {
          margin: 12px 8px 20px 0;
          color: #1890ff;
          background: hsla(0, 0%, 100%, 0.85);
          vertical-align: middle;
        }

        .icon {
          font-size: 16px;
          padding: 4px;
        }
      }
    }

    &.dark {
      .user-wrapper {
        .action {
          color: rgba(255, 255, 255, 0.85);

          &:hover {
            background: rgba(255, 255, 255, 0.16);
          }
        }
      }
    }
  }

  &.mobile {
    .top-nav-header-index {
      .header-index-wide {
        .header-index-left {
          .trigger {
            color: rgba(255, 255, 255, 0.85);
            padding: 0 12px;
          }

          .logo.top-nav-header {
            text-align: center;
            width: 56px;
            line-height: 58px;
          }
        }
      }

      &.light {
        .header-index-wide {
          .header-index-left {
            .trigger {
              color: rgba(0, 0, 0, 0.65);
            }
          }
        }
      }
    }
  }

  &.tablet {
    // overflow: hidden; text-overflow:ellipsis; white-space: nowrap;
    .top-nav-header-index {
      .header-index-wide {
        .header-index-left {
          .logo > a {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
      }
    }
  }

  .top-nav-header-index {
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    position: relative;
    transition: background 0.3s, width 0.2s;

    .header-index-wide {
      max-width: 1200px;
      margin: auto;
      padding-left: 0;
      display: flex;
      height: 50px;

      .ant-menu.ant-menu-horizontal {
        border: none;
        height: 50px;
        line-height: 50px;
      }

      .header-index-left {
        flex: 1 1;
        display: flex;

        .logo.top-nav-header {
          width: 165px;
          height: 50px;
          position: relative;
          line-height: 50px;
          transition: all 0.3s;
          overflow: hidden;

          img {
            display: inline-block;
            vertical-align: middle;
            height: 32px;
          }

          h1 {
            color: #fff;
            display: inline-block;
            vertical-align: top;
            font-size: 14px;
            margin: 0 0 0 12px;
            font-weight: 400;
          }
        }
      }

      .header-index-right {
        float: right;
        height: 50px;
        overflow: hidden;
      }
    }

    &.light {
      background-color: #fff;

      .header-index-wide {
        .header-index-left {
          .logo {
            h1 {
              color: #002140;
            }
          }
        }
      }
    }
  }

  // 内容区
  .layout-content {
    margin: 24px 24px 0px;
    height: 100%;
    height: 50px;
    padding: 0 12px 0 0;
  }
}

.topmenu {
  .page-header-index-wide {
    max-width: 1200px;
    margin: 0 auto;
  }
}

// drawer-sider 自定义
.ant-drawer.drawer-sider {
  .sider {
    box-shadow: none;
  }

  &.dark {
    .ant-drawer-content {
      background-color: rgb(0, 21, 41);
    }
  }

  &.light {
    box-shadow: none;

    .ant-drawer-content {
      background-color: #fff;
    }
  }

  .ant-drawer-body {
    padding: 0;
  }
}

// 菜单样式
.sider {
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  position: relative;
  z-index: 10;

  &.ant-fixed-sidemenu {
    position: fixed;
    height: 100%;
  }

  .logo {
    height: 50px;
    position: relative;
    line-height: 50px;
    padding-left: 24px;
    -webkit-transition: all 0.3s;
    transition: all 0.3s;
    background: #002140;
    overflow: hidden;

    img,
    h1 {
      display: inline-block;
      vertical-align: middle;
    }

    img {
      height: 25px;
    }

    h1 {
      color: #fff;
      font-size: 16px;
      margin: 0 0 0 12px;
      font-family: 'Chinese Quote', -apple-system, BlinkMacSystemFont,
        'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
        'Helvetica Neue', Helvetica, Arial, sans-serif, 'Apple Color Emoji',
        'Segoe UI Emoji', 'Segoe UI Symbol';
      /*font-weight: 600;*/
    }
  }

  &.light {
    background-color: #fff;
    box-shadow: 2px 0px 8px 0px rgba(29, 35, 41, 0.05);

    .logo {
      background: #fff;
      box-shadow: 1px 1px 0px 0px #e8e8e8;

      h1 {
        color: unset;
      }
    }

    .ant-menu-light {
      border-right-color: transparent;
    }
  }
}

// 外置的样式控制
.user-dropdown-menu-wrapper.ant-dropdown-menu {
  padding: 4px 0;

  .ant-dropdown-menu-item {
    width: 160px;
  }

  .ant-dropdown-menu-item > .anticon:first-child,
  .ant-dropdown-menu-item > a > .anticon:first-child,
  .ant-dropdown-menu-submenu-title
    > .anticon:first-child
    .ant-dropdown-menu-submenu-title
    > a
    > .anticon:first-child {
    min-width: 12px;
    margin-right: 8px;
  }
}

.table-page-search-wrapper {
  .ant-form-inline {
    .ant-form-item {
      display: flex;
      margin-bottom: 24px;
      margin-right: 0;

      .ant-form-item-control-wrapper {
        flex: 1 1;
        display: inline-block;
        vertical-align: middle;
      }

      > .ant-form-item-label {
        line-height: 32px;
        padding-right: 8px;
        width: auto;
      }

      .ant-form-item-control {
        height: 32px;
        line-height: 32px;
      }
    }
  }

  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 24px;
    white-space: nowrap;
  }
}

.content {
  .table-operator {
    margin-bottom: 18px;

    button {
      margin-right: 8px;
    }
  }
}

//富文本编辑框默认样式
.ql-container {
  min-height: 100px;
}

/**
   * 自动异常超长字段
   */
.white-space-content {
  table-layout: fixed;
  max-width: 400px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/*.white-space-content:hover{*/
/*max-width: 600px;*/
/*white-space: normal;*/
/*overflow: visible;*/
/*}*/

/**
   * 百度地图
   */
#baiduMap {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
#allmap {
  height: 600px;
  width: auto;
  overflow: hidden;
}

.ant-input-disabled {
  color: rgba(109, 103, 103, 0.85) !important;
}

.ant-select-disabled {
  color: rgba(109, 103, 103, 0.85) !important;
}
/*页码选中样式*/
#components-pagination-demo-mini .ant-pagination:not(:last-child) {
  margin-bottom: 24px;
}

.bg-row {
  background-color: lightgrey;
}

.drawer-form-bottom-toolbar {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e9e9e9;
  padding: 10px 16px;
  background: #fff;
  text-align: right;
  z-index: 1;
}
</style>