<template>
   <div class="vue-component">
      <h2><a name="section-30"></a>{{ $t('origin.title') }}</h2>

      <div class="mb-4">
         <label for="selectOrigin">
            {{ $t('origin.label') }}
         </label>

         <VueMultiselect
            name="selectOrigin"
            label="text"
            v-bind:custom-label="transText"
            track-by="id"
            v-bind:multiple="false"
            v-bind:searchable="true"
            v-bind:placeholder="$t('origin.placeholder')"
            v-model="value"
            v-bind:options="options"
            @select="updateOrigin">
         </VueMultiselect>

      </div>
   </div>
</template>

<script>
import VueMultiselect from 'vue-multiselect';
import { getListOrigins } from '../api';
import { mapState } from 'vuex';

export default {
   name: 'OriginDemand',
   components: { VueMultiselect },
   data() {
      return {
         options: []
      }
   },
   computed: {
      ...mapState({
         value: state => state.accompanyingCourse.origin,
      }),
   },
   mounted() {
      this.getOptions();
   },
   methods: {
      getOptions() {
         //console.log('loading origins list');
         getListOrigins().then(response => new Promise((resolve, reject) => {
            this.options = response.results;
            resolve();
         }));
      },
      updateOrigin(value) {
         //console.log('value', value);
         this.$store.dispatch('updateOrigin', value);
      },
      transText ({ text }) {
         return text.fr //TODO multilang
      },
   }
}
</script>

<style src="vue-multiselect/dist/vue-multiselect.css"></style>
<style lang="css" scoped>
   label {
      display: none;
   }
</style>
