Files
myprojplanet_vite/src/components/CProductCard/CProductCard.ts
Surya Paolo 4ac6cacf19 aa1
2023-12-11 16:51:51 +01:00

243 lines
5.7 KiB
TypeScript
Executable File

import { defineComponent, ref, toRef, computed, PropType, watch, onMounted } from 'vue'
import { useI18n } from '@src/boot/i18n'
import { useUserStore } from '@store/UserStore'
import { useGlobalStore } from '@store/globalStore'
import { useQuasar } from 'quasar'
import { CTitleBanner } from '../CTitleBanner'
import { CCardState } from '../CCardState'
import { CCopyBtn } from '../CCopyBtn'
import { IOrder, IProduct } from '@src/model'
import { tools } from '@store/Modules/tools'
import { useProducts } from '@store/Products'
export default defineComponent({
name: 'CProductCard',
props: {
product: {
type: Object as PropType<IProduct | null>,
required: false,
default: null,
},
code: {
type: String,
required: false,
default: '',
},
complete: {
type: Boolean,
required: false,
default: false,
},
},
components: { CTitleBanner, CCardState, CCopyBtn },
setup(props, { emit }) {
const $q = useQuasar()
const { t } = useI18n()
const userStore = useUserStore()
const globalStore = useGlobalStore()
const products = useProducts()
const myorder = ref(<IOrder>{
idapp: process.env.APP_ID,
quantity: 0,
idStorehouse: ''
})
const myproduct = ref(<IProduct>{})
const storeSelected = ref('')
const valoriopt = computed(() => (item: any, addall: boolean, addnone: boolean) => {
// console.log('valoriopt', item.table)
return globalStore.getTableJoinByName(item.table, addall, addnone, item.filter)
})
function iconWhishlist(order: IProduct) {
if (true) {
return 'far fa-heart'
} else {
return 'fas fa-heart'
}
}
function decqty() {
if (myorder.value.quantity > 0)
myorder.value.quantity--
}
function addqty() {
if (myorder.value.quantity < 10)
myorder.value.quantity++
}
function addtoCart(add: boolean) {
if (!userStore.isLogged) {
tools.showNeutralNotif($q, t('ecomm.area_personale'))
globalStore.rightDrawerOpen = true
return false
}
products.addToCart({ product: myproduct.value, order: myorder.value, addqty: add }).then((ris) => {
let strprod = t('ecomm.prodotto')
if (myorder.value.quantity > 1 || myorder.value.quantity === 0)
strprod = t('ecomm.prodotti')
let msg = ''
if (ris === null)
msg = t('ecomm.error_cart')
else {
if (myorder.value.quantity === 0) {
msg = t('ecomm.prodotto_tolto')
} else {
msg = t('ecomm.prod_sul_carrello', { strprod, qty: myorder.value.quantity })
}
}
if (ris === null || myorder.value.quantity === 0)
tools.showNotif($q, msg)
else
tools.showPositiveNotif($q, msg)
})
}
function getnumstore() {
if (myproduct.value.storehouses)
return myproduct.value.storehouses.length
else
return 0
}
function getSingleStorehouse() {
try {
const mystore = myproduct.value.storehouses[0]
if (mystore)
return mystore.name + ' (' + mystore.city + ')'
else
return ''
} catch (e) {
return ''
}
}
function getStorehouses() {
const myarr: any = []
let ind = 1
myproduct.value.storehouses.forEach((store) => {
myarr.push(
{
id: ind,
label: store.name + ' (' + store.city + ')',
value: store._id
})
ind++
})
// console.log('arraystore', myarr)
return myarr
}
function checkifCartDisable() {
return !myorder.value.idStorehouse
}
function updateQtyAvailable() {
myproduct.value.quantityAvailable = myproduct.value.stockQty - myorder.value.quantity
}
function getQtyAvailable() {
updateQtyAvailable()
let qty = myproduct.value.quantityAvailable
return qty
}
function getQtyWarn() {
if (myorder.value.quantity > 0){
return t('ecomm.di_cui_x_in_carrello', {qty: myorder.value.quantity})
}
return ''
}
watch(() => props.code, (newval, oldval) => {
console.log('change code')
load()
})
watch(() => storeSelected.value, (newval, oldval) => {
myorder.value.idStorehouse = newval
})
async function load() {
// console.log('created Cproductcard', code)
if (props.code) {
myproduct.value = await products.loadProduct({ code: props.code })
} else {
// @ts-ignore
myproduct.value = props.product
}
console.log('myproduct', myproduct, 'product', props.product)
if (!!myproduct.value) {
if (myproduct.value.storehouses.length === 1) {
myorder.value.idStorehouse = myproduct.value.storehouses[0]._id
}
}
const ord = products.getOrderProductInCart(myproduct.value._id)
if (ord) {
myorder.value.quantity = ord.quantity
}
}
function getmycardcl() {
return (props.complete) ? 'my-card-big' : 'my-card-prod'
}
function getclimg() {
return (props.complete) ? 'myimgtitle centermydiv' : 'centermydiv'
}
function enableSubQty() {
return myorder.value ? myorder.value.quantity > 0 : true
}
function enableAddQty() {
return getQtyAvailable() > 0
}
onMounted(load)
return {
decqty,
addqty,
addtoCart,
iconWhishlist,
getmycardcl,
getclimg,
getnumstore,
getSingleStorehouse,
getStorehouses,
checkifCartDisable,
myproduct,
myorder,
tools,
t,
storeSelected,
enableSubQty,
enableAddQty,
getQtyAvailable,
getQtyWarn,
}
}
})