<template>
  <div class="nav" @click.stop>
    <div class="nav-content">
      <router-link :to="{name: 'home'}" class="logo" ><img width="128" height="37" src="http://video.cgaii.com/new4dage/images/images/logo.svg" @click="showMenu = false"></router-link>
      <div class="menu" :style="[{height: height}, style]" ref="menu">
        <Menu v-for="(menu,i) in menus"
            :link="menu.link"
            :key="i"
            :text="menu.text"
            :childs="menu.childs"
            :show="menu.show"
            class="menu-item"
            @open=" open(menu)"
            @hide="hide(menu)"
            @change="change(menu)"
            @tolink="showMenu = false">
        </Menu>
        <div class="head-info">
          <a class="number">400<span>-</span>6698<span>-</span>025</a><div class="language"
            :class="{pc: !isMobile}"
            @mouseenter="langShow = true"
            @mouseleave="langShow = false">
            <Menu
              v-if="!isMobile"
              :link="'#'"
              :text="langMap[language - 1]"
              :childs="[
                {link: '#', lang: '/cn/', text: '简体中文'},
                {link: '#', lang: '/en/', text: 'English'},
                {link: '#', lang: '/de/', text: 'Deutsch'}
              ]"
              :show="langShow"
              class="menu-item"
              @checkItem="changeLange">
            </Menu>

            <template v-else>
              <span @click="urlrep('/cn/')">简体中文</span> |
              <span @click="urlrep('/en/')">English</span> |
              <span @click="urlrep('/de/')">Deutsch</span>
            </template>
          </div>
        </div>
      </div>
      <button class="ctl" @click="changeState">
        <span></span><span></span><span></span>
      </button>
    </div>
  </div>
</template>

<script>
import Menu from '@/components/menu'
import browser from '@/util/browser'

export default {
  name: 'navigation',
  data () {
    return {
      langMap: ['中文简体', 'English', 'Deutsch'],
      language: global.language,
      langShow: false,
      menus: [
        {
          link: '/product',
          text: ' 产品中心',
          show: false,
          childs: [
            {link: '/product?id=swkk', text: '四维看看'},
            {link: '/product?id=swmk', text: '四维模库'},
            {link: '/product?id=swys', text: '四维艺术'},
            {link: '/product?id=swkz', text: '四维看展'},
            {link: '/product?id=wwq', text: '万物墙'},
            {link: '/product?id=swar', text: '四维AR'}
          ]
        },
        {
          link: '/planculture',
          text: '解决方案',
          show: false,
          childs: [
            {link: '/planculture', text: '数字文博'},
            {link: '/planlife', text: '数字生活'},
            {link: '/planindustry', text: '数字工业'}
          ]
        },
        {
          link: '/technology',
          text: '核心技术',
          show: false,
          childs: [
            {link: '/technology?id=szjm', text: '自动化三维数字建模'},
            {link: '/technology?id=jqsj', text: '高精度机器视觉'},
            {link: '/technology?id=slam', text: 'SLAM视觉追踪'},
            {link: '/technology?id=sjjs', text: '人工智能视觉技术'}
          ]
        },
        {
          link: '/case/文博',
          text: '客户案例',
          show: false,
          childs: [
            {link: {name: 'case_details', params: {id: '文博'}}, text: '文博'},
            {link: {name: 'case_details', params: {id: '政务'}}, text: '政务'},
            {link: {name: 'case_details', params: {id: '电商'}}, text: '电商'},
            {link: {name: 'case_details', params: {id: '工业'}}, text: '工业'},
            {link: {name: 'case_details', params: {id: '地产'}}, text: '地产'},
            {link: {name: 'case_details', params: {id: '文物'}}, text: '文物'}
          ]
        },
        {
          link: '/information/news',
          text: '新闻中心',
          show: false,
          childs: [
            {link: '/information/news', text: '新闻资讯'},
            {link: '/information/media', text: '媒体资料库'},
            {link: '/information/code', text: '官方社交及媒体账号'}
          ]
        },
        {
          link: '/about',
          text: '关于我们',
          show: false,
          childs: [
            {link: '/about?id=qyjj', text: '企业简介'},
            {link: '/about?id=jrwm', text: '加入我们'}
          ]
        }
      ],
      showMenu: false,
      height: 0,
      mxHeight: null,
      isMobile: browser.mobile
    }
  },
  methods: {
    changeLange (item) {
      console.log(item)
      this.urlrep(item.lang)
    },
    urlrep (lg) {
      location.pathname = lg
    },
    open (menu) {
      this.menus.forEach(m => {
        m.show = false
      })
      menu.show = true
    },
    hide (menu) {
      menu.show = false
    },
    change (menu) {
      menu.show ? this.hide(menu) : this.open(menu)
    },
    over (menu) {
      menu.active = true
    },
    out (menu) {
      menu.active = false
    },
    changeState () {
      if (!this.menuHeight) {
        this.$refs.menu.style.visibility = 'hidden'
        this.$refs.menu.style.height = 'auto'
        this.menuHeight = this.$refs.menu.offsetHeight
        this.$refs.menu.style.visibility = ''
        this.$refs.menu.style.height = '0'
      }
      this.showMenu = !this.showMenu
    },
    sizeHandle () {
      if (window.innerWidth < 1400) {
        this.mxHeight = window.innerHeight - 65
      } else {
        this.mxHeight = 0
      }
    }
  },
  watch: {
    showMenu () {
      this.height = !this.showMenu ? '0' : this.menuHeight + 'px'
      this.showMenu && setTimeout(() => {
        this.height = 'auto'
      }, 300)
    }
  },
  computed: {
    style () {
      if (this.mxHeight) {
        return {
          maxHeight: this.mxHeight + 'px',
          overflowY: 'scroll'
        }
      } else {
        return {
          overflowY: 'inherit',
          maxHeight: 'auto'
        }
      }
    }
  },
  mounted () {
    document.documentElement.addEventListener('click', () => {
      this.showMenu = false
    })

    if (browser.isMobile) {
      this.mxHeight = window.innerHeight - 65
    } else {
      this.sizeHandle = this.sizeHandle.bind(this)
      this.sizeHandle()
      this.$bus.$on('resize', this.sizeHandle)
    }
  },
  beforeDestroy () {
    this.$bus.$off('resize', this.sizeHandle)
  },
  components: { Menu }
}
</script>

