<template>
  <div :class="[$style.select,{ [$style.open]: open }]" @blur="open = !open">
    <div :class="[$style.selected]" @click="open = options.length !== 0 ? !open : false">
      <div v-if="selected" :class="[$style.selected__text]">
        {{ selected.label }}
      </div>
      <div v-else :class="[$style.selected__text]">
        No Option
      </div>
    </div>
    <div v-if="options" :class="$style.options">
      <div
        v-for="(option, i) of options"
        :key="i"
        :class="$style.option"
        @click="() => {
          open = false;
          $emit('change', option);
        }"
      >
        {{ option.label }}
      </div>
    </div>
  </div>
</template>

<script>
// https://blog.boot.dev/javascript/custom-select-component-in-vue/

/*

使われてるページ（一部）

/search

*/
export default {
  props: {
    styles: {
      type: Object,
      default: () => {
        return {
          select: ''
        }
      }
    },
    options: {
      type: Array,
      required: true
    },
    selected: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      open: false
    }
  }
}
</script>

<style module lang="scss">

.select {
  position: relative;
  width: 100%;
  text-align: left;
  outline: none;
  height: 100%;
  text-align: center;
  // transition: 0.3s;
  color: #fff;
  letter-spacing: 0.08em;
  box-sizing: border-box;
  /* transition: all 0.3s linear; */
  cursor: pointer; @include mq-pc {
      width: 16rem;
    }
}

.selected {
  height:100%;
  cursor: pointer;
  user-select: none;
  padding:1rem 3.3rem 1rem 1.3rem;
  background-color:#2f2f33;

  border-top-left-radius: 0.4rem;
  border-top-right-radius:  0.4rem;
  border-bottom-left-radius:  0;
  border-bottom-right-radius:  0;
  border:  none;
  position: relative;
}

.select.open .selected{
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-bottom: none;
}

.selected:after {
  content: "";
  position: absolute;
  top: auto;
  bottom: auto;
  right: 1em;
  /* transform: translateY(-50%); */
  background-image: url("~assets/images/shared/arrow-down-white.svg");
  width: .8em;
  height: .8em;
  background-size: contain;
  background-position: center center;
  background-repeat: no-repeat;
  transition: transform .3s ease;
  transform-origin: center center;
  position: absolute;
  right: 1.8rem;
  top: 50%;
  transform: translateY(-50%);
}

.select.open .selected:after {
  transform: translateY(-50%) rotate(180deg);
}

.options {
  overflow: hidden;
  position: absolute;
  left: 0;
  right: 0;
  z-index: 1;
  display: none;
  background-color:#2f2f33;
  border-bottom-left-radius: 0.4rem;
  border-bottom-right-radius: 0.4rem;
  border:  none;
  border-top: none;
}

.select.open .options{
  display: block;
}

.option {
  cursor: pointer;
  user-select: none;
  border-top: #666 1px solid;
  padding: 1rem 3.3rem 1rem 1.3rem;
}

.option:hover {
  background-color: #666;
}
@include mq-pc {
  .select:hover {
    opacity: 0.8;
  }
  .select.open:hover {
    opacity: 1;
  }
}
@include mq-pc {
  .selected__text {
    text-align: left;
  }
}
@include mq-sp {
  .selected__text {
    text-align: left;
    font-size: 1.6rem;
  }
}
</style>
