<template>
  <div class="cart">
    <div class="cart__top">
      <div class="cart__top-left">
        <p><span>{{ t('profile.cart.quantity') }}</span> {{ productsInCartQuantity }}</p>
        <p><span>{{ t('profile.cart.total') }}: </span> {{ totalPrice }}{{ CURRENCY }}</p>
      </div>
      <ui-button
          :type="'button'"
          class="cart__top-button"
      >
        {{ t('buttons.checkout') }}
      </ui-button>
    </div>
    <div class="cart__list">
      <div class="cart__list-inner" v-if="productsInCart.length">
        <cards-product
            v-for="product in productsInCart"
            :key="product.node.uuid"
            :product="product.node.product"
            :isList="true"
            :isToolsVisible="true"
        />
      </div>
      <p class="cart__empty" v-else>{{ t('profile.cart.empty') }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import {usePageTitle} from "~/composables/utils";
import {CURRENCY} from "~/config/constants";

const {t} = useI18n();
const cartStore = useCartStore();

const productsInCart = computed(() => {
  return cartStore.currentOrder ? cartStore.currentOrder.orderProducts.edges : [];
});
const totalPrice = computed(() => {
  return cartStore.currentOrder ? cartStore.currentOrder.totalPrice : 0;
});
const productsInCartQuantity = computed(() => {
  let count = 0;
  cartStore.currentOrder?.orderProducts?.edges.forEach((el) => {
    count = count + el.node.quantity;
  });

  return count;
});

const { setPageTitle } = usePageTitle();

setPageTitle(t('breadcrumbs.cart'));
</script>

<style lang="scss" scoped>
.cart {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 50px;

  &__top {
    width: 100%;
    background-color: $white;
    padding: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
    border-radius: $default_border_radius;

    & p {
      font-weight: 600;
    }
    
    &-button {
      width: fit-content;
      padding-inline: 20px;
    }
  }

  &__list {
    width: 100%;
    padding: 20px;
    background-color: $white;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
    border-radius: $default_border_radius;

    &-inner {
      display: flex;
      flex-direction: column;
      gap: 20px;
    }
  }

  &__empty {
    font-weight: 600;
  }
}
</style>