<template>
  <div>
    <v-text-field
      ref="textField"
      v-currency="{
        currency: null,
        locale: languageName,
        precision: precision,
        allowNegative: true
      }"
      dense
      :value="currencyValue"
      :readonly="readonly"
      :disabled="disabled"
      :label="label"
      :rules="rules"
      :error-messages="errorMessages"
      @input="updateValue"
    ></v-text-field>
  </div>
</template>
<script>
//### NOTE: THIS IS A DUPLICATE OF CURRENCYCONTROL AND THE ONLY DIFFERENCE IS THE "currency:" VALUE IS SET TO NULL IN THE TEMPLATE AND IN THE updateValue METHOD
//https://dm4t2.github.io/vue-currency-input/guide/#introduction :value="formattedValue"
//https://codesandbox.io/s/vue-template-kd7d1?fontsize=14&module=%2Fsrc%2FApp.vue
//https://github.com/dm4t2/vue-currency-input
//https://github.com/dm4t2/vue-currency-input/releases
import { parse } from "vue-currency-input";
export default {
  props: {
    label: { type: String, default: null },
    rules: { type: Array, default: undefined },
    value: { type: Number, default: null },
    readonly: { type: Boolean, default: false },
    disabled: { type: Boolean, default: false },
    errorMessages: { type: Array, default: null },
    precision: { type: Number, default: undefined }
  },
  data() {
    return {
      languageName: window.$gz.locale.getResolvedLanguage()
    };
  },
  computed: {
    currencyValue() {
      return this.value;
    }
  },
  methods: {
    updateValue() {
      const val = this.$refs.textField.$refs.input.value;
      const parsedValue = parse(val, {
        currency: null,
        locale: this.languageName
      });
      if (parsedValue == this.value) {
        return;
      }

      this.$emit("input", parsedValue);
    }
  }
};
</script>
