<template>
  <div class="text-center">
    <v-dialog v-model="show" width="300">
      <v-form v-model="valid">
        <v-card>
          <DialogHeading title="Add Section">
            <v-btn icon class="ma-0 pa-0" @click="show = false">
              <v-icon color="white">clear</v-icon>
            </v-btn>
          </DialogHeading>

          <v-card-text>
            <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 flat @click="show = false">Cancel</v-btn>
            <v-btn
              :disabled="!valid"
              color="primary"
              flat
              @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>
