<template>
  <div class="selector" v-if="selected">
    <p class="selected">{{selected.name}}</p>
    <select v-model="selected">
      <option
        v-for="(option, key) in options"
        :key="key"
        :value="option"
        :selected="option === selected"
      >{{option[selector]}}</option>
    </select>
  </div>
</template>

<script>
export default {
  name: "Selector",
  props: ["options", "selector", "selectedIso"],
  data() {
    return {
      selected: ""
    };
  },
  watch: {
    selectedIso: function() {
      if (this.selectedIso)
        this.selected = this.options.find(
          item => item.iso2 === this.selectedIso
        );
    },
    selected: function() {
      this.$emit("input", this.selected);
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
.selector {
  position: relative;
  margin-left: 4px;
  cursor: pointer;
  * {
    cursor: pointer;
  }
  p {
    text-decoration: underline;
  }
  span {
    font-size: 10px;
    margin-left: 4px;
    margin-top: 6px;
  }
}
select {
  position: absolute;
  top: 2px;
  height: 100%;
  left: 0;
  width: 100%;
  opacity: 0;
}
.selected {
  width: 100%;
  position: relative;
  margin-top: 16px;
  cursor: pointer;
  p {
    margin-bottom: 3px;
    margin-left: 4px;
  }
}
</style>
