<i18n>
{
  "en": {
    "add-backend-btn": "Add Backend",
    "backend-id": "id",
    "backend-name": "name",
    "backend-type": "type",
    "delete-backend": "Delete {0} backend | Delete {0} backends",
    "check-connection": "Check {0} connection | Check {0} connections"
  },
  "de": {
    "add-backend-btn": "Backend hinzufügen",
    "backend-id": "id",
    "backend-name": "name",
    "backend-type": "typ",
    "delete-backend": "Lösche {0} Backend | Lösche {0} Backends",
    "check-connection": "Überprüfe {0} Verbindung | Überprüfe {0} Verbindungen"
  }
}
</i18n>

<template>
  <div>
    <v-card>
      <v-data-table
        class="group-table"
        :headers="headers"
        :items="backends"
        item-key="id"
        hide-actions
        select-all
        :value="selected"
        @input="$store.commit('backends/setSelected', $event)"
      >
        <template slot="items" slot-scope="props">
          <tr @click="props.selected = !props.selected">
            <td>
              <v-checkbox
                color="primary"
                v-model="props.selected"
                hide-details
                @click.native.stop
              ></v-checkbox>
            </td>
            <td>{{ props.item.id }}</td>
            <td>{{ props.item.name }}</td>
            <td>{{ props.item.type }}</td>
            <td>
              <v-layout>
                <v-btn
                  flat
                  icon
                  :loading="props.item.loading"
                  :disabled="props.item.loading"
                  :color="props.item.connection"
                  @click.stop="checkConnection(props.item)"
                >
                  <v-icon>cached</v-icon>
                </v-btn>
                <v-btn flat icon color="primary" @click.stop="$store.commit('backends/editBackend', props.item.id)">
                  <v-icon>edit</v-icon>
                </v-btn>
              </v-layout>
            </td>
          </tr>
        </template>
      </v-data-table>
    </v-card>
    <div class="text-xs-right">
      <v-btn color="primary" flat @click="checkConnections(selected)">
        <v-icon left>cached</v-icon>{{ $tc('check-connection', selected.length, [selected.length]) }}
      </v-btn>
      <v-btn color="error" flat @click="$store.commit('backends/setDialog', { show: true, del: true } )"><!-- @click="deleteBackends(selected)"-->
        <v-icon left>remove_circle_outline</v-icon>{{ $tc('delete-backend', selected.length, [selected.length]) }}
      </v-btn>
      <v-btn color="success" flat @click="$store.commit('backends/editBackend', 0)">
        <v-icon left>add_circle_outline</v-icon>{{ $t('add-backend-btn') }}
      </v-btn>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'

export default {
  name: 'BackendModuleTable',
  data () {
    return {
      dialog: false,
      backendName: '',
      backendId: '',
      backendType: '',
      test: false,
      headers: [
        { text: 'ID', value: 'id' },
        { text: 'Name', value: 'name', width: '10000px' },
        { text: 'Type', value: 'type' },
        { sortable: false }
      ]
    }
  },
  methods: {
    checkConnection (item) {
      // Set to start the loading animation.
      item.loading = true
      // Test the credential connection.
      this.$http('/api/backends/checkConnection?id=' + item.id, {
        headers: {
          'Cache-Control': 'no-cache'
        }
      }).then(response => {
        if (response.data.success) {
          // Set the button color to green if success.
          item.connection = 'success'
        } else {
          // Set the button color to red if error.
          item.connection = 'error'
          this.$store.commit('newSnackbar', response.data.msg)
        }
        // Set item.loading = false to end the loading animation.
        item.loading = false
      })
    },
    checkConnections (items) {
      const tmp = this
      items.forEach(function (item) {
        tmp.checkConnection(item)
      })
    }
  },
  watch: {
  },
  computed: {
    ...mapState('backends', ['selected', 'backends'])
  },
  created () {
    this.$store.dispatch('backends/loadData')
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
