<i18n>
{
  "en": {
  },
  "de": {
  }
}
</i18n>

<template>
  <v-container fill-height>
    <v-layout>
      <v-flex class="tabs-wrapper" xl10 offset-xl1 lg12>
        <v-card class="tabbar-card">
          <v-tabs :value="activeTab" @change="setActiveTab" :dark="tabsDark" :color="tabsColor" :slider-color="tabsSliderColor">
            <template v-for="(item, index) in tabChain">
              <v-icon v-if="item.id > 0 || item.id === 'create'" :key="'arrow' + index">keyboard_arrow_right</v-icon>
              <v-tab ripple :key="'tab' + index">
                <v-icon v-if="item.tabType === 'group' && item.id === 0">home</v-icon>
                <v-icon v-if="item.tabType === 'client'" style="margin-right: 10px">computer</v-icon>
                <template v-if="item.id > 0 || item.id === 'create'">{{ item.name ? item.name : item.id !== 'create' ? item.id : ' . . . . . . . . . . ' }}</template>
              </v-tab>
            </template>
          </v-tabs>
        </v-card>
        <v-tabs-items :value="activeTab" @input="setActiveTab" touchless style="padding-bottom: 20px">
          <v-tab-item v-for="(item, index) in tabChain" :key="index">
            <group-module-group-view v-if="item.tabType === 'group'" :group="item" :tabIndex="index" />
            <group-module-client-view v-if="item.tabType === 'client'" :client="item" :tabIndex="index" />
          </v-tab-item>
        </v-tabs-items>
      </v-flex>
    </v-layout>
    <group-module-dialog />
  </v-container>
</template>

<script>
import GroupModuleGroupView from '@/components/GroupModuleGroupView'
import GroupModuleClientView from '@/components/GroupModuleClientView'
import GroupModuleDialog from '@/components/GroupModuleDialog'
import { mapState, mapGetters, mapMutations } from 'vuex'

export default {
  name: 'GroupModule',
  routes () {
    return [
      { name: 'group', path: ':id' },
      { name: 'client', path: 'client/:id' }
    ]
  },
  components: {
    GroupModuleGroupView,
    GroupModuleClientView,
    GroupModuleDialog
  },
  data () {
    return {
    }
  },
  computed: {
    ...mapGetters(['tabsDark', 'tabsColor', 'tabsSliderColor']),
    ...mapState('groups', ['tabChain', 'activeTab', 'groupList', 'clientList'])
  },
  watch: {
    activeTab (index) {
      const item = this.tabChain[index]
      if (item.tabType !== this.$route.name.replace('GroupModule.', '') || String(item.id) !== this.$route.params.id) {
        this.$router.push({
          name: 'GroupModule.' + item.tabType,
          params: { id: item.id !== undefined ? item.id : 'create', noReload: true }
        })
      }
    }
  },
  methods: {
    ...mapMutations('groups', ['setActiveTab', 'setTab']),
    loadItem (routeName, id) {
      const type = routeName.replace('GroupModule.', '')
      if (id === 'create') {
        this.setTab({ index: 1, item: { id: 'create', tabType: type } })
        this.setActiveTab(1)
      } else {
        const action = type === 'group' ? 'loadGroup' : type === 'client' ? 'loadClient' : null
        if (action) this.$store.dispatch('groups/' + action, { id, tabIndex: 1, switchTab: true })
      }
    }
  },
  created () {
    this.$store.dispatch('groups/loadConfigs')
    if (this.groupList.length === 0 || this.clientList.length === 0) this.$store.dispatch('groups/loadLists')
    if (this.tabChain.length === 0) this.$store.dispatch('groups/loadGroup', { id: 0, tabIndex: 0 })
    if (this.$route.params.id !== '0') this.loadItem(this.$route.name, this.$route.params.id)
    this.$router.replace({
      name: 'GroupModule.' + this.tabChain[this.activeTab].tabType,
      params: { id: this.tabChain[this.activeTab].id, noReload: true }
    })
  },
  beforeRouteUpdate (to, from, next) {
    if (!to.params.noReload) {
      var tabIndex
      for (var i = 0; i < this.tabChain.length; i++) {
        if (this.tabChain[i].tabType === to.name.replace('GroupModule.', '') && String(this.tabChain[i].id) === to.params.id) {
          tabIndex = i
          break
        }
      }
      console.log(tabIndex)
      if (tabIndex === undefined) this.loadItem(to.name, to.params.id)
      else this.setActiveTab(tabIndex)
    }
    next()
  }
}
</script>
