<template>
  <header class="app-header">
    <section class="app-header-bar">
      <div class="inner">
        <div class="login">
          <template v-if="isLogin">
            <div>{{ user.nickname || user.realname }}</div>
            <span>|</span>
            <div @click="handleLogout">退出</div>
          </template>
          <template v-else>
            <div @click="handleRegister">注册</div>
            <span>|</span>
            <div @click="handleLogin">登录</div>
          </template>
        </div>
      </div>
    </section>
    <section class="app-header-main">
      <div class="hd">
        <div class="hd-left">
          <a href="/"><img src="../../assets/img/head/ezijing-logo.png" class="logo" /></a>
          <h1 class="title">{{ title }}</h1>
        </div>
        <div class="hd-right">
          <img src="../../assets/img/head/kelley-logo.png" />
        </div>
      </div>
      <div class="bd">
        <ul class="nav">
          <li v-for="item in navList" :key="item.path">
            <router-link :to="item.path">{{ item.title }}</router-link>
          </li>
        </ul>
        <div class="search">
          <div class="lang">EN</div>
          <input type="text" placeholder="Search" class="search-input" />
          <div class="search-button">搜索</div>
        </div>
      </div>
    </section>
    <vue-passport ref="passport" @ready="onReady" :options="options"></vue-passport>
  </header>
</template>

<script>
export default {
  data() {
    return {
      title: '中国未来金融领袖计划',
      navList: [
        { title: '首页', path: '/index' },
        { title: '项目介绍', path: '/project' },
        { title: '视频中心', path: '/videoCenter' },
        { title: '招生信息', path: '/recruit' },
        { title: '教学模块', path: '/teaching' },
        { title: '师生风采', path: '/presence' },
        { title: '职业发展', path: '/careerDev' },
        { title: '校友工作', path: '/alumniWork' }
      ],
      isLogin: false,
      user: null,
      options: {
        login: {
          account: {
            onSuccess: this.loginSuccess
          },
          phone: {
            onSuccess: this.loginSuccess
          }
        }
      }
    }
  },
  computed: {
    passport() {
      return this.$refs.passport
    }
  },
  watch: {
    $route(route) {
      if (route.query.needLogin) {
        this.handleLogin()
      }
    }
  },
  methods: {
    handleLogin() {
      this.passport.login()
    },
    handleRegister() {
      this.passport.register()
    },
    handleLogout() {
      this.passport.logout()
      this.isLogin = false
      window.G.UserInfo = {}
      this.$router.push('/')
    },
    onReady(isLogin, user = {}) {
      this.isLogin = isLogin
      this.user = user
      window.G.UserInfo = user
    },
    loginSuccess(data) {
      this.passport.checkLoginStatus((isLogin, user = {}) => {
        this.isLogin = isLogin
        this.user = user
        this.$router.push('/my')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-header {
  background-color: #fff;
  .inner {
    max-width: 970px;
    margin: 0 auto;
  }
}
.app-header-bar {
  height: 40px;
  background-color: #333;
  .login {
    float: right;
    display: flex;
    line-height: 40px;
    color: #fff;
    div {
      font-size: 18px;
      text-align: center;
      cursor: pointer;
    }
    span {
      padding: 0 20px;
    }
  }
}
.app-header-main {
  max-width: 970px;
  margin: 0 auto;
  .hd {
    height: 110px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .hd-left {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .logo {
    width: 156px;
    height: 50px;
  }
  .title {
    margin-left: 20px;
    padding: 0 10px;
    color: #222;
    font-size: 20px;
    line-height: 33px;
    font-weight: 600;
    border-left: 2px solid #333;
  }
  .bd {
    margin-top: -20px;
    padding: 10px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .nav {
    display: flex;
    li {
      font-size: 15px;
      font-weight: 600;
      color: #222;
      line-height: 20px;
      padding: 0 14px;
      a {
        position: relative;
      }
      &:first-child {
        padding-left: 0;
      }
    }
    li + li {
      border-left: 1px solid #d7d8d9;
    }
  }
  .router-link-active:after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    bottom: -3px;
    height: 2px;
    background: #bf0927;
  }
  .search {
    display: flex;
    align-items: center;
  }
  .lang {
    font-size: 15px;
    color: #999;
  }
  .search-input {
    outline: none;
    width: 159px;
    height: 34px;
    border-radius: 4px;
    border: 1px solid #ccc;
    margin: 0 3px 0 8px;
    padding-left: 13px;
    font-size: 15px;
    color: #999;
  }
  .search-button {
    width: 54px;
    height: 34px;
    border-radius: 4px;
    border: 1px solid #ccc;
    font-size: 15px;
    color: #999;
    text-align: center;
    line-height: 34px;
    cursor: pointer;
  }
}
</style>
