<i18n>
{
  "en": {
    "id": "ID",
    "name": "Name",
    "description": "Description",
    "config": "Config",
    "ip": "IP",
    "mac": "MAC",
    "uuid": "UUID",
    "username": "Username",
    "deleted": "Deleted"
  },
  "de": {
    "id": "ID",
    "name": "Name",
    "description": "Beschreibung",
    "config": "Konfiguration",
    "ip": "IP",
    "mac": "MAC",
    "uuid": "UUID",
    "username": "Benutzername",
    "deleted": "Gelöscht"
  }
}
</i18n>

<template>
  <div style="display: flex; align-items: center;">
    <template v-if="item.client">
      <v-menu v-model="menus.client" :close-on-content-click="false" offset-y content-class="log-entry-menu-conent">
        <template #activator="{ on }">
          <v-btn v-on="on" outline small class="description-button" :class="{ 'error--text': item.client.deleted }">
            <v-icon small class="mr-1">computer</v-icon>{{ item.client.name }}
          </v-btn>
        </template>
        <template #default>
          <v-card style="user-select: text;"><v-card-text>
            <div v-if="item.client.deleted" class="error--text" style="text-align: center; font-weight: bold;">{{ $t('deleted') }}</div>
            <v-btn v-else :to="{ name: 'GroupModule.client', params: { id: item.client.id } }" @click.native="scrollToTop" class="open-button" icon small>
              <v-icon small>open_in_new</v-icon>
            </v-btn>
            <table>
              <tr><td class="snapshot-key">{{ $t('id') }}</td><td>{{ item.clientSnapshot.id }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('name') }}</td><td>{{ item.clientSnapshot.name }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('description') }}</td><td style="white-space: pre;">{{ item.clientSnapshot.description }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('ip') }}</td><td>{{ item.clientSnapshot.ip }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('mac') }}</td><td>{{ item.clientSnapshot.mac }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('uuid') }}</td><td>{{ item.clientSnapshot.uuid }}</td></tr>
            </table>
          </v-card-text></v-card>
        </template>
      </v-menu>
    </template>

    <template v-if="item.group">
      <v-menu v-model="menus.group" :close-on-content-click="false" offset-y content-class="log-entry-menu-conent">
        <template #activator="{ on }">
          <v-btn v-on="on" outline small class="description-button" :class="{ 'error--text': item.group.deleted }">
            <v-icon small class="mr-1">category</v-icon>{{ item.group.name }}
          </v-btn>
        </template>
        <template #default>
          <v-card style="user-select: text;"><v-card-text>
            <div v-if="item.group.deleted" class="error--text" style="text-align: center; font-weight: bold;">{{ $t('deleted') }}</div>
            <v-btn v-else :to="{ name: 'GroupModule.group', params: { id: item.group.id } }" @click.native="scrollToTop" class="open-button" icon small>
              <v-icon small>open_in_new</v-icon>
            </v-btn>
            <table>
              <tr><td class="snapshot-key">{{ $t('id') }}</td><td>{{ item.groupSnapshot.id }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('name') }}</td><td>{{ item.groupSnapshot.name }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('description') }}</td><td style="white-space: pre;">{{ item.groupSnapshot.description }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('config') }}</td><td style="white-space: pre;">{{ item.groupSnapshot.configId || "-" }}</td></tr>
            </table>
          </v-card-text></v-card>
        </template>
      </v-menu>
    </template>

    <template v-if="item.user">
      <v-menu v-model="menus.user" :close-on-content-click="false" offset-y content-class="log-entry-menu-conent">
        <template #activator="{ on }">
          <v-btn v-on="on" outline small class="description-button" :class="{ 'error--text': item.user.deleted }">
            <v-icon small class="mr-1">person</v-icon>{{ item.user.name }}
          </v-btn>
        </template>
        <template #default>
          <v-card style="user-select: text;"><v-card-text>
            <div v-if="item.user.deleted" class="error--text" style="text-align: center; font-weight: bold;">{{ $t('deleted') }}</div>
            <table>
              <tr><td class="snapshot-key">{{ $t('id') }}</td><td>{{ item.userSnapshot.id }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('username') }}</td><td>{{ item.userSnapshot.username }}</td></tr>
              <tr><td class="snapshot-key">{{ $t('name') }}</td><td>{{ item.userSnapshot.name }}</td></tr>
            </table>
          </v-card-text></v-card>
        </template>
      </v-menu>
    </template>

    <v-menu
      v-if="item.descriptionHeader"
      v-model="menus.description"
      :close-on-content-click="false"
      nudge-top="12"
      nudge-left="16"
      content-class="log-entry-menu-conent"
    >
      <template #activator="{ on }">
        <v-btn v-on="on" small icon style="margin: 0 2px 0 0; overflow: hidden;">
          <v-icon :style="menus.description ? 'transform: rotate(180deg)' : ''">arrow_drop_down</v-icon>
        </v-btn>
      </template>
      <template #default>
        <v-card style="display: flex; padding: 16px">
          <v-btn small icon style="margin: -4px 2px 0 0; overflow: hidden;" @click="menus.description = false">
            <v-icon :style="menus.description ? 'transform: rotate(180deg)' : ''">arrow_drop_down</v-icon>
          </v-btn>
          <div style="user-select: text; white-space: pre;">{{ item.description }}</div>
        </v-card>
      </template>
    </v-menu>
    <div style="user-select: text;" @click="menus.description = true">{{ item.descriptionHeader || item.description }}</div>
  </div>
</template>

<script>

export default {
  name: 'LogModuleEntry',
  props: {
    item: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      menus: { description: false, client: false, group: false, user: false }
    }
  },
  watch: {
    item () {
      this.menus = { description: false, client: false, group: false, user: false }
    },
    menus: {
      deep: true,
      handler (menus) {
        if (menus.description || menus.client || menus.group || menus.user) this.$emit('menu-translate', this.$el.offsetParent.style.transform)
      }
    }
  },
  methods: {
    scrollToTop () {
      window.scrollTo(0, 0)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.description-button {
  min-width: 10px;
  margin: 0 10px 0 0;
}

.description-button >>> .v-btn__content {
  max-width: 180px;
  overflow: hidden;
  display: block;
  text-overflow: ellipsis;
  text-transform: none;
}

.snapshot-key {
  vertical-align: top;
  font-weight: bold;
  text-align: right;
  padding-right: 16px;
}

.open-button {
  position: absolute;
  top: 0;
  left: 0;
}
</style>

<style>
.log-entry-menu-conent {
  font-size: 13px;
  font-family: 'Roboto Mono';
}
</style>
