<template>
  <widget-wrapper v-bind="$props" ref="widget" v-if="!model.forbidDelete">
    <q-btn v-bind="$vnode.data && $vnode.data.props" @click="onClick"></q-btn>
  </widget-wrapper>
</template>

<script lang="ts">
import { Component, Prop, Mixins } from 'vue-property-decorator'
import { FormWidget, ValidatedField, WidgetWrapper } from '@devsun/form-generator'

@Component({
  components: {
    WidgetWrapper,
  },
})
export default class Qbtn extends Mixins(ValidatedField, FormWidget) {
  @Prop() value: any
  @Prop() model: any
  @Prop() fieldNode: any

  $refs!: {
    widget: any
  }

  onClick() {
    try {
      if (
        typeof this.model[this.fieldNode.name] !== 'number' ||
        Number.isNaN(this.model[this.fieldNode.name])
      ) {
        this.$refs.widget.inputEvents?.input(0)
      }

      this.$refs.widget.inputEvents?.input(+this.model[this.fieldNode.name] + 1)
    } catch (err) {
      if (this.fieldNode?.name && this.model) {
        this.$refs.widget.inputEvents?.input(0)
      }
    }
  }
}
</script>
