<i18n>
{
  "en": {
    "title": "Delete this user? | Delete these {0} users?",
    "deleteYourself": "You are about to delete yourself!"
  },
  "de": {
    "title": "Diesen Benutzer löschen? | Diese {0} Benutzer löschen?",
    "deleteYourself": "Du bist dabei dich selber zu löschen!"
  }
}
</i18n>

<template>
  <v-card :color="doIDeleteMyself ? 'error' : ''">
    <v-card-title class="dialog-title elevation-3" >
      <div class="headline">{{ $tc('title', dialog.info.selected.length, [dialog.info.selected.length]) }}</div>
    </v-card-title>
    <v-card-text style="height: 100%">
      <div v-for="item in dialog.info.selected" class="grey--text" :key="item.id">
        <div v-if="item.id === dialog.info.id" class="black--text">[{{ item.id }}] {{ item.name }}</div>
        <div v-else :class="doIDeleteMyself ? 'white--text' : ''">[{{ item.id }}] {{ item.name }}</div>
      </div>
      <div v-if="doIDeleteMyself" class="white--text" style="margin-top: 20px;">{{ $t('deleteYourself') }}</div>
    </v-card-text>
    <v-divider></v-divider>
    <v-card-actions>
      <v-spacer></v-spacer>
      <v-btn text="flat" @click="setDialog({ show: false })">{{ $t('cancel') }}</v-btn>
      <v-btn :color="doIDeleteMyself ? 'white' : 'error'" @click="deleteItems"><div :class="doIDeleteMyself ? 'red--text' : 'white--text'">{{ $t('delete') }}</div></v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'UserModuleDelete',
  data () {
    return {
    }
  },
  computed: {
    ...mapState('users', ['dialog']),
    doIDeleteMyself () {
      return this.dialog.info.selected.map(x => x.id).includes(this.dialog.info.id)
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('users/setDialog', data)
    },
    async deleteItems () {
      await this.$http.post('/api/users/?delete', {
        ids: this.dialog.info.selected.map(x => x.id)
      })
      if (this.doIDeleteMyself) {
        this.$http.post('/api/authentication/logout').then(response => {
          this.$router.push('/login')
          this.$socket.close()
        })
      }
      this.$store.dispatch('users/loadData')
      this.setDialog({ show: false })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.dialog-title {
  z-index: 1;
}
.selected-list {
  padding: 30px;
}
</style>
