From 7025fd8cd5ec2333042f7e08a96afbc33fb3b04b Mon Sep 17 00:00:00 2001 From: Viet An Date: Tue, 9 Jun 2026 19:21:39 +0700 Subject: [PATCH] changes --- app/components/dialog/Confirm.vue | 36 +- app/components/pos/CartItem.vue | 71 +++ app/components/pos/ChooseIMEIModal.vue | 61 ++- app/components/pos/POS.vue | 58 +- app/plugins/02-connection.js | 21 +- app/stores/index.js | 4 +- app/utils/apis.js | 719 +------------------------ 7 files changed, 212 insertions(+), 758 deletions(-) create mode 100644 app/components/pos/CartItem.vue diff --git a/app/components/dialog/Confirm.vue b/app/components/dialog/Confirm.vue index da1dff4..05c6213 100644 --- a/app/components/dialog/Confirm.vue +++ b/app/components/dialog/Confirm.vue @@ -6,27 +6,26 @@ to=".modal-card:has(.confirm) .modal-card-foot" >
-
-
- - -
-
+
+ + +
@@ -49,4 +48,9 @@ function confirm() { emit("modalevent", { name: "confirm" }); cancel(); } + +const confirmBtn = useTemplateRef("confirmBtn"); +onMounted(() => { + confirmBtn.value.focus(); +}); diff --git a/app/components/pos/CartItem.vue b/app/components/pos/CartItem.vue new file mode 100644 index 0000000..9361c6c --- /dev/null +++ b/app/components/pos/CartItem.vue @@ -0,0 +1,71 @@ + + + diff --git a/app/components/pos/ChooseIMEIModal.vue b/app/components/pos/ChooseIMEIModal.vue index 3c91dbf..7a39790 100644 --- a/app/components/pos/ChooseIMEIModal.vue +++ b/app/components/pos/ChooseIMEIModal.vue @@ -15,23 +15,54 @@ const imeis = ref([]); const selectedImeis = ref([]); function toggleSelected(imeiRec) { - if ( - store.selectedImeis.find((i) => i.imei === imeiRec.imei) || - selectedImeis.value.find((i) => i.imei === imeiRec.imei) - ) { + if (selectedImeis.value.find((i) => i.imei === imeiRec.imei)) { remove(selectedImeis.value, (i) => i.imei === imeiRec.imei); } else { selectedImeis.value.push(imeiRec); } } -function addToCart() { - // store.selectedImeis = [...store.selectedImeis, ...selectedImeis.value]; - /* - insert into cart with customer = - */ - $snackbar(`Thêm ${selectedImeis.value.length} sản phẩm vào giỏ hàng`, "Success"); - emit("close"); +const cartItems = inject("cartItems"); +const getCart = inject("getCart"); +const isAdding = ref(false); +async function addToCart() { + try { + isAdding.value = true; + console.log("store.customer", store.customer); + let cart = await $getdata("Cart", { + filter: { customer: store.customer }, + first: true, + }); + + if (!cart) { + const newCart = await $insertapi("Cart", { + data: { customer: store.customer }, + notify: false, + }); + + cart = newCart; + } + + const cartItemsPayload = selectedImeis.value.map((imeiRec) => ({ + cart: cart.id, + imei: imeiRec.id, + quantity: 1, + total_price: imeiRec.variant__price, + })); + const newCartItems = await $insertapi("Cart_Item", { + data: cartItemsPayload, + notify: false, + }); + + $snackbar(`Đã thêm ${newCartItems.length} sản phẩm vào giỏ hàng`, "Success"); + getCart(); + emit("close"); + } catch (error) { + console.error(error); + $snackbar("Đã có lỗi khi thêm sản phẩm vào giỏ hàng", "Error"); + } finally { + isAdding.value = false; + } } async function fetchImeis() { @@ -40,7 +71,11 @@ async function fetchImeis() { filter: { variant: props.variant.id }, }); - imeis.value = imeisFetched.filter((imeiRec) => !store.selectedImeis.find((i) => i.imei === imeiRec.imei)); + imeis.value = imeisFetched.filter((imeiRec) => { + const alreadyInCart = cartItems.value.find((cartItem) => cartItem.imei === imeiRec.id); + return !alreadyInCart; + }); + isLoading.value = false; } onMounted(fetchImeis); @@ -111,7 +146,7 @@ onMounted(fetchImeis);