<template>
  <div>
    <multiselect 
      v-model="selectedPositions"
      :options="options"
      :multiple="true"
      :close-on-select="false"
      :clear-on-select="false"
      :preserve-search="true"
      placeholder="Positionen auswählen"
      label="caption"
      track-by="id"
    ></multiselect>
  </div>
</template>

<script>
import Multiselect from 'vue-multiselect';
import axios from 'axios';

export default {
  name: 'PositionSelect',
  components: { Multiselect },
  props: {
    value: {
      type: Array,
      default: () => []
    },
    options: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      selectedPositions: this.value
    };
  },
  watch: {
    value(newVal) {
      this.selectedPositions = newVal;
    },
    selectedPositions(newVal) {
      this.$emit('input', newVal);
    }
  },
  created() {
    this.fetchPositions();
  },
  methods: {
    async fetchPositions() {
      try {
        const response = await axios.get('/positions');
        this.$emit('update:options', response.data);
      } catch (error) {
        console.error('Fehler beim Abrufen der Positionen:', error);
      }
    }
  }
};
</script>

<style scoped>
@import '~vue-multiselect/dist/vue-multiselect.css';
</style>
