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

<template>
  <div>
    <template v-if="tabIndex > 0">
      <v-subheader>Info</v-subheader>
      <v-card>
        <v-card-text>
          <v-layout wrap>
            <v-flex sm6 xs12 order-xs2 order-sm1>
              <v-text-field v-if="editMode" hide-details class="info-input" label="Name" box color="primary" v-model="info.name"></v-text-field>
              <div v-else class="info-input">
                <div class="body-2">Name</div>
                {{ group.name || '-' }}
              </div>
            </v-flex>
            <v-flex sm6 xs12 order-xs1 order-sm2 class="text-xs-right">
              <div class="info-input">
                <v-btn v-if="!editMode" color="primary" flat @click="editInfo">
                  <v-icon left>create</v-icon>Edit
                </v-btn>
                <div v-else>
                  <v-btn color="primary" flat @click="editMode = false">Cancel</v-btn>
                  <v-btn color="primary" @click="saveInfo">
                    <v-icon left>save</v-icon>Save
                  </v-btn>
                </div>
              </div>
            </v-flex>
          </v-layout>
          <v-layout wrap>
            <v-flex sm6 xs12>
              <v-textarea v-if="editMode" rows="1" auto-grow hide-details class="info-input" label="Description" box color="primary" v-model="info.description"></v-textarea>
              <div v-else class="info-input">
                <div class="body-2">Description</div>
                <pre>{{ group.description || '-' }}</pre>
              </div>
            </v-flex>
            <v-flex sm6 xs12>
              <v-autocomplete
                v-if="editMode"
                class="info-input"
                :items="groupList"
                v-model="info.parents"
                hide-details
                offset-y
                label="Parents"
                box
                color="primary"
                multiple
              >
                <template slot="selection" slot-scope="data">
                  <v-chip small :color="chipColor" :text-color="chipTextColor" :selected="data.selected" @input="removeParent(data.item.value)" close>
                    {{ data.item.text }}
                  </v-chip>
                </template>
              </v-autocomplete>
              <div v-else class="info-input">
                <div class="body-2">Parents</div>
                <template v-if="group.parents.length > 0">
                  <v-chip v-for="parent in group.parents" :key="parent.id" small>
                    {{ parent.name || parent.id }}
                  </v-chip>
                </template>
                <span v-else>-</span>
              </div>
            </v-flex>
          </v-layout>
        </v-card-text>
      </v-card>
    </template>
    <v-subheader>{{ tabIndex === 0 ? 'Groups' : 'Subgoups' }}</v-subheader>
    <group-module-group-list :tabIndex="tabIndex" />
    <v-subheader>Clients</v-subheader>
    <group-module-client-list :tabIndex="tabIndex" />
  </div>
</template>

<script>
import GroupModuleGroupList from '@/components/GroupModuleGroupList'
import GroupModuleClientList from '@/components/GroupModuleClientList'

export default {
  name: 'GroupModuleGroupView',
  props: ['tabIndex'],
  components: {
    GroupModuleGroupList,
    GroupModuleClientList
  },
  data () {
    return {
      editMode: false,
      info: {
        name: '',
        description: '',
        parents: []
      }
    }
  },
  watch: {
    groupId (v) {
      this.editMode = false
    }
  },
  computed: {
    groupList () {
      return this.$store.state.groups.groupList
    },
    group () {
      return this.$store.state.groups.groupChain[this.tabIndex]
    },
    groupId () {
      return this.group.id
    },
    chipColor () {
      return this.$store.state.settings.dark ? 'grey darken-1' : 'white'
    },
    chipTextColor () {
      return this.$store.state.settings.dark ? 'white' : 'black'
    }
  },
  methods: {
    removeParent (id) {
      this.info.parents.splice(this.info.parents.indexOf(id), 1)
    },
    editInfo () {
      this.editMode = true
      this.info.name = this.group.name
      this.info.description = this.group.description
      this.info.parents = this.group.parents.map(x => x.id)
    },
    saveInfo () {

    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.info-input {
  margin: 10px;
}
</style>
