<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 setup>
import { makeFetch } from "ChillMainAssets/lib/api/apiMethods.ts";
import { defineProps, defineEmits } from "vue";

const props = defineProps({
  suggested: {
    type: Array,
    required: false,
    // default: [],
  },
  periodId: {
    type: Number,
    required: true,
  },
});

const emit = defineEmits(["referrerSet"]);

function setReferrer(ref) {
  const url = `/api/1.0/person/accompanying-course/${props.periodId}.json`;
  const body = {
    type: "accompanying_period",
    user: { id: ref.id, type: ref.type },
  };

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

<style scoped></style>
