<template>
  <div class="navbar">
    <!-- <hamburger :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" /> -->

    <breadcrumb class="breadcrumb-container" />

    <div class="right-menu">
      <div v-if="user.userType != 9" class="check">
        <!-- <el-select v-model="languageItem"
                   @change="setLanguage"
                   popper-class="selectCls"
                   size="mini">
          <el-option v-for="item in languageList"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select> -->

        <el-dropdown trigger="click" placement="bottom-start">
          <div class="kuang">
            <div class="dropdown_i">
              <img :src="ruleForm.nationalFlag2" alt="">
              <div>{{ ruleForm.name }}</div>
            </div>
            <i slot="suffix" class="el-icon-arrow-down" />
          </div>
          <el-dropdown-menu slot="dropdown" class="dropdown-container">
            <el-dropdown-item v-for="(item, i) in countryList" :key="i">
              <div class="dropdown_i" @click="handleClick(item)">
                <img :src="item.nationalFlag2" alt="">
                <div>{{ item.name }}</div>
              </div>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div class="login" @click="logout">退出登录</div>
      <!-- <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar">
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown" class="user-dropdown">
          <router-link to="/">
            <el-dropdown-item>
              Home
            </el-dropdown-item>
          </router-link>

          <el-dropdown-item divided @click.native="logout">
            <span style="display:block;">登出</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown> -->
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import { Message } from 'element-ui'
import {
  userCountryList,
  changeList,
  languages,
  changeSystemList
} from '@/api/user'
export default {
  components: {
    Breadcrumb,
    Hamburger
  },
  computed: {
    ...mapGetters(['sidebar', 'avatar', 'user'])
  },
  data() {
    return {
      countryList: [],
      ruleForm: {
        pkCountry: '',
        nationalFlag2: '',
        name: ''
      },
      languageList: [],
      languageItem: 0,
      systemItem: ''
    }
  },
  mounted() {
    const that = this
    setTimeout(() => {
      // 获取结算国列表
      that.getJScountry()
      // 获取语言列表
      that.getLanguages()
      // that.getSystem()
    }, 10)

    this.systemItem = localStorage.getItem('systemItem') || 2
  },
  methods: {
    getJScountry() {
      userCountryList().then((res) => {
        this.countryList = res.data
        this.ruleForm.pkCountry = this.user.pkCountry
        this.countryList.forEach((item) => {
          if (item.pkCountry == this.ruleForm.pkCountry) {
            this.ruleForm.nationalFlag2 = item.nationalFlag2
            this.ruleForm.name = item.name
          }
        })
      })
    },

    getLanguages() {
      languages().then((res) => {
        res.data.forEach((item) => {
          if (item.field == localStorage.getItem('lang')) {
            this.languageItem = item.value
          }
        })
        if (
          localStorage.getItem('lang') == undefined ||
          localStorage.getItem('lang') == 'undefined'
        ) {
          localStorage.setItem('lang', 'zh-CN')
        }

        this.languageList = res.data
      })
    },
    handleClick(item) {
      this.ruleForm.nationalFlag2 = item.nationalFlag2
      this.ruleForm.name = item.name
      changeList(item.pkCountry).then((res) => {
        this.$store.dispatch('user/getInfo').then((response) => {
          this.$router.go(0)
        })
      })
    },
    changeSelection(e) {},
    setSystem(e) {
      console.log(e, 'eeee')
      this.systemItem = e
      changeSystemList(e).then((res) => {
        if (res.code == 200) {
          Message({
            message: res.msg,
            type: 'success'
          })
          localStorage.setItem('systemItem', e)
          location.reload()
        }
      })
    },
    // setLanguage(e) {
    //   this.languageList.forEach((item) => {
    //     if (item.value == e) {
    //       localStorage.setItem("lang", item.field);
    //       this.$i18n.locale = item.field;
    //     }
    //   });
    //   location.reload();
    // },
    // toggleSideBar() {
    //   this.$store.dispatch('app/toggleSideBar')
    // },
    // async logout() {
    //   await this.$store.dispatch('user/logout')
    //   this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    // },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', this.$t('w_0034'), {
        confirmButtonText: this.$t('w_0035'),
        cancelButtonText: this.$t('ENU_P_TYPE0'),
        type: 'warning'
      })
        .then(() => {
          this.$store.dispatch('user/LogOut').then(() => {
            this.$router.push(`/login?redirect=${this.$route.fullPath}`)
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-popper[x-placement^="bottom"] {
  top: 35px !important;
}
::v-deep .el-input__suffix {
  top: -2px;
}
::v-deep .el-select .el-input .el-select__caret {
  color: #fff;
}
::v-deep .el-dropdown-menu__item:hover {
  background: #c8161d !important;
  .dropdown_i:hover {
    color: #fff !important;
  }
}
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
    margin-left: 0;
  }

  .right-menu {
    // float: right;
    display: flex;
    justify-content: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }
    .check {
      margin-right: 20px;
      display: flex;
      align-items: center;
      // ::v-deep .el-input--suffix {
      //   height: 40px;
      //   line-height: 40px;
      //   background: #fff;
      //   border-radius: 4px;
      // }
      ::v-deep .el-input {
        width: 154px;
        margin-right: 20px;
      }
    }
    .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 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

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

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .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;
        }
      }
    }
    .login {
      color: #333;
      margin-right: 20px;
      cursor: pointer;
    }
  }
}

.guoqi {
  background: #fff;
  .el-select-dropdown__item {
    height: 26px;
    // text-align: center;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
  }
  .shortName {
    margin-left: 10px;
  }
}
.imgSize {
  height: 20px;
}
.kuang {
  width: 154px;
  height: 28px;
  border-radius: 4px 4px 4px 4px;
  padding: 0 10px;
  // background: rgba(255, 112, 117, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid #dcdfe6;
}
.dropdown_i {
  display: flex;
  align-items: center;
  color: #666;
  img {
    margin-right: 10px;
    width: 28px;
    height: 19px;
  }
}
::v-deep .el-input__inner {
  //border-color: rgba(255, 255, 255, 1);
  background: #c8161d !important;
  color: #fff;
  //background: rgba(255, 112, 117, 0.1);
  :hover {
    border-color: rgba(255, 255, 255, 1) !important;
  }
  :focus {
    border-color: rgba(255, 255, 255, 1) !important;
  }
}
::v-deep .el-select {
  :hover {
    border-color: rgba(255, 255, 255, 1) !important;
  }
  :focus {
    border-color: rgba(255, 255, 255, 1) !important;
  }
}
</style>
