<template>
  <div class="toolBar">
    <div class="item" v-if="userInfo" @click="go('/cart')">
      <el-badge :value="cartCount || ''">
        <i class="img el-icon-shopping-cart-2"></i>
        <div class="text">购物车</div>
      </el-badge>
    </div>

    <div class="item">
      <el-popover placement="left" width="180" trigger="hover">
        <img
          style="width: 150px; height: 150px"
          src="@/assets/xcxqrcode.jpg"
          alt=""
        />
        <img slot="reference" src="@/assets/qr.png" class="img" alt="" />
      </el-popover>
      <div class="text">二维码</div>
    </div>

    <div class="item">
      <div class="text blue">020<br />87085982</div>
      <div class="text">报名咨询</div>
    </div>
    <div class="item" @click="scollTop">
      <img src="@/assets/top.png" class="img" alt="" />
      <div class="text">返回顶部</div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapMutations } from "vuex";
export default {
  name: "ToolBar",
  computed: {
    ...mapGetters(["userInfo", "cartCount"]),
  },
  data() {
    return {
      scrollTimer: null,
    };
  },
  mounted() {
    if (this.$tools.isLogin()) {
      this.getCartCount();
    }
  },
  methods: {
    ...mapMutations(["getCartCount"]),
    go(path) {
      this.$router.push({
        path,
      });
    },
    scollTop() {
      clearInterval(this.scrollTimer);
      this.scrollTimer = setInterval(() => {
        const top = document.documentElement.scrollTop;
        const speed = top / 10;
        if (document.documentElement.scrollTop !== 0) {
          document.documentElement.scrollTop -= speed;
        } else {
          clearInterval(this.scrollTimer);
        }
      }, 16);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.toolBar {
  border: 1px solid #eeeeee;
  box-shadow: 0px 0px 7px 1px rgba(7, 82, 208, 0.04);
  border-radius: 10px;
  padding: 4px;
  position: fixed;
  right: 0;
  top: 380px;
  background: #fff;
  display: flex;
  flex-direction: column;
  z-index: 12;
  .item {
    cursor: pointer;
    width: 78px;
    height: 80px;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #3f8dfd;
    position: relative;

    &:nth-last-of-type(1) {
      border: 0;
    }

    .img {
      width: 24px;
      font-size: 24px;
      height: 24px;
    }

    .text {
      text-align: center;
      word-break: break-all;
      font-size: 12px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #999999;
      line-height: 24px;
    }

    .blue {
      font-weight: bold;
      font-size: 14px;
      color: #3f8dfd;
    }

    .el-badge {
      text-align: center;
    }
  }
}
</style>
