<template>
    <div class="relative mb-2">
        <label v-if="label" :for="name" class="pb-2 font-bold text-xl ml-1">{{ label }}</label>
        <textarea :id="name"
                  type="text"
                  v-model="value"
                  :placeholder="placeholder"
                  @input="updateField()"
                  :class="errorClassObject()"
                  class="w-full h-64 rounded p-2">
        </textarea>
        <p class="text-red no-indent m-0" v-text="errorMessage()">Error Here</p>
    </div>
</template>

<script>
export default {
    name: 'TextAreaField',
    props: {
        name: {
            type: String,
            required: true
        },
        type: {
            type: String,
            default: 'text'
        },
        label:  String,
        placeholder: String,
        required: {
            type: Boolean,
            default: false
        },
        errors: Object,
        data: String,
    },
    data: function () {
        return {
            value: ''
        }
    },
    computed: {
        hasError: function () {
            return this.required && this.errors && this.errors[this.name] && this.errors[this.name].length > 0
        }
    },
    methods: {
        updateField: function () {
            this.clearErrors(this.name)
            this.$emit('update:field', this.value)
        },
        errorMessage: function () {
            if (this.hasError) {
                return this.errors[this.name][0]
            }
        },
        clearErrors: function () {
            if (this.hasError) {
                this.errors[this.name] = null
            }
        },
        errorClassObject: function () {
            if (this.hasError) {
                return 'border-red'
            }
        }
    },
    watch: {
        data: function (val) {
            this.value = val
        }
    }
}
</script>
