<template>
  <div
    class="list-group list-button d-flex flex-column w-100 mb-2"
    :class="hasError ? 'has-error' : ''"
    @mouseup="handleClick(value)"
    @keyup.space="handleClick(value)"
  >
    <input
      :type="isMultiSelect ? 'checkbox' : 'radio'"
      :id="buttonID"
      :name="groupName"
      :value="buttonID"
      :aria-required="isRequired"
      :data-focus-target="groupName"
      v-model="checkValue"
      @change="!selectingInitiatesLoad ? handleCheckChange() : ''"
    >
    <label
      tabindex="-1"
      :for="buttonID"
      :aria-labelledby="buttonID"
      class="d-flex flex-column justify-content-center py-3 px-4"
    >
      <span
        class="m-0"
        :class="textPosition"
      >
        {{ buttonLabel }}
      </span>
      <span
        v-if="buttonLabelSubCopy"
        class="m-0 small"
        :class="textPosition"
      >
        {{ buttonLabelSubCopy }}
      </span>
      <span
        v-if="screenReaderOnlyText"
        class="sr-only"
      >
        {{ screenReaderOnlyText }}
      </span>
      <loader
        v-if="isLoaderDisplayed && !isMultiSelect"
        :class="[this.loaderColor, this.loaderPosition]"
      />
    </label>
  </div>
</template>

<script>
import { toRefs } from "vue";
import { useField } from "vee-validate";
import loader from "@/ux-components/loader/loader";
export default {
  name: "listButton",
  props: {
    isMultiSelect: Boolean,
    groupName: String,
    buttonLabel: [Number, String],
    buttonID: [Number, String],
    isRequired: Boolean,
    textPosition: String,
    buttonLabelSubCopy: String,
    screenReaderOnlyText: String,
    selectingInitiatesLoad: Boolean,
    loaderColor: String,
    loaderPosition: String,
    value: {
    // Field initial value
      type: [String, Number],
      default: "",
    },
    selectedButtonIDs: [Array, String],
    hasError: Boolean,
  },
  data() {
    return {
      isLoaderDisplayed: false,
      checkValue: Boolean,
    };
  },
  created(){
    if(this.selectedButtonIDs){
      this.checkValue = this.isMultiSelect ? this.selectedButtonIDs.includes(this.buttonID) : this.selectedButtonIDs[0];
    }
  },
  methods: {
    displayLoader() {
      this.isLoaderDisplayed = true;
    },
    handleClick(value) {
      if(this.selectingInitiatesLoad) {
        this.displayLoader();
        this.handleCheckChange();
      }
      this.handleChange(value);
    },
    handleCheckChange(newValue, oldValue){
      const isInitialization = typeof(oldValue) === 'function';
      if (!isInitialization) {
        this.$emit('isCheckedChanged', { isChecked: this.checkValue, buttonId: this.buttonID.toString() });
      }
    },
  },
  components: {
    loader,
  },
  setup(props) {
    const { groupName, value } = toRefs(props);
    const inputType = props.isMultiSelect ? "checkbox" : "radio";
    const { checked, handleChange, errorMessage } = useField(
      groupName,
      undefined,
      {
        type: inputType,
        checkedValue: value,
      }
    );
    return {
      checked,
      handleChange,
      errorMessage,
    };
  },
};
</script>

<style lang="scss">
.list-group {
  &.list-button {
    input[type="radio"],
    input[type="checkbox"] {
      position: static; //override bootstrap
      height: 0;
      opacity: 0;

      &:focus-visible + label {
        box-shadow: 0 0 0 2.5px $blue inset;
      }
      &:focus + label {
        box-shadow: 0 0 0 2.5px $blue inset;
      }
      &:checked + label {
        color: $black;
        font-weight: 500;
        background: $blue-100;
        box-shadow: 0 0 0 1px $blue;
      }
      &:checked + label p:first-child {
        font-weight: 500;
      }
    }
  }
  label {
    color: $gray-600;
    position: relative;
    background: $white;
    transition: all 150ms linear;
    border-radius: $border-radius-lg;
    border: 1px solid $gray-500;
    width: 100%;

    &:hover {
      @include media-breakpoint-up(sm) {
        box-shadow: 0 0 0 4px $blue-300;
      }
      cursor: pointer;
    }
    + p {
      display: none;
    }
  }
}
</style>
