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

   <div class="my-4">
      <label class="col-form-label" for="selectReferrer">
         {{ $t('referrer.label') }}
      </label>

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

      <ul class="record_actions">
         <li>
            <button 
               class="btn btn-create"
               type="button"
               name="button"
               @click="assignMe">
               {{ $t('referrer.assign_me') }}
            </button>
         </li>
      </ul>

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

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

export default {
   name: "Referrer",
   components: { VueMultiselect },
   data() {
      return {
         options: []
      }
   },
   computed: {
      ...mapState({
         value: state => state.accompanyingCourse.user,
      }),
   },
   mounted() {
      this.getOptions();
   },
   methods: {
      getOptions() {
         getUsers().then(response => new Promise((resolve, reject) => {
            this.options = response.results;
            resolve();
         }));
      },
      updateReferrer(value) {
         //console.log('value', value);
         this.$store.dispatch('updateReferrer', value);
      },
      assignMe() {
         //console.log('assign me');
         whoami().then(me => new Promise((resolve, reject) => {
            this.$store.dispatch('updateReferrer', me);
            resolve();
         }));
      }
   }
}
</script>

<style src="vue-multiselect/dist/vue-multiselect.css"></style>
