<template>
  <Layout style="height: 100%;background: #EEF0F3;" class="main">
    <div class="main__bg"></div>
    <Sider hide-trigger collapsible :width="256" :collapsed-width="64" v-model="collapsed" class="left-sider" :style="{overflow: 'hidden'}">
      <side-menu accordion ref="sideMenu" class="side-menu" :active-name="$route.name" :collapsed="collapsed" @on-select="turnToPage" :menu-list="menuList">
        <!-- 需要放在菜单上面的内容，如Logo，写在side-menu标签内部，如下 -->
        <div class="logo-con">
          <img v-show="!collapsed" :src="maxLogo" key="max-logo" />
          <img v-show="collapsed" :src="minLogo" key="min-logo" />
        </div>
      </side-menu>
    </Sider>
    <Layout>
      <Header class="header-con">
        <header-bar :collapsed="collapsed" @on-coll-change="handleCollapsedChange">
          <!-- <a target="_blank" style="float:left;margin-right:20px" href="/docs.php">接口文档</a> -->

          <user/>
          <div style="margin-right:20px;">
            <p style="color:#333333;font-size:16px;height:16px;margin-bottom:3px;">{{username}}</p>
            <p style="color: #666666;font-size:12px;height:12px;">{{level_name}}</p>
          </div>
          <language v-if="$config.useI18n" @on-lang-change="setLocal" style="margin-right: 25px;" :lang="local"/>
          <fullscreen v-model="isFullscreen" style="margin-right: 20px;"/>
          <!--
          <error-store v-if="$config.plugin['error-store'] && $config.plugin['error-store'].showInHeader" :has-read="hasReadErrorPage" :count="errorCount"></error-store>-->
        </header-bar>
      </Header>
      <Content class="main-content-con">
        <Layout class="main-layout-con">
          <!-- <div class="tag-nav-wrapper">
            <tags-nav :value="$route" @input="handleClick" :list="tagNavList" @on-close="handleCloseTag"/>
          </div> -->
          <Content class="content-wrapper">
            <keep-alive :include="cacheList">
              <router-view/>
            </keep-alive>
            <ABackTop :height="100" :bottom="80" :right="50" container=".content-wrapper"></ABackTop>
          </Content>
        </Layout>
      </Content>
    </Layout>
  </Layout>
