<i18n>
{
  "en": {
    "name": "Name",
    "description": "Description",
    "type": "Script type",
    "groups": "Groups",
    "script": "Script",
    "titleNew": "Create hook",
    "titleExisting": "Edit hook"
  },
  "de": {
    "name": "Name",
    "description": "Beschreibung",
    "type": "Skript Art",
    "groups": "Gruppen",
    "script": "Skript",
    "titleNew": "Hook erstellen",
    "titleExisting": "Hook bearbeiten"
  }
}
</i18n>

<template>
  <v-card>
    <v-card-title primary-title class="dialog-title elevation-3">
      <div class="headline">{{ dialog.info.id ? $t('titleExisting') : $t('titleNew') }}</div>
    </v-card-title>
    <v-card-text>
      <v-layout row wrap>
        <v-flex xs12 sm9>
          <v-text-field prepend-icon="label" :label="$t('name')" color="primary" v-model="name"></v-text-field>
        </v-flex>
        <v-flex xs12 sm2 offset-sm1>
          <v-select prepend-icon="label" color="primary" :items="types" :label="$t('type')" v-model="type" menu-props="offsetY"></v-select>
        </v-flex>
      </v-layout>
      <v-autocomplete
              prepend-icon="device_hub"
              :items="groupList"
              v-model="groups"
              :label="$t('groups')"
              color="primary"
              multiple
              item-value="id"
              item-text="name"
              small-chips
              deletable-chips
      ></v-autocomplete>
      <v-textarea prepend-icon="description" rows="3" :label="$t('description')" color="primary" v-model="description"></v-textarea>
      <v-textarea prepend-icon="code" rows="20" :label="$t('script')" color="primary" v-model="script"></v-textarea>
    </v-card-text>
    <v-divider></v-divider>
    <v-card-actions>
      <v-spacer></v-spacer>
      <v-btn flat="flat" @click="setDialog({ show: false })">{{ $t('cancel') }}</v-btn>
      <v-btn :color="dialog.info.id ? 'primary' : 'success'" @click="saveHook">{{ dialog.info.id ? $t('save') : $t('create') }}</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
import axios from 'axios'
import { mapState } from 'vuex'

export default {
  name: 'RegistrationModuleEdit',
  data () {
    return {
      name: '',
      description: '',
      type: '',
      groups: [],
      script: '',
      types: ['BASH', 'IPXE']
    }
  },
  computed: {
    ...mapState('registration', ['dialog', 'groupList'])
  },
  watch: {
    dialog: {
      immediate: true,
      deep: true,
      handler (value) {
        if (value.type === 'edit' && value.show) {
          this.name = value.info.name
          this.description = value.info.description
          this.type = value.info.type || 'BASH'
          this.groups = value.info.groups ? value.info.groups.map(x => x.id) : []
          this.script = value.info.script
        }
      }
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('registration/setDialog', data)
    },
    async saveHook () {
      await axios.post('/api/registrations/hooks/' + this.dialog.info.id, {
        name: this.name,
        description: this.description,
        type: this.type,
        groups: this.groups,
        script: this.script
      })
      this.$store.dispatch('registration/loadHooks')
      this.setDialog({ show: false })
    }
  }
}
</script>

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