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

const { result, refetch } = useQuery(RegistrationRequestsDocument, { status: 'PENDING' });
const review = useMutation(ReviewRegistrationRequestDocument);

async function approve(requestId: string) {
  await review.mutate({ input: { requestId, decision: 'APPROVE' } });
  await refetch();
}

async function reject(requestId: string) {
  await review.mutate({ input: { requestId, decision: 'REJECT', rejectionReason: 'Не хватает данных' } });
  await refetch();
}
</script>

<template>
  <section class="space-y-4">
    <h1 class="text-2xl font-bold">Заявки на регистрацию</h1>
    <article v-for="request in result?.registrationRequests ?? []" :key="request.id" class="card bg-base-100 border border-base-300">
      <div class="card-body">
        <h2 class="card-title">{{ request.companyName }}</h2>
        <p>{{ request.contactName }} • {{ request.email }}</p>
        <div class="flex gap-2">
          <button class="btn btn-success" @click="approve(request.id)">Approve</button>
          <button class="btn btn-error" @click="reject(request.id)">Reject</button>
        </div>
      </div>
    </article>
  </section>
</template>
