<template>
    <teleport to="#location">
        <div class="mb-3 row">
            <label class="col-form-label col-sm-4">
                Localisation
            </label>
            <div class="col-sm-8">

                <VueMultiselect
                    name="selectLocation"
                    id="selectLocation"
                    track-by="id"
                    :multiple="false"
                    :searchable="true"
                    open-direction="top"
                    placeholder="Choisissez une localisation"
                    label="name"
                    :custom-label="customLabel"
                    v-model="location"
                    :options="locations">
                </VueMultiselect>
                <!--
                -->
                <new-location></new-location>
            </div>
        </div>
    </teleport>
</template>

<script>
import { mapState } from "vuex";
import VueMultiselect from 'vue-multiselect';
import NewLocation from './Location/NewLocation.vue';
import { getLocations } from '../api.js';

export default {
    name: "Location",
    components: {
        NewLocation,
        VueMultiselect
    },
    data() {
        return {
            locations: []
        }
    },
    computed: {
        ...mapState(['activity']),
        location: {
            get() {
                return this.activity.location;
            },
            set(value) {
                this.$store.commit('updateLocation', value);
            }
        }
    },
    mounted() {
        this.getLocationsList();
    },
    methods: {
        getLocationsList() {
            getLocations().then(response => new Promise(resolve => {
                console.log('getLocations', response);
                this.locations = response.results;
                resolve();
            }))
        },
        customLabel(value) {
            return `${value.locationType.title.fr} ${value.name}`;
        }
    }
}
</script>
