<i18n>
{
  "en": {
    "info": "Info",
    "showall": "Show All",
    "groups": "Groups",
    "subgroups": "Subgroups",
    "clients": "Clients",
    "name": "Name",
    "description": "Description",
    "ipranges": "IP Ranges",
    "config": "iPXE Config",
    "parents": "Parents",
    "startIp": "Start IP",
    "endIp": "End IP",
    "selectParents": "Select parents"
  },
  "de": {
    "info": "Info",
    "showall": "Show All",
    "groups": "Groups",
    "subgroups": "Untergruppen",
    "clients": "Clienten",
    "name": "Name",
    "description": "Beschreibung",
    "ipranges": "IP Bereiche",
    "config": "iPXE Konfiguration",
    "parents": "Übergruppen",
    "startIp": "Start IP",
    "endIp": "End IP",
    "selectParents": "Übergruppen auswählen"
  }
}
</i18n>

<template>
  <div>
    <v-subheader v-if="group.id !== 0">Info</v-subheader>
    <v-card v-if="group.id !== 0">
      <v-card-text>
        <v-layout wrap>
          <v-flex lg4 sm6 xs12 order-lg1 order-xs2>
            <v-layout column>
              <v-flex>
                <div class="info-box">
                  <div class="body-2 info-heading"><v-icon>label</v-icon><span>{{ $t('name') }}</span></div>
                  <div class="info-text">
                    <v-text-field v-if="editMode" class="info-input" color="primary" v-model="info.name" hide-details></v-text-field>
                    <div v-else>{{ group.name || '-' }}</div>
                  </div>
                </div>
              </v-flex>
              <v-flex>
                <div class="info-box">
                  <div class="body-2 info-heading">
                    <v-icon>device_hub</v-icon><span>{{ $t('parents') }}</span>
                    <v-menu v-if="editMode" offset-y :close-on-content-click="false" :max-width="400" lazy>
                      <v-btn slot="activator" small icon  class="info-heading-button"><v-icon>edit</v-icon></v-btn>
                      <v-card>
                        <data-table ref="datatable" v-model="parents" :headers="headers" :items="groupList" menu-mode min-width="200px"></data-table>
                      </v-card>
                    </v-menu>
                  </div>
                  <div class="info-text">
                    <div>
                      <v-chip
                        v-for="(parent, index) in parentChips"
                        :key="parent.id"
                        v-if="index <= 5"
                        small
                        :close="editMode"
                        @input="removeParentChip(index)"
                      >
                        {{ parent.name || parent.id }}
                      </v-chip>
                      <span v-if="parentChips && parentChips.length > 5" class="and-more-chip">...</span>
                      <span v-if="parentChips && parentChips.length === 0">-</span>
                    </div>
                  </div>
                </div>
              </v-flex>
              <v-flex>
                <div class="info-box">
                  <div class="body-2 info-heading"><v-icon>list</v-icon><span>{{ $t('config') }}</span></div>
                  <div class="info-text">
                    <v-select v-if="editMode"
                      class="info-input"
                      clearable
                      item-text="name"
                      item-value="id"
                      :menu-props="{ offsetY: '' }"
                      hide-details
                      color="primary"
                      v-model="info.configId"
                      :items="configList"
                    ></v-select>
                    <div v-else>{{ configName || '-' }}</div>
                  </div>
                </div>
              </v-flex>
            </v-layout>
          </v-flex>
          <v-flex lg4 sm6 xs12 order-lg2 order-xs3>
            <div class="info-box">
              <div class="body-2 info-heading"><v-icon>description</v-icon><span>{{ $t('description') }}</span></div>
              <div class="info-text">
                <v-textarea v-if="editMode" class="info-input" rows="1" auto-grow hide-details color="primary" v-model="info.description"></v-textarea>
                <div v-else style="white-space: pre-wrap;">{{ group.description || '-' }}</div>
              </div>
            </div>

            <div class="info-box">
              <div class="body-2 info-heading">
                <v-icon>settings_ethernet</v-icon><span>{{ $t('ipranges') }}</span>
                <v-btn v-if="editMode" small icon @click="addIprange" class="info-heading-button"><v-icon>add</v-icon></v-btn>
              </div>
              <div class="info-text">
                <div v-if="editMode">
                  <div v-for="(iprange, index) in ipranges" :key="index" class="iprange">
                    <v-btn class="iprange-remove-button" small icon @click="removeIprange(index)"><v-icon>remove</v-icon></v-btn>
                    <v-text-field class="info-input" hide-details :label="$t('startIp')" color="primary" v-model="iprange.startIp" single-line></v-text-field>
                    <span class="ip-seperator">-</span>
                    <v-text-field class="info-input" hide-details :label="$t('endIp')" color="primary" v-model="iprange.endIp" single-line></v-text-field>
                  </div>
                  <div v-if="ipranges && ipranges.length === 0">-</div>
                </div>
                <div v-else>
                  <table>
                    <tr v-for="(iprange, index) in group.ipranges" :key="index">
                      <td class="text-xs-right">{{ iprange.startIp }}</td>
                      <td class="ip-seperator">-</td>
                      <td>{{ iprange.endIp }}</td>
                    </tr>
                  </table>
                  <div v-if="group.ipranges && group.ipranges.length === 0">-</div>
                </div>
              </div>
            </div>
          </v-flex>
          <v-flex lg4 xs12 order-lg3 order-xs1 class="text-xs-right">
            <div class="info-box">
              <div v-if="!editMode">
                <v-btn color="error" flat @click="deleteGroup" class="info-buttons">
                  <v-icon left>delete</v-icon>{{ $t('delete') }}
                </v-btn>
                <v-btn color="primary" flat @click="editInfo" class="info-buttons">
                  <v-icon left>create</v-icon>{{ $t('edit') }}
                </v-btn>
              </div>
              <div v-else>
                <v-btn color="primary" flat @click="cancelEdit" class="info-buttons">{{ $t('cancel') }}</v-btn>
                <v-btn color="primary" @click="saveData" class="info-buttons">
                  <v-icon left>save</v-icon>{{ $t('save') }}
                </v-btn>
              </div>
            </div>
          </v-flex>
        </v-layout>
      </v-card-text>
    </v-card>
    <template v-if="group.id !== 'create'">
      <v-layout>
        <v-spacer></v-spacer>
        <div><v-switch
          :input-value="group.tabShowAll"
          @change="setShowAll"
          class="show-toggle"
          :label="$t('showall')"
          hide-details
          color="primary"
        ></v-switch></div>
      </v-layout>
      <v-subheader>{{ group.id > 0 ? $t('subgroups') : $t('groups') }}</v-subheader>
      <group-module-group-list :tabIndex="tabIndex" :groupId="group.id" :groups="group.subgroups || []" />
      <v-subheader>{{ $t('clients') }}</v-subheader>
      <group-module-client-list :tabIndex="tabIndex" :groupId="group.id" :clients="group.clients || []" />
    </template>
  </div>
