<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
    <partner-site-menu v-if="siteMenu && siteMenu.length"></partner-site-menu>
    <breadcrumb v-else id="breadcrumb-container" class="breadcrumb-container" />
    <!--setting-->
    <div class="right-menu" style="display: flex; flex-wrap: wrap; align-items: center; align-content: center">
      <div class="navbar-fast-link" v-if="uiFastLink.length">
        <a
          v-for="(item,index) in uiFastLink"
          :target="item.target || '_self'"
          :href="item.href" v-html="item.title"
          :key="index"
        ></a>
      </div>
      <!--pc-->
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <span class="user-name" v-html="name"></span>
          <i class="el-icon-setting"/>
        </div>
        <el-dropdown-menu slot="dropdown" class="app-dropdown">
          <div class="ercode" v-if="0">
            <img v-if="ercode" width="150" height="150" :src="ercode">
          </div>
          <a :href="myAppUrl">
            <el-dropdown-item>我的应用</el-dropdown-item>
          </a>
          <el-dropdown-item @click.native="toEditPwd">修改密码</el-dropdown-item>
          <el-dropdown-item :divided="false" @click.native="userLogout">
            <span style="display:block;">退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <!--pc-->
    </div>
    <!--setting end-->
  </div>
</template>

<script>
import { mapGetters, mapActions, mapState } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import ErrorLog from '@/components/ErrorLog'
import Screenfull from '@/components/Screenfull'
import PartnerSiteMenu from "./PartnerSiteMenu.vue"



export default {
  components: {
    Breadcrumb,
    Hamburger,
    ErrorLog,
    Screenfull,
    PartnerSiteMenu
  },

  computed: {
    ...mapState(['fastLinks']),
    ...mapGetters([
      'sidebar',
      'avatar',
      'device',
      'name',
      'siteMenu'
    ])
  },


  data(){
    return {
      ercode: '/images/code.png',
      uiFastLink: [],
      myAppUrl: '//sandbox-admin-login.jufubao.cn/index',
      repwd: 'http://sandbox-admin-login.jufubao.cn/repwd',
    }
  },

  watch:{
    fastLinks(val){
      this.uiFastLink = val;
    }
  },

  created(){
    const current = require('./../../../env.current.json')
    if(this.fastLinks) {
      this.uiFastLink = this.fastLinks;
    }
    
    if(this.$setting['myAppUrl']) {
      this.myAppUrl = this.$setting['myAppUrl']
    }

    if(current.service==='build'&&current.type==='build') {
      this.repwd = "//idaas.jufubao.cn/repwd"
    }
    
  },

  methods: {
    ...mapActions([
      'logout'
    ]),

    userLogout(){
      this.logout({path: `${window.location.protocol}//${window.location.host}/`})
    },

    toEditPwd() {
      window.location.href = this.repwd
    },

    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },

  }
}
</script>

<style lang="scss" scoped>

  .navbar-fast-link {
    display: flex;
    justify-content: flex-end;
    align-items: center;

    & > a {
      font-size: 14px;
      color: #666;
      margin-left: 15px;
    }
  }
.app-dropdown {
  width: 160px;

  .ercode {
    width: 160px;
    text-align: center;
  }
}
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      margin-right: 10px;

      .avatar-wrapper {
        display: flex;
        align-items: center;
        height: 50px;

        span {
          font-size: 14px;
          padding-right: 10px;
        }

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}

@media screen and (max-width: 450px) {
  #hamburger-container {
    padding: 0 10px 0 15px!important;
  }

  .breadcrumb-container {
    margin-left: 1vw !important;
  }

  .user-name {
    display: none;
  }

  .avatar-container {
    margin-right: 0;
  }

  .avatar-admin {
    padding: 0 !important;
  }
  .avatar-admin:active,
  .avatar-admin:hover,
  .avatar-container:hover,
  .avatar-container:active {
    background: none !important;
  }

  .avatar-admin .el-button {
    padding-right: 1vw;
    padding-left: 1vw;
  }

}
</style>
