<i18n>
{
  "en": {
    "name": "Name",
    "script": "iPXE Script",
    "titleNew": "Create entry",
    "titleExisting": "Edit entry"
  },
  "de": {
    "name": "Name",
    "script": "iPXE Script",
    "titleNew": "Eintrag erstellen",
    "titleExisting": "Eintrag 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-text-field prepend-icon="label" :label="$t('name')" color="primary" v-model="name"></v-text-field>
      <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="saveEntry">{{ 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: 'ConfiguratorModuleEntry',
  data () {
    return {
      name: '',
      script: ''
    }
  },
  computed: {
    ...mapState('configurator', ['dialog'])
  },
  watch: {
    dialog: {
      immediate: true,
      deep: true,
      handler (value) {
        if (value.type === 'entry' && value.show) {
          this.name = value.info.name
          this.script = value.info.script
        }
      }
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('configurator/setDialog', data)
    },
    async saveEntry () {
      await axios.post('/api/configurator/entries/' + this.dialog.info.id, {
        name: this.name,
        script: this.script
      })
      this.$store.dispatch('configurator/loadData')
      this.setDialog({ show: false })
    }
  }
}
</script>

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