<template>
  <div class="cart">
    <div class="cart__items">
      <div
        v-for="cartItem in cart?.line_items"
        :key="cartItem.variation_id"
      >
        <CartItem :cart-item="cartItem" />
      </div>
    </div>

    <PayBlock />
  </div>
</template>

<script setup lang="ts">
import { useCart } from '~/composables'
import PayBlock from './checkout/PayBlock.vue'

const route = useRoute()
const { cart } = useCart()
const { contacts } = useCheckout()

onMounted(async () => {
  if (!route?.query?.ID)
    return

  await $fetch('/api/create', {
    method: 'POST',
    body: {
      payment_method: 'bacs',
      payment_method_title: 'Оплата по реквизитам',
      set_paid: true,
      billing: {
        first_name: contacts?.value?.name,
        last_name: contacts?.value?.surname,
        phone: contacts?.value?.phone,
        email: contacts?.value?.email,
        address_1: 'ул. Ленина, 1',
        city: 'Москва',
        country: 'RU',
      },
      transaction_id: route?.query?.ID,
      line_items: cart.value.line_items,
      status: 'processing',
    },
  })
})
</script>

<style lang="scss">
@use '~/assets/scss/utils' as *;

.cart {
  margin-top: 120px;
  margin-inline: auto;
  max-width: 1200px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;

  @include mobile {
    margin-top: 20px;
    flex-direction: column;
  }

  &__items {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }
}
</style>
