<template>
  <widget-wrapper v-bind="$props" v-slot="{ inputEvents }">
    <q-field
      filled
      :label="label"
      :value="currentValue"
      :readonly="isReadonly"
      :error="hasError"
      :error-message="errorMessage"
      @input="inputEvents.input"
    >
      <template v-slot:control="{ id, floatingLabel, value, emitValue }">
        <currency-input
          class="q-field__input text-left"
          :value="value"
          :currency="null"
          @input="emitValue"
        />
      </template>
    </q-field>
  </widget-wrapper>
</template>

<script lang="ts">
import { Component } from 'vue-property-decorator';
import { mixins } from 'vue-class-component';
import {
  ValidatedField,
  SubscriberWidget,
  WidgetWrapper,
  FormWidget,
} from '@devsun/form-generator/dist';

@Component({
  name: 'MoneyInput',
  components: {
    WidgetWrapper,
  },
})
export default class Float extends mixins(
  SubscriberWidget,
  ValidatedField,
  FormWidget
) {
  private get label(): string {
    return this.fieldNode.label ? this.fieldNode.label : '';
  }

  private get isReadonly(): boolean {
    return this.fieldNode.readonly;
  }

  private get currentValue(): number {
    if (this.value) {
      return Number(this.value.content);
    }

    return 0;
  }
}
</script>
