<i18n>
{
  "en": {
    "addBackendBtn": "Add Backend",
    "backendId": "id",
    "backendName": "name",
    "backendType": "type",
    "removeBackend": "Remove one backend | Remove {0} backends",
    "checkConnection": "Check one connection | Check {0} connections",
    "name": "Name",
    "type": "Type",
    "id": "ID"
  },
  "de": {
    "addBackendBtn": "Backend hinzufügen",
    "backendId": "id",
    "backendName": "name",
    "backendType": "typ",
    "removeBackend": "Entferne ein Backend | Entferne {0} Backends",
    "checkConnection": "Überprüfe eine Verbindung | Überprüfe {0} Verbindungen",
    "name": "Name",
    "type": "Type",
    "id": "ID"
  }
}
</i18n>

<template>
  <div>
    <v-card>
      <component-search-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 :style="props.color" @click="props.data.selected = !props.data.selected">
            <td>
              <v-checkbox
                color="primary"
                v-model="props.data.selected"
                hide-details
                @click.native.stop
              ></v-checkbox>
            </td>
            <td>{{ props.data.item.id }}</td>
            <td>{{ props.data.item.name }}</td>
            <td>{{ props.data.item.type }}</td>
            <td>
              <v-layout>
                <v-btn
                  flat
                  icon
                  :loading="props.data.item.loading"
                  :disabled="props.data.item.loading"
                  :color="props.data.item.connection"
                  @click.stop="checkConnection(props.data.item)"
                >
                  <v-icon>cached</v-icon>
                </v-btn>
                <v-btn flat icon color="primary" @click.stop="$store.commit('backends/editSync', props.data.item.id)">
                  <v-icon>settings_ethernet</v-icon>
                </v-btn>
                <v-btn flat icon color="primary" @click.stop="$store.commit('backends/editBackend', props.data.item.id)">
                  <v-icon>edit</v-icon>
                </v-btn>
              </v-layout>
            </td>
          </tr>
        </template>
      </component-search-table>
    </v-card>
    <div class="text-xs-right">
      <v-btn color="primary" flat @click="checkConnections(selected)" :disabled="selected.length === 0">
        <v-icon left>cached</v-icon>{{ $tc('checkConnection', selected.length, [selected.length]) }}
      </v-btn>
      <v-btn color="error" flat @click="$store.commit('backends/setDialog', true)" :disabled="selected.length === 0">
        <v-icon left>remove_circle_outline</v-icon>{{ $tc('removeBackend', 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('addBackendBtn') }}
      </v-btn>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import ComponentSearchTable from '@/components/ComponentSearchTable'

export default {
  name: 'BackendModuleTable',
  components: {
    ComponentSearchTable
  },
  data () {
    return {
      dialog: false,
      backendName: '',
      backendId: '',
      backendType: '',
      test: false,
      edit: false
    }
  },
  methods: {
    checkConnection (item) {
      // Set to start the loading animation.
      item.loading = true
      // Test the credential connection.
      this.$http.post('/api/backends/' + item.id + '/connection', {
        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.error)
        }
        // 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']),
    headers () {
      return [
        { text: this.$t('id'), value: 'id' },
        { text: this.$t('name'), value: 'name', width: '10000px' },
        { text: this.$t('type'), value: 'type' },
        { sortable: false }
      ]
    }
  },
  created () {
    this.$store.dispatch('backends/loadData')
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
