<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 class="tab-bar-item-create__icon"></div>
            <div class="tab-bar-item-create__title">创建</div>
          </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: 80px;
}
.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 -8px 16px 0px rgba(0, 0, 0, 0.05);
}
.tab-bar-list {
  display: flex;
}
.tab-bar-item {
  flex: 1;
  font-size: 12px;
  color: #999;
  text-align: center;
}
.tab-bar-item.is-active {
  color: #ff6767;
}
.tab-bar-item-create {
  margin: -20px auto 0;
}
.tab-bar-item-create__icon {
  width: 60px;
  height: 60px;
  margin: 0 auto;
  background: #fff url('~@/assets/img/tab_create.png') no-repeat center center;
  background-size: 100%;
  border-radius: 50%;
}
.tab-bar-item-create__title {
  margin-top: -10px;
}
.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>
