<template>
    <ul class="list-suggest add-items" v-if="suggested.length > 0">
        <li v-for="(r, i) in suggested" @click="setReferrer(r)" :key="i">
            <span>{{ r.text }}</span>
        </li>
    </ul>
</template>

<script>
import { makeFetch } from "ChillMainAssets/lib/api/apiMethods.ts";

export default {
    name: "SetReferrer",
    props: {
        suggested: {
            type: Array,
            required: false,
            //default: [],
        },
        periodId: {
            type: Number,
            required: true,
        },
    },
    data() {
        return {
            /*suggested: [
        {id: 5, text: 'Robert'}, {id: 8, text: 'Monique'},
      ]*/
        };
    },
    emits: ["referrerSet"],
    methods: {
        setReferrer: function (ref) {
            const url = `/api/1.0/person/accompanying-course/${this.periodId}.json`;
            const body = {
                type: "accompanying_period",
                user: { id: ref.id, type: ref.type },
            };

            return makeFetch("PATCH", url, body)
                .then(() => {
                    this.$emit("referrerSet", ref);
                })
                .catch((error) => {
                    throw error;
                });
        },
    },
};
</script>

<style scoped></style>
