<!-- See the component-test.vue page for example implementation -->
<template>
  <!-- IMPORTANT: Refrain from using more than 4 horizontal buttons on desktop, 3 on mobile. -->
  <div v-if="isMultiSelect" class="list-group list-button-horizontal d-flex flex-column w-100 mb-2">
    <input type="checkbox" :id="buttonID" :name="groupName" :value="buttonID" :aria-required="isRequired">
    <label tabindex="-1" aria-checked="false" :for="buttonID" class="d-flex flex-column justify-content-center py-3 px-4" :class="isFirstOrLastButton">
      <span class="m-0" :class="[this.textPosition]">{{buttonID}}</span>
      <span v-if="buttonLabelSubCopy" class="m-0 small" :class="[this.textPosition]">{{buttonLabelSubCopy}}</span>
      <span v-if="screenReaderOnlyText" class="sr-only">{{screenReaderOnlyText}}</span>
  </label>
  <p class="small">{{errorMessage}}</p>
  </div>
  <div v-else class="col list-group list-button-horizontal d-flex flex-column mb-2">
    <input type="radio" :id="buttonID" :name="groupName" :value="buttonID" aria-required="true" @keyup.space="displayLoader()"/>
    <label tabindex="-1" aria-checked="false" :for="buttonID" class="d-flex flex-column justify-content-center py-3 px-4" :class="isFirstOrLastButton" @click="displayLoader()">
      <span class="m-0" :class="[this.textPosition]">{{buttonID}}</span>
      <span v-if="buttonLabelSubCopy" class="m-0 small" :class="[this.textPosition]">{{buttonLabelSubCopy}}</span>
      <span v-if="screenReaderOnlyText" class="sr-only">{{screenReaderOnlyText}}</span>
      <loader v-if="isLoaderDisplayed" :style="{width: `${sizeInRem}rem`, height: `${sizeInRem}rem`}" :class="[this.loaderColor, this.loaderPosition]" />
    </label>
    <p class="small">{{ errorMessage }}</p>
  </div>
</template>

<script>
import loader from "@/ux-components/loader/loader";
export default {
  name: "listButtonHorizontal",
  props: {
    isMultiSelect: Boolean,
    groupName: String, /* Required, unique for each button GROUP */
    buttonID: String, /* Required, unique for each button. Used for button id, label and <label for> */
    buttonLabelSubCopy: String, /* Optional, used for multi-line buttons */
    screenReaderOnlyText: String, /* Optional, copy to be read by screenreader */
    textPosition: String, /* Optional, use Bootstrap classes: text-start, text-center, text-end. Default (empty) is text-start */
    errorMessage: String, /* Optional, if there is an error message to be displayed */
    loaderColor: String, /* Specify color of loader/spinner. Options are blue, red, green, white, black. Default is blue */
    loaderPosition: String, /* Specify horizontal position of loader/spinner. Options are center, right, left */
    sizeInRem: [Number,String], /* Specify size of loader/spinner in rem. Example: 1.5 (equals 24px (16x1.5)) */
    totalInGroup: Number, /* Required, total number of buttons in group. Used to tell first and last in group to apply border radius. */
    positionInGroup: Number /* Rquired, position of button in group. Example, 1,2,3 */
  },
  data() {
    return {
      isError: false,
      isLoaderDisplayed: false,
    };
  },
  methods: {
    displayLoader() {
      this.isLoaderDisplayed = true;
    },
  },
  components: {
    loader,
  },
  computed: {
    isFirstOrLastButton() {
      let className = "";
      if (this.positionInGroup == this.totalInGroup) {
        className = "last-item";
      } else if (this.positionInGroup == 1) {
        className = "first-item";
      }
      return className;
    },
  },
};
</script>
<style lang="scss">
.list-button-horizontal {
  input[type="radio"],
  input[type="checkbox"] {
    opacity: 0;
    position: fixed;
    width: 0;
    &:focus-visible + label {
      box-shadow: 0 0 0 2px $blue;
    }
    &:focus + label {
      box-shadow: 0 0 0 2px $blue;
    }
    &:checked + label {
      background: $blue-100;
      box-shadow: 0 0 0 1px $blue;
    }
    &:checked + label p:first-child {
      font-weight: 500;
    }
  }
  label {
    position: relative;
    background: $white;
    transition: all 150ms linear;
    border: 1px solid $gray-500;
    width: 100%;
    &:hover {
      box-shadow: 0 0 0 4px $blue-100;
      cursor: pointer;
      z-index: 2;
    }
    + p {
      display: none;
    }
    &.error {
      border: 1px solid $danger;
      + p {
        display: flex;
        color: $danger;
      }
    }
    &.first-item {
      border-bottom-left-radius: 0.5rem;
      border-top-left-radius: 0.5rem;
    }
    &.last-item {
      border-bottom-right-radius: 0.5rem;
      border-top-right-radius: 0.5rem;
    }
  }
}
</style>
