<template>
  <UButton label="СОздать ОредЕр" @click="createOrder" />
</template>

<script setup lang="ts">
import type { IBspb } from '~/server/shared/types/bspb'
import { usePostOrdersCreate } from '~/api/mutations/wp/usePostOrdersCreate'
import { useCart } from '~/composables'

const router = useRouter()
const { cart } = useCart()
const { mutateAsync } = usePostOrdersCreate()

const createOrder = async () => {
  await mutateAsync({ line_items: cart.value.line_items })

  const { data } = await useFetch<IBspb>('/api/bspb')

  const redirectUrl = `${data?.value?.order?.hppUrl}?orderId=${data?.value?.order?.id}&password=${data.value?.order?.password}`
  window.open(redirectUrl, '_blank')
}

definePageMeta({
  layout: 'checkout',
})
</script>

<style scoped lang="scss">

</style>
