<template>
    <component :is="Default || 'div'">
  <div class="container mx-auto">
    <h2
      class="font-semibold text-black dark:text-white pb-6 text-2xl">
      {{ t('Cart Items') }}
    </h2>
    <div
      class="bg-(--second-light) dark:bg-(--main-dark) rounded-lg border border-(--border-light) dark:border-(--border-dark) overflow-hidden">
      <div v-if="cartStore.items.length > 0" class="divide-y divide-(--border-light) dark:divide-(--border-dark)">
        <div v-for="item in cartStore.items" :key="item.id" class="flex items-center justify-between p-4 gap-4">
          <div class="grid grid-cols-5 w-[100%]">
            <div
              class="w-20 bg-(--second-light) dark:bg-(--main-dark) rounded flex items-center justify-center overflow-hidden">
              <img v-if="item.image" :src="item.image" :alt="item.name" class="w-full h-full object-cover" />
              <UIcon v-else name="mdi:package-variant" class="text-xl text-gray-400" />
            </div>
            <p class="text-black dark:text-white text-sm font-medium truncate">{{ item.name }}</p>
            <p class="text-black dark:text-white text-sm font-medium truncate">{{ item.product_number }}</p>
            <p class="text-black dark:text-white font-medium">${{ (item.price * item.quantity).toFixed(2) }}</p>
            <div class="flex items-center justify-end gap-10">
              <UInputNumber v-model="item.quantity" class="text-gray-500 dark:text-gray-400 text-sm" />
              <div class="flex justify-center">
                <button @click="removeItem(item.id)"
                  class="p-2 text-red-500 bg-red-100 dark:bg-(--main-dark)  rounded transition-colors"
                  :title="t('Remove')">
                  <UIcon name="material-symbols:delete" class="text-[20px]" />
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div v-else class="p-8 text-center">
        <UIcon name="mdi:cart-outline" class="text-5xl text-gray-300 dark:text-gray-600 mb-4 mx-auto" />
        <p class="text-gray-500 dark:text-gray-400">{{ t('Your cart is empty') }}</p>
      </div>
    </div>
    <div v-if="cartStore.items.length > 0" class="flex gap-4 justify-end items-center pt-6">
      <UButton color="primary" @click="handleContinueToCheckout"
        class="bg-(--accent-blue-light) dark:bg-(--accent-blue-dark) text-white hover:bg-(--accent-blue-dark) dark:hover:bg-(--accent-blue-light)">
        {{ t('Continue to Checkout') }}
      </UButton>
      <UButton variant="outline" color="neutral" @click="handleCancel"
        class="text-black dark:text-white border-(--border-light) dark:border-(--border-dark) hover:bg-gray-100 dark:hover:bg-gray-700">
        {{ t('Cancel') }}
      </UButton>
    </div>
  </div>
  </component>
</template>

<script setup lang="ts">
import { useCartStore } from '@/stores/cart'
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import Default from '@/layouts/default.vue'
const cartStore = useCartStore()
const { t } = useI18n()
const router = useRouter()
function handleCancel() {
  router.back()
}

function handleContinueToCheckout() {
  router.push({ name: 'carts' })
}

function removeItem(itemId: number) {
  cartStore.removeItem(itemId)
}
</script>
