<template>
  <div class="picker-wrap">
    <!-- <pre style="position: fixed; z-index: 500; right: 0; top: 7rem; font-size: 0.6rem; color: orange;">
      {{ selectedAirport }}
    </pre> -->
    <label for="">
      {{ leg }}
    </label>
    <v-select
      :value="selectedAirportComp"
      label="iata"
      :options="airports"
      :filter-by="filterBy"
      :components="{OpenIndicator, Deselect}"
      @input="changeAirport"
    >
      <template #no-options="{ search, searching }">
        <template v-if="searching">
          No results found for <em>{{ search }}</em>.
        </template>
        <em
          v-else
          style="opacity: 0.5;"
        >
          Start typing to search for an airport.
        </em>
      </template>
      <template #selected-option="option">
        <div class="picker-item">
          <div class="picker-item__section">
            <div class="picker-item__iata">
              {{ option.iata }}
            </div>
          </div>
          <div class="picker-item__section">
            <div class="picker-item__name">
              {{ option.name }}
            </div>
            <div class="picker-item__muni">
              {{ option.municipality }}
            </div>
          </div>
        </div>
      </template>
      <template #option="option">
        <div class="picker-item">
          <div class="picker-item__section">
            <div class="picker-item__iata">
              {{ option.iata }}
            </div>
          </div>
          <div class="picker-item__section">
            <div class="picker-item__name">
              {{ option.name }}
            </div>
            <div class="picker-item__muni">
              {{ option.municipality }}
            </div>
          </div>
        </div>
      </template>
      <template #search="{ events, attributes }">
        <input
          :placeholder="placeholder"
          type="search"
          class="vs__search"
          v-bind="attributes"
          v-on="events"
        >
      </template>
    </v-select>
  </div>
</template>

<script>

import OpenIndicator from './OpenIndicator.vue'
import Deselect from './Deselect.vue'

export default {
  // ClearButton,
  props: {
    airports: {
      type: [Array],
      default () {
        return []
      }
    },
    selectedAirport: {
      type: [Object],
      default () {
        return {
          iata: '',
          lat: '',
          long: '',
          icon: '',
          name: '',
          municipality: '',
          type: '',
          search: ''
        }
      }
    },
    leg: {
      type: [String],
      default () {
        return ''
      }
    }
  },
  data: () => ({
    OpenIndicator,
    filterBy: (option, label, search) => {
      const temp = search.toLowerCase()
      return option.search.toLowerCase().includes(temp)
    }
  }),
  computed: {
    Deselect () {
      return this.selectedAirportComp.iata ? Deselect : ''
    },
    selectedAirportComp () {
      return {
        iata: this.selectedAirport.iata,
        lat: this.selectedAirport.lat,
        long: this.selectedAirport.long,
        icon: this.selectedAirport.icon,
        name: this.selectedAirport.name,
        municipality: this.selectedAirport.municipality,
        type: this.selectedAirport.type,
        search: this.selectedAirport.search,
        label: this.selectedAirport.label
      }
    },
    placeholder () {
      return (this.selectedAirport.iata) ? '' : 'town, airport, or iata'
    }
  },
  methods: {
    changeAirport (value) {
      if (value) {
        this.$emit('select-airport', value)
      } else {
        this.$emit('deselect-airport', value)
      }
    }
  }
}
</script>

<style>
html {
  color: black;
}

.picker-wrap {
  margin: 1rem;
  position: relative;
}

.picker-wrap:last-of-type {
  margin-top: 0;
}

.picker-wrap .vs__search {
  font-size: 3rem !important;
  padding: 0.5rem;
}

.vs__search::placeholder {
  color: #bbb;
}

.vs__dropdown-toggle {
  background: #eee;
  border: 0;
  border-radius: 3rem;
  padding-right: 1rem;
  padding-left: 1rem;
}

.picker-item {
  padding: 0.5rem;
  display: flex;
  flex-direction: row;
  align-items: center;
}

.picker-item__name {
  font-size: 1rem;
}

.picker-item__muni {
  color: #007fff;
}

.picker-item__iata {
  font-size: 3rem;
  margin-right: 1rem;
}

.vs--single .vs__selected {
  position: absolute;
}

</style>

<style scoped>
  label {
    display: block;
    color: black;
    position: absolute;
    z-index: 1;
    text-transform: uppercase;
    font-weight: 300;
    top: 0.3rem;
    left: 2rem;
    letter-spacing: 0.13rem;
    font-size: 0.8rem;
  }
</style>