</template>

<script>
import GroupModuleGroupList from '@/components/GroupModuleGroupList'
import GroupModuleClientList from '@/components/GroupModuleClientList'
import DataTable from '@/components/DataTable'
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'GroupModuleGroupView',
  props: ['tabIndex', 'group'],
  components: {
    GroupModuleGroupList,
    GroupModuleClientList,
    DataTable
  },
  data () {
    return {
      editMode: false,
      info: {
        name: '',
        description: '',
        configId: null
      },
      parents: [],
      ipranges: []
    }
  },
  computed: {
    ...mapState('groups', ['groupList', 'configList']),
    configName () {
      return this.$store.state.groups.configNames[this.group.configId]
    },
    parentChips () {
      if (this.editMode) return this.parents
      else return this.group.parents
    },
    headers () {
      return [
        { key: 'name', text: this.$t('name') }
      ]
    }
  },
  watch: {
    group (newValue, oldValue) {
      if (newValue.id === 'create') this.editInfo()
      else if (newValue.id !== oldValue.id) this.editMode = false
    }
  },
  methods: {
    ...mapMutations('groups', ['setDialog']),
    setShowAll (value) {
      this.$store.commit('groups/setShowAll', { index: this.tabIndex, value })
      this.$store.dispatch('groups/loadGroup', { id: this.group.id, tabIndex: this.tabIndex, reload: true })
    },
    removeIprange (index) {
      this.ipranges.splice(index, 1)
    },
    addIprange () {
      this.ipranges.push({ startIp: '', endIp: '' })
    },
    removeParentChip (index) {
      this.parents.splice(index, 1)
    },
    editInfo () {
      this.editMode = true
      this.info.name = this.group.name
      this.info.description = this.group.description
      this.info.configId = this.group.configId
      this.parents = this.group.parents ? this.group.parents.slice(0) : []
      this.ipranges = this.group.ipranges ? this.group.ipranges.slice(0) : []
    },
    cancelEdit () {
      this.editMode = false
      if (this.group.id === 'create') {
        this.$store.commit('groups/deleteFromTabChain', { index: this.tabIndex, count: 1 })
        this.$store.commit('groups/setActiveTab', this.tabIndex - 1)
      }
    },
    saveData () {
      this.info.configId = this.info.configId === undefined ? null : this.info.configId
      this.ipranges = this.ipranges.filter(iprange => iprange.startIp && iprange.endIp)
      this.$store.dispatch('groups/saveGroup', {
        id: this.group.id,
        data: this.info,
        parents: this.parents,
        ipranges: this.ipranges,
        tabIndex: this.tabIndex,
        callback: this.updateUrl
      })
      this.editMode = false
    },
    deleteGroup () {
      this.setDialog({ show: true, info: { action: 'delete', type: 'group', selected: [this.group] } })
    },
    updateUrl (id) {
      this.$router.replace({
        name: 'GroupModule.group',
        params: { id, noReload: true }
      })
    }
  },
  created () {
    if (this.group.id === 'create') this.editInfo()
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.info-buttons {
  margin: 0;
}

.iprange {
  display: flex;
  align-items: center;
  min-height: 34px;
}

.iprange >>> input {
  font-size: 14px;
}

.ip-seperator {
  padding: 0 10px;
}

.iprange-remove-button {
  margin: 0 8px 0 0;
}

.info-box {
  padding: 20px;
}

.info-input {
  margin: 0;
  padding: 0 0 1px 0;
  overflow: hidden;
}

.info-heading {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.info-heading-button {
  margin: -2px 0 -2px 10px;
}

.info-heading > span {
  margin-left: 10px;
}

.info-text {
  overflow-x: auto;
  margin-left: 34px;
  font-family: 'Roboto Mono';
  min-height: 34px;
  display: flex;
  align-items: center;
}

.show-toggle {
  margin-top: 20px;
  margin-right: 20px;
  margin-bottom: -20px;
}
</style>
