<template>
  <header class="header" :class="{ 'no-shadow': !shadow }">
    <headerNavMobile v-if="$isMobile" :nav="navbar" :userInfo="userInfo"></headerNavMobile>
    <div class="container" v-else>
      <div class="logo">
        <span class="logo_text">
          <img src="../assets/logo.png" height="40px" />
        </span>
      </div>
      <nav class="navbar">
        <ul class="navbar_item_wrap">
          <li
            class="navbar_item"
            :class="{ active: active === item.path }"
            v-for="item in navbar"
            @click="changePath"
            :key="item.path">
            <a :href="item.path">{{ item.name }}</a>
          </li>
        </ul>
      </nav>
      <div class="right_btn">
        <!-- <el-button type="primary" size="mini" @click="login">登录</el-button>
        <el-button size="mini" @click="register">注册</el-button> -->
        <div class="user-operate" v-if="!userInfo">
          <span class="login-btn" @click="login">登录</span>
          <span class="reg-btn" @click="register">注册</span>
        </div>
        <div class="user-info" v-else>
          <div class="user-msg">
            <span class="user-name">{{ userInfo.companyName }}</span>
            <span>欢迎您</span>
          </div>
          <div class="logout" title="退出登录" @click="logout">
            <img src="../assets/images/logout.png" />
          </div>
        </div>
        <el-button type="primary" @click="freeTrial">免费试用</el-button>
      </div>
    </div>
  </header>
</template>

<script>
import { mapMutations } from 'vuex'
import headerNavMobile from './headerNavMobile.vue'

export default {
  props: {
    shadow: { type: Boolean },
  },
  inject: {
    scrollParent: 'scrollParent',
  },
  components: {
    headerNavMobile,
  },
  data() {
    return {
      navbar: [
        { name: '首页', path: '/home' },
        { name: '核心功能', path: '/function' },
        // { name: '价格', path: '/price' },
        { name: '需求痛点', path: '/demand_pain_point' },
        { name: '关于我们', path: '/about_us' },
        { name: '一键定制', path: 'https://jomalls.com/custom/' },
        { name: '帮助', path: '/help' },
      ],
      userInfo:JSON.parse(localStorage.getItem('userInfo'))
    }
  },
  computed: {
    // ...mapState(['shopifyObj']),
    active() {
      return this.$route.path
    },
  },
  mounted() {
    if(this.$route.query.shopKey){
      localStorage.setItem('shopifyObj', JSON.stringify(this.$route.query))
    }
    if(this.userInfo && this.$route.query.shopKey){
      this.$router.push('/contact')
    }
  },
  methods: {
    ...mapMutations(['setUserInfo','setShopKey']),
    changePath() {
      this.scrollParent().scrollTo({
        behavior: 'smooth',
        top: 0,
      })
    },
    login() {
      if(this.$route.query&&this.$route.query.shopKey){
        this.setShopKey(this.$route.query.shopKey)
      }
      this.$router.push('/login')
    },
    register() {
      this.$router.push('/register')
    },
    freeTrial() {
      window.open('https://demo.jomalls.com')
    },
    logout() {
      localStorage.removeItem('userInfo')
      this.setUserInfo(undefined)
      this.$router.push('/login')
    },
  },
}
</script>

<style scoped>
.header {
  position: fixed;
  z-index: 999;
  top: 0;
  right: 0;
  left: 0;
}

.user-name {
  margin-right: 10px;
}

.user-msg {
  color: #fff;
}

.logout {
  margin-right: 30px;
}

@media screen and (max-width: 1100px) {
  .header {
    position: sticky;
  }
}

.header.no-shadow {
  box-shadow: none !important;
}

.logo {
  float: left;
  margin-top: 10px;
}
.navbar {
  float: left;
  margin-left: 100px;
}
.navbar_item {
  float: left;
  position: relative;
}

.navbar_item:hover a {
  color: #f19240;
}
.navbar_item + .navbar_item {
  margin-left: 30px;
}
.navbar_item a {
  display: block;
  height: 60px;
  line-height: 60px;
  text-decoration: none;
  padding: 0 20px;
  color: #fff;
}
.right_btn,
.user-info {
  display: flex;
  align-items: center;
  margin-left: auto;
}

.user-msg {
  margin-right: 50px;
}

.logout img {
  width: 20px;
  vertical-align: middle;
  cursor: pointer;
}

.user-info {
  color: #fff;
}

.login-btn {
  position: relative;
  margin-right: 20px;
  cursor: pointer;
  color: #fff;
}

.reg-btn {
  cursor: pointer;
  color: #fff;
}

.login-btn::after {
  content: '';
  width: 1px;
  height: 15px;
  position: absolute;
  display: inline-block;
  margin-left: 10px;
  top: 4px;
  background-color: #fff;
}

.user-operate {
  margin-right: 10px;
}

.active a {
  color: #f19240;
  font-weight: bold;
}

.active::after {
  content: '';
  width: 20px;
  height: 2px;
  background-color: #f19240;
  border-radius: 4px;
  display: block;
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
}
</style>
