<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 style="overflow: hidden">
    <v-card-title class="dialog-title elevation-3">
      <div class="headline">{{ dialog.info.id ? $t('titleExisting') : $t('titleNew') }}</div>
    </v-card-title>
    <v-card-text style="height: 100%;">
      <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="file_copy" color="primary" :items="types" :label="$t('type')" v-model="type" menu-props="offsetY"></v-select>
        </v-flex>
        <v-flex xs12 md6>
          <v-textarea prepend-icon="description" rows="1" auto-grow :label="$t('description')" color="primary" v-model="description"></v-textarea>
        </v-flex>
        <v-flex xs12 md5 offset-md1>
          <select-box prepend-icon="device_hub" :label="$t('groups')" v-model="groups" :items="groupList"></select-box>
        </v-flex>
      </v-layout>
      <div class="body-1 script-heading"><v-icon>code</v-icon><span>{{ $t('script') }}</span></div>
      <codemirror class="script-editor" ref="editor" v-model="script"></codemirror>
    </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 SelectBox from '@/components/SelectBox'
import { mapState } from 'vuex'

export default {
  name: 'RegistrationModuleEdit',
  components: {
    SelectBox
  },
  data () {
    return {
      name: '',
      description: '',
      type: '',
      groups: [],
      script: '',
      types: ['BASH', 'IPXE'],
      interval: {
        id: null,
        counter: 0
      }
    }
  },
  computed: {
    ...mapState('registration', ['dialog']),
    ...mapState('groups', ['groupList']),
    headers () {
      return [
        { key: 'name', text: this.$t('name') }
      ]
    }
  },
  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 : []
          this.script = value.info.script || ''
          this.interval.id = setInterval(this.refreshEditor, 50)
          this.interval.counter = 0
        }
      }
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('registration/setDialog', data)
    },
    async saveHook () {
      await this.$http.post('/api/registration/hooks/' + this.dialog.info.id, {
        name: this.name,
        description: this.description,
        type: this.type,
        groups: this.groups.map(x => x.id),
        script: this.script
      })
      this.$store.dispatch('registration/loadHooks')
      this.setDialog({ show: false })
    },
    refreshEditor () {
      this.interval.counter++
      if (this.$refs.editor) this.$refs.editor.codemirror.refresh()
      if (this.interval.counter >= 15) clearInterval(this.interval.id)
    }
  }
}
</script>

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

.script-heading {
  display: flex;
  align-items: center;
}

.script-heading > span {
  margin-left: 9px;
}

.script-editor {
  margin-top: 8px;
}
</style>
