<template>
    <div :class="['control', { 'has-icons-right': errors.has(field.name) }]">
        <money :class="['input', { 'is-danger': errors.has(field.name) }]"
            v-bind="meta"
            v-model="field.value"
            :tabindex="tabindex"
            @update:model-value="errors.clear(field.name); $emit('changed')"/>
            <error-icon v-if="errors.has(field.name)"/>
    </div>
</template>

<script>
import Money from '@enso-ui/money';
import ErrorIcon from '../parts/ErrorIcon.vue';

export default {
    name: 'MoneyField',

    components: { Money, ErrorIcon },

    props: {
        errors: {
            type: Object,
            required: true,
        },
        field: {
            type: Object,
            required: true,
        },
        i18n: {
            type: Function,
            required: true,
        },
        readonly: {
            type: Boolean,
            default: false,
        },
        tabindex: {
            type: Number,
            default: null,
        },
    },

    emits: ['changed'],

    computed: {
        meta() {
            return {
                ...this.field.meta,
                placeholder: this.i18n(this.field.meta.placeholder),
                readonly: this.readonly || this.field.meta.readonly === true,
            };
        },
    },
};
</script>