</template>
<script>
import SideMenu from './components/side-menu'
import HeaderBar from './components/header-bar'
import TagsNav from './components/tags-nav'
import User from './components/user'
import ABackTop from './components/a-back-top'
import Fullscreen from './components/fullscreen'
import Language from './components/language'
import ErrorStore from './components/error-store'
import { mapMutations, mapActions, mapGetters } from 'vuex'
//  getNewTagList,
import { routeEqual, getToken } from '@/libs/util'
// import { Profile } from '@/api/user'
import routers from '@/router/routers'
import minLogo from '@/assets/images/logo-min.png'
// import maxLogo from '@/assets/images/logo.png'
import './main.less'
import store from '@/store'
export default {
  name: 'Main',
  components: {
    SideMenu,
    HeaderBar,
    Language,
    TagsNav,
    Fullscreen,
    ErrorStore,
    User,
    ABackTop
  },
  data () {
    return {
      token: getToken(),
      nickname: '',
      collapsed: false,
      minLogo,
      // maxLogo,
      /* minLogo: localStorage.getItem('minLogo'), */
      maxLogo: localStorage.getItem('maxLogo'),
      isFullscreen: false
    }
  },
  beforeRouteEnter (to, from, next) {
    if (!store.state.user.a_levelname) {
      store.dispatch('getIndexInfo')
    }
    next()
  },
  computed: {
    ...mapGetters([
      'errorCount'
    ]),
    level_name () {
      return store.state.user.a_levelname
    },
    username () {
      return store.state.user.username
    },
    tagNavList () {
      return this.$store.state.app.tagNavList
    },
    tagRouter () {
      return this.$store.state.app.tagRouter
    },
    userAvatar () {
      return this.$store.state.user.avatarImgPath
    },
    cacheList () {
      const list = ['ParentView', ...this.tagNavList.length ? this.tagNavList.filter(item => !(item.meta && item.meta.notCache)).map(item => item.name) : []]
      return list
    },
    menuList () {
      // return this.$store.getters.menuList //
      return this.$store.state.app.menuList //
    },
    local () {
      return this.$store.state.app.local
    },
    hasReadErrorPage () {
      return this.$store.state.app.hasReadErrorPage
    },
    unreadCount () {
      return this.$store.state.user.unreadCount
    }
  },
  methods: {
    ...mapMutations([
      'setBreadCrumb',
      // 'setTagNavList',
      // 'addTag',
      'setLocal',
      'setHomeRoute',
      'closeTag'
    ]),
    ...mapActions([
      'handleLogin',
      'getIndexInfo'
      // 'getUnreadMessageCount'
    ]),
    turnToPage (route) {
      let { name, params, query } = {}
      if (typeof route === 'string') name = route
      else {
        name = route.name
        params = route.params
        query = route.query
      }
      if (name.indexOf('isTurnByHref_') > -1) {
        window.open(name.split('_')[1])
        return
      }
      this.$router.push({
        name,
        params,
        query
      })
    },
    handleCollapsedChange (state) {
      this.collapsed = state
    },
    handleCloseTag (res, type, route) {
      if (type !== 'others') {
        if (type === 'all') {
          this.turnToPage(this.$config.homeName)
        } else {
          if (routeEqual(this.$route, route)) {
            this.closeTag(route)
          }
        }
      }
      // this.setTagNavList(res)
    },
    handleClick (item) {
      this.turnToPage(item)
    }
    /* ProfileFunction () {
      const formdata = new FormData()
      formdata.append('access_token', this.token)
      Profile(formdata).then(res => {
        const backData = res.data.profile
        if (res.ret === 200) {
          this.username = backData.username
          this.nickname = backData.nickname
          this.member_level_name = backData.member_level_name
          if (backData.member_level === 0) {
            this.member_level_str = '普通会员'
          } else if (backData.member_level === 100) {
            this.member_level_str = '个人开发者'
          } else if (backData.member_level === 101) {
            this.member_level_str = '企业开发者'
          } else if (backData.member_level === 200) {
            this.member_level_str = '管理员'
          } else if (backData.member_level === 255) {
            this.member_level_str = '超级管理员'
          }
        } else {
          this.$router.push({ name: 'login' })
        }
      })
    } */
  },
  watch: {
    '$route' (newRoute) {
      // const { name, query, params, meta } = newRoute
      // this.addTag({
      //   route: { name, query, params, meta },
      //   type: 'push'
      // })
      this.setBreadCrumb(newRoute)
      // this.setTagNavList(getNewTagList(this.tagNavList, newRoute))
      this.$refs.sideMenu.updateOpenName(newRoute.name)
    },
    '$i18n.locale' () {
      this.$router.go(0)
    }
  },
  mounted () {
    /**
     * @description 初始化设置面包屑导航和标签导航
     */
    // this.setTagNavList()
    this.setHomeRoute(routers)
    // const { name, params, query, meta } = this.$route
    // this.addTag({
    //   route: { name, params, query, meta }
    // })
    this.setBreadCrumb(this.$route)
    // 设置初始语言
    this.setLocal(this.$i18n.locale)
    // 如果当前打开页面不在标签栏中，跳到homeName页
    // if (!this.tagNavList.find(item => item.name === this.$route.name)) {
    //   this.$router.push({
    //     name: this.$config.homeName
    //   })
    // }
    // 获取未读消息条数
    // this.getUnreadMessageCount()
    // this.ProfileFunction()
  }
}
</script>
<style lang="scss">
.logo-con{
  display: flex;
  justify-content: center;
}
  .ivu-menu-dark.ivu-menu-vertical .ivu-menu-item, .ivu-menu-dark.ivu-menu-vertical .ivu-menu-submenu-title {
    color:#87878B!important;
  }
  .ivu-card-head {
    span{
      float: left;
      margin-left: 10px;
    }
  }
  .left-sider {
    position: relative;
    .sider-footer {
      font-family: PingFangSC-Regular, "PingFang SC";
      font-weight: 400;
      color: rgb(139, 139, 143);
      text-align: center;
      margin: 20px 0;
    }
  }
</style>
