<template>
  <div class="text-center">
    <v-dialog v-model="show" width="300">
      <v-form v-model="valid">
        <v-card>
          <DialogHeading title="Add Section" @hide="show = false" />

          <v-card-text class="my-0 pb-0 pt-2">
            <v-text-field
              v-model="name"
              :rules="[v => !!v || 'Required']"
              label="Name"
              autofocus
              required
              @keydown.enter.prevent="doNewSection()"
            />
          </v-card-text>

          <v-card-actions>
            <v-spacer/>
            <v-btn
              small
              class="mb-2"
              @click="show = false">Cancel</v-btn>
            <v-btn
              :disabled="!valid"
              color="primary"
              class="black--text ml-4 mb-2"
              small
              @click="doNewSection()">OK</v-btn>
            <v-spacer/>
          </v-card-actions>
        </v-card>
      </v-form>
    </v-dialog>
  </div>
</template>

<script>
import DialogHeading from './DialogHeading'
import { mapActions } from 'vuex'

export default {
  components: {
    DialogHeading,
  },
  props: {
    value: Boolean,
  },
  data: () => ({
    name: '',
    valid: false,
  }),
  computed: {
    show: {
      get() {
        return this.value
      },
      set(value) {
        this.$emit('input', value)
      }
    },
  },
  methods: {
    ...mapActions([
      'newSection',
    ]),
    doNewSection() {
      if (this.name) {
        this.newSection(this.name)
      }
      this.name = ''
      this.show = false
    }
  },
}
</script>

<style>

</style>
