<template>
  <label :for="name" class="default-input" :class="[inputClass, { 'default-input--error': hasErrors } ]">
    <span class="default-input__label" v-if="label" v-text="label"></span>
    <div class="default-input__wrapper">
      <input
        :type="inputType"
        :name="name"
        :id="name"
        :placeholder="inputPlaceholder"
        :value="value"
        :required="required"
        :max="max"
        :min="min"
        :step="step"
        @input="$emit('input', $event.target.value)"
        class="default-input__input"
        @blur="resetErrors"
      >
      <transition name="fade">
        <img v-if="hasErrors" :src="errorImg" alt="error" class="default-input__image">
      </transition>
    </div>
    <transition name="fade">
      <template v-if="hasErrors">
        <p
          class="default-input__message default-input__message--error"
          role="alert"
          v-for="(message, index) in errors"
          :key="index"
          v-text="message"
        ></p>
      </template>
    </transition>
  </label>
</template>

<script>
import size from 'lodash/size';

export default {
  name: 'default-input',
  props: {
    inputType: {
      type: String,
      required: false,
      default: 'text',
    },
    inputStyle: {
      type: String,
      required: false,
      default: 'boxed',
    },
    name: {
      type: String,
      required: true,
    },
    label: {
      type: String,
      required: false,
    },
    value: {
      type: [String, Number],
      required: true,
    },
    placeholder: {
      type: String,
      required: false,
    },
    required: {
      type: Boolean,
      required: false,
      default: false,
    },
    autocomplete: {
      type: String,
      required: false,
      default: 'off',
    },
    max: {
      type: Number,
      required: false,
    },
    min: {
      type: Number,
      required: false,
    },
    step: {
      type: Number,
      required: false,
    },
    errors: {
      type: Array,
      required: false,
    },
    errorImg: {
      type: String,
      required: false,
    }
  },
  computed: {
    hasErrors() {
      return size(this.errors) > 0;
    },
    inputClass() {
      return this.inputStyle === 'boxed' ? 'default-input--boxed' : 'default-input--line';
    },
    inputPlaceholder() {
      return this.placeholder ? this.placeholder : '';
    }
  },
  methods: {
    resetErrors() {
      this.$emit('reset-errors', this.name)
    }
  }
}
</script>

<style lang="scss" scoped>
.default-input {
  display: flex;
  flex-direction: column;
  position: relative;
  margin-bottom: 30px;
  margin-bottom: 3rem;
  &__label {
    @include body-small(medium, label, $color-black-primary, tertiary, quaternary);
    margin-bottom: 5px;
    margin-bottom: .5rem;
    transition: color transition(fast);
  }
  &__wrapper {
    position: relative;
  }
  &__image {
    position: absolute;
    top: 50%;
    right: 15px;
    transform: translateY(-50%);
  }
  &__input {
    width: 100%;
    background-color: transparent;
    padding: 0;
    border: none;
    text-indent: 10px;
    text-indent: 1rem;
    height: 50px;
    height: 5rem;
    outline: none;
    caret-color: $color-primary;
    transition: all transition(fast);
    @include body-normal(regular, placeholder, $color-black-primary, tertiary, quaternary);
    &::-webkit-input-placeholder { /* Chrome/Opera/Safari */
      @include body-normal(regular, placeholder, $color-black-primary, tertiary, quaternary);
      color: rgba($color-black-primary, .5);
    }
    &::-moz-placeholder { /* Firefox 19+ */
      @include body-normal(regular, placeholder, $color-black-primary, tertiary, quaternary);
      color: rgba($color-black-primary, .5);
    }
    &:-ms-input-placeholder { /* IE 10+ */
      @include body-normal(regular, placeholder, $color-black-primary, tertiary, quaternary);
      color: rgba($color-black-primary, .5);
    }
    &:-moz-placeholder { /* Firefox 18- */
      @include body-normal(regular, placeholder, $color-black-primary, tertiary, quaternary);
      color: rgba($color-black-primary, .5);
    }
  }
  &__message {
    position: absolute;
    margin-bottom: 0;
    left: 0;
    bottom: -30px;
    bottom: -3rem;
    @include body-small;
    &--error {
      color: $color-message-tertiary;
    }
  }
  &--boxed {
    input {
      background-color: $white;
      border-radius: border-radius(primary);
      box-shadow: box-shadow(secondary);
      border-bottom: 2px solid $white;
      &:focus {
        border-bottom-color: $color-primary;
      }
    }
  }
  &--line {
    input {
      border-bottom: border(solid-secondary);
      &:focus {
        border-bottom-color: $color-primary;
      }
    }
    .default-input__message {
      left: 10px;
      left: 1rem;
    }
  }
  &--error {
    .default-input__label {
      color: $color-message-tertiary;
    }
    .default-input__input {
      outline: none;
      border-bottom-color: $color-message-tertiary;
    }
  }
}
</style>

