<template>
  <div class="tab-bar">
    <div class="tab-bar-inner">
      <ul class="tab-bar-list">
        <li
          class="tab-bar-item tab-bar-item__home"
          :class="{ 'is-active': isActive('home') }"
          @click="$router.push('home')"
        >首页</li>
        <li class="tab-bar-item" @click="$router.push({ name: 'createActivity' })" v-if="hasCreate">
          <div class="tab-bar-item-create"></div>
        </li>
        <li
          class="tab-bar-item tab-bar-item__my"
          :class="{ 'is-active': isActive('my') }"
          @click="$router.push('my')"
        >我的</li>
      </ul>
    </div>
  </div>
</template>

<script>
import * as api from '@/api'
export default {
  data() {
    return {}
  },
  computed: {
    groupList() {
      return this.$store.state.groupList
    },
    hasCreate() {
      return !!this.groupList.find(item => item.is_mine)
    }
  },
  methods: {
    isActive(path) {
      return this.$route.name === path
    },
    // 获取我加入的组织
    getJoinedGroupes() {
      api.getJoinedGroupes().then(response => {
        const list = Array.isArray(response) ? response : []
        this.$store.commit('setGroupList', list)
      })
    }
  },
  beforeMount() {
    if (!this.groupList.length) {
      this.getJoinedGroupes()
    }
  }
}
</script>

<style lang="scss">
.tab-bar {
  height: 50px;
}
.tab-bar-inner {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 50px;
  background-color: #fff;
  max-width: 750px;
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
  box-sizing: content-box;
  box-shadow: 0px -3px 8px 0px rgba(0, 0, 0, 0.2);
}
.tab-bar-list {
  display: flex;
  padding-top: 5px;
}
.tab-bar-item {
  flex: 1;
  font-size: 12px;
  color: #444;
  text-align: center;
  line-height: 1;
}
.tab-bar-item.is-active {
  color: #af1b40;
}
.tab-bar-item-create {
  width: 40px;
  height: 40px;
  margin: 0 auto;
  background: #fff url('~@/assets/img/tab_create.png') no-repeat center center;
  background-size: 100%;
  border-radius: 50%;
}
.tab-bar-item__home {
  padding-top: 30px;
  background: url('~@/assets/img/tab_home.png') no-repeat center 2px;
  background-size: 27px auto;
}
.tab-bar-item__home.is-active {
  background-image: url('~@/assets/img/tab_home_hover.png');
}
.tab-bar-item__my {
  padding-top: 30px;
  background: url('~@/assets/img/tab_my.png') no-repeat center 2px;
  background-size: 23px auto;
}
.tab-bar-item__my.is-active {
  background-image: url('~@/assets/img/tab_my_hover.png');
}
</style>
