<template>
    <div >
        <div class="header1">
          <div class="header2">
            <div class="language">
                <span  class="active" style="margin-left: 30px;">
                  <span class="cursor" @click="locale('zh')">{{ $t('common.CN')}}</span>
                  <span style="margin: 0 12px; display: inline-block; width: 2px; height: 12px; background: #ccc;vertical-align: middle;"></span>
                  <span class="cursor" @click="locale('en')">EN</span>
                </span>
                <span class="active"  style="float: right;">
                  <span style="margin-right: 9px;" @click="toView('Register',{})" v-if="i==1">
                    <img src="@/assets/注册.png" style="height: 12px;width: 12px;margin-right: 0px;vertical-align: middle;">
                    {{ $t('common.register')}}
                  </span>
                  <span style="margin-right: 9px;" @click="toView('login',{})" v-if="i==1">
                    <img src="@/assets/登录.png" style="height: 12px;width: 12px;margin-right: 0px;vertical-align: middle;">
                    {{ $t('common.login')}}
                  </span>
                  <!-- <span style="margin-right: 9px;" v-if="i==2 || i==3">{{ $t('common.OngoingProject')}}（<span style="color: #ff0000;">0</span>）</span> -->

                  <!-- <span style="margin-right: 9px;" v-if="i==2 || i==3">{{ $t('common.MyAttention')}}（<span style="color: #ff0000;">0</span>）</span>
                  <span style="margin-right: 9px;" v-if="i==2 || i==3">{{ $t('common.MyCollection')}}</span> -->
                  <i class="el-icon-s-custom" style="color: #fff;margin-right: 5px;"  v-if="i==2 || i==3"></i>
                  <el-dropdown trigger="click" @command="handleCommand" >

                    <span style="margin-right: 9px; cursor:pointer" v-if="i==2 || i==3">
                      {{ $t('common.Usercenter')}}
                      <i class="el-icon-arrow-down"></i>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          v-for="(item,index) in options"
                          :command="item"
                        >{{$t(item.label)}}</el-dropdown-item>
                      </el-dropdown-menu>
                    </span>
                  </el-dropdown>
                  <i class="el-icon-message-solid"  style="margin-left: 15px;color: #fff;" v-if="i==2 || i==3"></i>
                  <span  v-if="i==2 || i==3" style="margin-left: 5px;">{{ $t('common.MessageNotification')}}（<span style="color: #ff0000;">0</span>）</span>
                  <!-- <span style="margin-right: 9px;">{{ $t('common.CustomerService')}}</span> -->
                  <!-- <span style="margin-right: 9px;">{{ $t('common.HelpCenter')}}</span> -->
                </span>
            </div>
          </div>

        </div>
    </div>
  </template>

  <script>
    import Bus from "@/api/bus";   //注意引入
  export default {
    name: 'IntroductionHeader',
    data () {
      return {
        i:'1',
        options: [{
          value: 'userCenterView',
          label: 'common.Usercenter',
          json:''
        }, {
          value: 'information',
          label: 'common.BasicInformation',
          json:'information'
        }, {
          value: 'authentication',
          label: 'common.Auth',
          json:'/authentication'
        }, {
          value: 'securitySetting',
          label: 'common.SecuritySettings',
          json:'/securitySetting'
        }, {
          value: 'loginOut',
          label: 'common.Logout',
          json:''
        }],
      }
    },
    watch:{
      '$i18n.locale'(){
        if(this.$i18n.locale=='en'&& this.$Cookies.get('token')){
          this.i=3
        }else if(this.$i18n.locale=='zh'&& this.$Cookies.get('token')){
          this.i=2
        }else {
          this.i=1
        }
      }
    },
    mounted(){
      let locale=window.localStorage.getItem('locale')
      if(!locale){
        window.localStorage.setItem('locale','en')
      }
      document.title = this.$t('common.GEIDcpTitleSeptember');
      let locales=window.localStorage.getItem('locale')
      this.$i18n.locale = locales
      console.log(window.localStorage.getItem('locale'),window.localStorage.getItem('userLogin'))
      //if (window.localStorage.getItem('userLogin')) {
      if (this.$Cookies.get('token') && this.$i18n.locale=='zh') {
          return this.i=2
      }else if(this.$i18n.locale=='en'&& this.$Cookies.get('token')){
        return this.i=3
      }else {
          return this.i=1
      }
    },
    methods:{
      locale(lang){
        this.$i18n.locale = lang
        let locale=window.localStorage.getItem('locale')
        if(locale){
          window.localStorage.setItem('locale',lang)
        }else {
          window.localStorage['locale'] = lang;
        }
        console.log(window.localStorage.getItem('locale'))
      },
      handleCommand(command){
        if(command.value=='loginOut'){
          this.loginOut()
        }else {
          this.toView(0,command.value,command.json)
        }
      },
        // 退出登录
        loginOut(){
        this.$confirm('此操作将退出登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 退出的逻辑代码
          this.$store
            .dispatch("user/LOGOUT")
            .then(() => {
              // location.href = "/";
              this.$router.push({
                path: "/login",
              });
            })
            .catch((error) => {
              location.href = "/";
            });
          // this.toView(2,'login','')// 返回登录
          // window.localStorage.removeItem('token')
          window.sessionStorage.removeItem("token");
          this.getInitData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
      },
    toView(num, router, json) {
      this.$router.push({ name: router, query: { key: json } });
      // this.$store.state.router=router
      this.$store.commit("modify", router);
      // this.$router.push({name:'userCenterView'})
      //  this.$store.state.router=router
      // this.$store.commit('modify', router)
      window.localStorage.setItem("router", router);
      if (num == 1) {
        let i = {
          label: "用户中心",
          value: "userCenterView",
        };
        if (this.$Cookies.get('token')) {
          return (this.i = 2);
        } else if (this.$i18n.locale == "en") {
          return (this.i = 3);
        } else {
          return (this.i = 1);
        }
        this.$nextTick(() => {
          Bus.$emit("title", i);
        });

        //  this.BusChange(i)
      }
    }

    }
  }
  </script>

  <!-- Add "scoped" attribute to limit CSS to this component only -->
  <style scoped>
    body {
        margin: 0;
        padding: 0;
    }
    .header1 {
      width:100%;
      margin: auto;
      height: 70px;
      /* background: transparent; */
      background: rgba(50,75,116,.8);

    }
    .header2 {
      width:1200px;
      height: 70px;
      margin: auto;
      position: relative;
      /* background: url("../assets/img/home/banner.png") no-repeat; */
    }
    .language {
      line-height: 70px;
      color: #ccc;
      font-weight: 400;
      font-size: 14px;
      width:100%;
      /* background: rgba(50,75,116,.8); */
    }
    .logo {
      position: absolute;
      top: 50px;
      left: 80px;
      width: 260px;
      height: 55px;
      /* background-image: url(@/assets/logo.png); */
    }
    .title {
      position: absolute;
      top: 60px;
      left: 450px;
      font-size: 30px;
      font-weight: 700;
      color: #fff;
    }
    .active span:hover{
      color:#ff0000;
      cursor:pointer
    }
    .el-dropdown {
      color: #ccc;
      font-size: 12px;
    }
</style>
