<script setup lang="ts">
import { useMutation } from '@vue/apollo-composable';
import { CreateReferralDocument } from '~/composables/graphql/generated';

definePageMeta({
  middleware: ['manager-only'],
});

const refereeUserId = ref('');
const createdReferralId = ref('');
const createReferralMutation = useMutation(CreateReferralDocument);

async function createReferral() {
  createdReferralId.value = '';
  const response = await createReferralMutation.mutate({
    input: {
      refereeUserId: refereeUserId.value,
    },
  });

  createdReferralId.value = response?.data?.createReferral.id ?? '';
}
</script>

<template>
  <section class="space-y-6 max-w-3xl">
    <NuxtLink to="/bonus-system" class="text-sm font-semibold text-[#0d854a]">← Назад к бонусам</NuxtLink>

    <div class="manager-hero">
      <p class="manager-eyebrow">Бонусы</p>
      <h1 class="manager-title">Создать реферальную связь</h1>
    </div>

    <div class="surface-card rounded-3xl p-5">
      <label class="form-control">
        <span class="label-text">ID приглашенного пользователя</span>
        <input v-model="refereeUserId" class="input manager-field w-full" placeholder="user id">
      </label>

      <div class="mt-4">
        <button class="btn btn-primary border-0" @click="createReferral">Создать</button>
      </div>
    </div>

    <div v-if="createdReferralId" class="surface-card rounded-3xl p-5 text-sm text-[#123824]">
      Создана связь: <span class="font-semibold">{{ createdReferralId }}</span>
    </div>
  </section>
</template>
