<template>
  <label :for="name" class="radio-input" :title="label" :class="{ 'radio-input--disabled': disabled }">
    <input
      type="radio"
      :name="name"
      :id="name"
      :value="radioValue"
      v-model="selected"
      @change="onChange"
      class="radio-input__input"
      :disabled="disabled"
    >
    <span class="radio-input__label" v-if="label" v-text="label"></span>
  </label>
</template>

<script>
export default {
  name: 'radio-input',
  props: {
    name: {
      type: String,
      required: true,
    },
    label: {
      type: String,
      required: true,
    },
    radioValue: {
      type: [Number, String, Boolean],
      required: true,
    },
    value: {
      required: true,
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      radio: false,
    }
  },
  computed: {
    selected: {
      get() {
        return this.value;
      },
      set(val) {
        this.radio = val;
      }
    }
  },
  methods: {
    onChange() {
      this.$emit('input', this.radio);
    }
  }
}
</script>

<style lang="scss" scoped>
.radio-input {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-bottom: 45px;
  margin-bottom: 4.5rem;
  cursor: pointer;
  &--disabled, &--preselected {
    pointer-events: none;
  }
  &__input {
    margin-right: 10px;
    margin-right: 1rem;
    width: 22px;
    height: 22px;
    width: 2.2rem;
    height: 2.2rem;
    position: relative;
    appearance: none;
    flex-shrink: 0;
    cursor: pointer;
    &:focus {
      outline: none;
    }
    &:before {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      content: '';
      width: 100%;
      height: 100%;
      transition: all transition(fast);
      background-repeat: no-repeat;
    }
    &:checked {
      &:not(:disabled) {
        &:before {
          background-image: url('./../../assets/images/icons/radio/checked.svg');
        }
      }
      &:disabled {
        &:before {
          background-image: url('./../../assets/images/icons/radio/checked_disabled.svg');
        }
      }
    }
    &:not(:checked) {
      &:not(:disabled) {
        &:before {
          background-image: url('./../../assets/images/icons/radio/unchecked.svg');
        }
      }
      &:disabled {
        &:before {
          background-image: url('./../../assets/images/icons/radio/unchecked_disabled.svg');
        }
      }
    }
  }
  &__label {
    @include body-small(medium, label, $color-black-primary, tertiary, quaternary);
    user-select: none;
    @include ellipsis()
  }
}
</style>