<style scoped type="text/css">
.nav {
  background-color: #292929;
  box-shadow: 0 0 20px rgba(0,0,0,0.7);
  position: relative;
  padding: 0 20px;
  color: #fff;
  font-size: 16px;
  font-weight: 400;
  box-sizing: border-box;
  transition: all linear .3s;
}

.nav::after {
  content: '';
  display: block;
  clear: both;
}

.nav-content {
  max-width: 1400px;
  margin: 0 auto;
}

.ctl {
  float: right;
  border: 1px solid #fff;
  padding: 6px;
  background-color: transparent;
  width: 40px;
  overflow: hidden;
  margin: 18px 0;
}

.ctl span {
  background-color: #888;
  width: 100%;
  height: 2px;
  border-radius: 1px;
  float: left;
}

.ctl span:nth-child(2) {
  margin: 4px 0;
}

.logo {
  float: left;
  padding: 13px 0;
}

.menu {
  display: block;
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  background-color: #292929;
  height: 0;
  transition: height ease-in-out .3s;
  overflow-y: scroll;
}

.menu .menu-item {
  text-decoration: none;
  padding: 0 24px;
  display: block;
}

.language span {
  transition: color .3s linear;
}

.language span:hover {
  color: #18ade9;
}

.language {
  vertical-align: middle;
  margin-left: 0;
  padding: 24px 0 24px 30px;
  display: block;
}

.number {
  vertical-align: middle;
  padding: 20px;
  position: relative;
  margin-left: 30px;
  padding-left: 30px;
  display: inline-block;
}

.number::before {
  position: absolute;
  content: '';
  width: 20px;
  height: 20px;
  background: url('http://video.cgaii.com/new4dage/images/images/phone.png') no-repeat left top;
  left: 0;
  top: 17px;
}

.number span {
  display: inline-block;
  transform: translateY(-2px)
}

.head-info {
  display: inline-block;
}

.language.pc {
  padding: 0 !important
}

@media screen and (min-width: 1400px) {
  .nav {
    text-align: right;
  }

  .ctl {
    display: none;
  }

  .menu {
    position: static;
    margin: 0;
    width: auto;
    display: inline-block;
    background: none;
    height: auto !important;
    overflow: inherit;
  }

  .menu .menu-item {
    height: 100%;
    padding: 24px 40px;
    display: inline-block;
  }

  .language {
    display: inline-block;
  }

  .number {
    display: inline-block;
    padding: 0;
    padding: 24px 0 24px 30px;
  }

  .number::before {
    top: 22px;
  }
}

@media screen and (min-width: 1600px) {
  .nav {
    padding: 0 150px;
  }
}
</style>
