<template>
  <pro-layout
    v-bind="settings"
    :collapsed="collapsed"
    :handleCollapse="handleCollapse"
    :handleMediaQuery="handleMediaQuery"
    :i18nRender="i18nRender"
    :isMobile="isMobile"
    :mediaQuery="query"
    :menus="menus"
  >
    <template v-slot:menuHeaderRender>
      <div>
        <!-- <img v-if="logo!= ''" :src="logo" /> -->
        <img style="border-radius: 50%;" src="~@/assets/logo.png" />
        <h1>{{ title }} </h1>
      </div>
    </template>

    <template v-slot:rightContentRender>
      <span class="rightContentRenderTitle">欢迎使用水感应经销商端</span>
      <right-content :is-mobile="isMobile" :theme="settings.theme" :top-menu="settings.layout === 'topmenu'" />
    </template>

    <!--    <template v-slot:footerRender>-->
    <!--      <global-footer />-->
    <!--    </template>-->

    <keep-alive>
      <router-view v-if="keepAlive" />
    </keep-alive>
    <router-view v-if="!keepAlive" />
    <div class="size">{{ size }}</div>

    <a-back-top />

  </pro-layout>
</template>

<script>
import { updateTheme } from '@ant-design-vue/pro-layout'
import { i18nRender } from '@/locales'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'
import { asyncRouterMap } from '@/config/router.config.js'
import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'
export default {
  name: 'BasicLayout',
  components: {
    RightContent,
    GlobalFooter
  },
  data () {
    return {
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: ' 水感应代理商 ',
      logo: '',
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,
        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},
      // 是否手机模式
      isMobile: false,
      screenWidth: document.documentElement.clientWidth,
      size: this.filterSize(document.documentElement.clientWidth)
    }
  },
  computed: {
    keepAlive () {
      return !(this.$route.meta.keepAlive !== undefined && !this.$route.meta.keepAlive)
    }
  },
  watch: {
    screenWidth: function (val) {
      this.size = this.filterSize(val)
    }
  },
  created () {
    const routes = asyncRouterMap.find((item) => item.path === '/')

    this.menus = (routes && routes.children) || []
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted () {
    window.addEventListener('resize', () => {
      this.screenWidth = document.body.offsetWidth
    })

    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    filterSize (val) {
      let size = ''
      if (val < 576) {
        size = 'xs'
      }

      if (val >= 576) {
        size = 'sm'
      }

      if (val >= 768) {
        size = 'md'
      }

      if (val >= 992) {
        size = 'lg'
      }

      if (val >= 1200) {
        size = 'xl'
      }

      if (val >= 1600) {
        size = 'xxl'
      }
      return size + '/' + val
    },
    i18nRender,
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    }
  }
}
</script>

<style lang="less">
@import "./BasicLayout.less";

.size {
  position: fixed;
  bottom: 10px;
  right: 10px;
}

</style>
