<template>
  <v-dialog v-model="show" width="500" @keydown.esc="cancel">
    <v-card class="pa-4" color="background2">
      <v-card-title class="subtitle-1">Edit Commit</v-card-title>

      <v-card-text class="pl-2 pr-2 pt-0 pb-0">
        <v-form
          ref="form"
          v-model="valid"
          lazy-validation
          @submit.prevent="agree"
        >
          <v-container>
            <v-row>
              <v-col cols="12" class="pb-0">
                <v-text-field
                  v-model="commit.message"
                  label="Message"
                  :rules="nameRules"
                  required
                  filled
                  autofocus
                ></v-text-field>
              </v-col>
            </v-row>
          </v-container>
        </v-form>
      </v-card-text>
      <v-card-actions>
        <v-spacer></v-spacer>
        <v-btn :disabled="!valid" color="primary" text @click.native="agree">
          Save
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>
<script>
export default {
  data: () => ({
    dialog: false,
    commit: {},
    nameRules: [],
    valid: true
  }),
  computed: {
    show: {
      get() {
        return this.dialog
      },
      set(value) {
        this.dialog = value
        if (value === false) {
          this.cancel()
        }
      }
    }
  },
  watch: {
    "commit.name"(val) {
      this.nameRules = []
    }
  },
  methods: {
    open(commit, streamId) {
      this.dialog = true
      if (this.$refs.form) this.$refs.form.resetValidation()

      this.commit = {
        message: commit.message,
        id: commit.id,
        streamId: streamId
      }

      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },
    agree() {
      this.nameRules = [
        (v) => !!v || "Please write a commit message",
        (v) => (v && v.length >= 3) || "Message must be at least 3 characters"
      ]

      let self = this
      setTimeout(function () {
        if (self.$refs.form.validate()) {
          self.resolve({
            result: true,
            commit: self.commit
          })
          self.dialog = false
        }
      })
    },
    cancel() {
      this.resolve({
        result: false
      })
      this.dialog = false
    }
  }
}
</script>
