Files
salvato.newfreeplanet/src/components/CProductCard/CProductCard.ts

395 lines
11 KiB
TypeScript
Raw Normal View History

2023-12-28 21:00:10 +01:00
import { defineComponent, ref, toRef, computed, PropType, watch, onMounted, reactive, onBeforeUnmount } from 'vue'
2021-09-16 21:08:02 +02:00
import { useI18n } from '@src/boot/i18n'
import { useUserStore } from '@store/UserStore'
import { useGlobalStore } from '@store/globalStore'
import { useQuasar } from 'quasar'
2023-11-28 15:31:25 +01:00
import { CTitleBanner } from '../CTitleBanner'
2021-09-16 21:08:02 +02:00
import { CCardState } from '../CCardState'
import { CCopyBtn } from '../CCopyBtn'
import { func_tools, toolsext } from '@store/Modules/toolsext'
import { IBaseOrder, IGasordine, IOrder, IOrderCart, IProduct } from '@src/model'
2021-09-16 21:08:02 +02:00
import { tools } from '@store/Modules/tools'
import { useProducts } from '@store/Products'
import { shared_consts } from '@src/common/shared_vuejs'
2021-09-16 21:08:02 +02:00
export default defineComponent({
name: 'CProductCard',
props: {
product: {
type: Object as PropType<IProduct | null>,
required: false,
default: null,
},
2023-12-27 02:58:23 +01:00
id: {
2021-09-16 21:08:02 +02:00
type: String,
required: false,
default: '',
},
cosa: {
type: Number,
required: false,
default: 0,
},
2021-09-16 21:08:02 +02:00
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()
2023-12-21 01:34:39 +01:00
const listord = ref(<IOrderCart[]>[])
const sumval = ref(0)
2023-12-15 23:36:48 +01:00
const site = ref(globalStore.site)
2023-12-29 01:27:59 +01:00
let myorder = reactive(<IOrder>{
idapp: process.env.APP_ID,
2023-12-10 15:15:27 +01:00
quantity: 0,
2023-12-21 01:34:39 +01:00
quantitypreordered: 0,
idStorehouse: '',
2023-12-27 02:58:23 +01:00
idGasordine: '',
storehouse: {},
2023-12-29 21:17:25 +01:00
gasordine: { active: false },
})
2021-09-16 21:08:02 +02:00
2023-11-30 15:43:32 +01:00
const storeSelected = ref('')
const gasordineSelected = ref('')
2023-12-28 21:00:10 +01:00
const recgasordineSelected = ref(<any>null)
const arrordersCart = ref(<IOrderCart[]>[])
2023-12-28 21:00:10 +01:00
const timerInterval = ref(<any>null)
const timerLabelScadenza = ref('')
2023-12-28 23:48:09 +01:00
const labelDataRitiro = ref('')
const labelDataArrivoMerce = ref('')
2023-12-28 21:00:10 +01:00
const openlistorders = ref(false)
const endload = ref(false)
2023-12-17 19:19:10 +01:00
const myproduct = ref(<IProduct>{})
2023-11-30 15:43:32 +01:00
2023-12-10 15:15:27 +01:00
const valoriopt = computed(() => (item: any, addall: boolean, addnone: boolean) => {
// console.log('valoriopt', item.table)
return globalStore.getTableJoinByName(item.table, addall, addnone, item.filter)
})
2023-12-17 19:19:10 +01:00
/*const myproduct = computed((): IProduct => {
console.log('getproduct computed')
const ris = products.getProduct(props.code)
console.log(' received', ris)
return ris
})*/
2023-12-10 15:15:27 +01:00
2021-09-16 21:08:02 +02:00
function iconWhishlist(order: IProduct) {
if (true) {
return 'far fa-heart'
} else {
return 'fas fa-heart'
}
}
async function addtoCart(add: boolean) {
2021-09-16 21:08:02 +02:00
if (!userStore.isLogged) {
2023-11-30 14:27:46 +01:00
tools.showNeutralNotif($q, t('ecomm.area_personale'))
2021-09-16 21:08:02 +02:00
globalStore.rightDrawerOpen = true
return false
}
2023-12-27 02:58:23 +01:00
const ris = await products.addtoCartBase({ $q, t, id: myproduct.value._id, order: myorder, addqty: add })
2023-12-21 01:34:39 +01:00
updateproduct()
if (ris && ris.myord) {
2023-12-21 01:34:39 +01:00
}
2023-12-10 15:15:27 +01:00
}
function getnumstore() {
2023-12-18 08:02:35 +01:00
if (myproduct.value) {
if (myproduct.value.storehouses)
return myproduct.value.storehouses.length
else
return 0
}
return 0
2023-12-10 15:15:27 +01:00
}
function getSingleStorehouse() {
2023-12-11 16:51:51 +01:00
try {
const mystore = myproduct.value.storehouses[0]
if (mystore)
return mystore.name + ' (' + mystore.city + ')'
else
return ''
} catch (e) {
2023-12-10 15:15:27 +01:00
return ''
2023-12-11 16:51:51 +01:00
}
2021-09-16 21:08:02 +02:00
}
function getSingleGasordine(gasordine: IGasordine) {
try {
const mygas = gasordine
if (mygas)
2023-12-29 21:17:25 +01:00
/*return mygas.name + ' (' + mygas.city + ') ' + t('gas.dataora_chiusura_ordini') + ': ' + tools.getstrDateShort(mygas.dataora_chiusura_ordini)
2023-12-28 23:48:09 +01:00
+ ' ' + t('gas.data_arrivo_merce') + ': ' + tools.getstrDateShort(mygas.data_arrivo_merce)
2023-12-29 21:17:25 +01:00
+ ' ' + t('gas.dataora_ritiro') + ': ' + tools.getstrDateShort(mygas.dataora_ritiro)*/
return mygas.name
else
return ''
} catch (e) {
return ''
}
}
2023-12-29 01:27:59 +01:00
function initproduct() {
myorder.quantity = 0
myorder.quantitypreordered = 0
myorder.idStorehouse = ''
myorder.idGasordine = ''
gasordineSelected.value = ''
recgasordineSelected.value = null
2023-12-29 21:17:25 +01:00
2023-12-29 01:27:59 +01:00
}
2023-12-17 19:19:10 +01:00
function updateproduct() {
2023-12-29 01:27:59 +01:00
2023-12-27 02:58:23 +01:00
myproduct.value = products.getProductById(props.id)
2023-12-29 21:17:25 +01:00
2023-12-20 21:56:30 +01:00
// products.updateQuantityAvailable(myproduct.value._id)
2023-12-17 19:19:10 +01:00
}
2023-12-10 15:15:27 +01:00
function getStorehouses() {
2023-12-18 08:02:35 +01:00
if (!myproduct.value)
return []
2023-12-20 21:56:30 +01:00
2023-12-10 15:15:27 +01:00
const myarr: any = []
let ind = 1
myproduct.value.storehouses.forEach((store) => {
myarr.push(
{
id: ind,
label: store.name + ' (' + store.city + ')',
value: store._id
})
ind++
})
2021-09-16 21:08:02 +02:00
2023-12-10 15:15:27 +01:00
// console.log('arraystore', myarr)
return myarr
}
2021-09-16 21:08:02 +02:00
function getGasordines() {
if (!myproduct.value)
return []
const myarr: any = []
let ind = 1
myproduct.value.gasordines.forEach((gasordine: IGasordine) => {
myarr.push(
{
id: ind,
label: getSingleGasordine(gasordine),
value: gasordine._id
})
ind++
})
return myarr
}
2023-12-10 15:15:27 +01:00
function checkifCartDisable() {
// return !myorder.idStorehouse
return false
2023-12-10 15:15:27 +01:00
}
2023-11-30 15:43:32 +01:00
2023-12-11 16:51:51 +01:00
function getQtyWarn() {
if (myorder.quantity > 0) {
return t('ecomm.di_cui_x_in_carrello', { qty: myorder.quantity })
2023-12-11 16:51:51 +01:00
}
return ''
}
2023-12-20 21:56:30 +01:00
function getQtyWarnPreOrdered() {
if (myorder.quantitypreordered > 0) {
return t('ecomm.di_cui_x_in_carrello', { qty: myorder.quantitypreordered })
}
return ''
}
2023-12-27 02:58:23 +01:00
watch(() => props.id, (newval, oldval) => {
// console.log('change code')
2023-12-10 15:15:27 +01:00
load()
})
2021-09-16 21:08:02 +02:00
2023-12-10 15:15:27 +01:00
watch(() => storeSelected.value, (newval, oldval) => {
myorder.idStorehouse = newval
2023-12-10 15:15:27 +01:00
})
2021-09-16 21:08:02 +02:00
watch(() => gasordineSelected.value, (newval, oldval) => {
2023-12-28 23:48:09 +01:00
if (newval) {
myorder.idGasordine = newval
recgasordineSelected.value = myproduct.value.gasordines.find((rec: any) => rec._id === gasordineSelected.value)
updateLabel()
}
})
2023-12-28 23:48:09 +01:00
function updateLabel() {
2023-12-29 01:27:59 +01:00
if (recgasordineSelected.value) {
//labelDataRitiro.value = tools.getstrDateTime(recgasordineSelected.value.dataora_ritiro)
if (recgasordineSelected.value.data_arrivo_merce)
labelDataArrivoMerce.value = tools.getstrDateShort(recgasordineSelected.value.data_arrivo_merce)
if (recgasordineSelected.value.dataora_ritiro)
labelDataRitiro.value = tools.getstrDateTime(recgasordineSelected.value.dataora_ritiro)
} else {
labelDataArrivoMerce.value = ''
labelDataRitiro.value = ''
}
2023-12-28 23:48:09 +01:00
}
2023-12-28 21:00:10 +01:00
function mounted() {
load()
// Start the timer when the component is mounted
startTimer();
}
function beforeDestroy() {
// Clear the interval when the component is destroyed to prevent memory leaks
clearInterval(timerInterval.value);
}
function updateTimerLabel() {
if (recgasordineSelected.value)
timerLabelScadenza.value = tools.getCountDown(recgasordineSelected.value.dataora_chiusura_ordini)
else
timerLabelScadenza.value = ''
}
function startTimer() {
// Update the timer label every second
timerInterval.value = setInterval(() => updateTimerLabel(), 1000);
}
function load() {
2023-12-29 01:27:59 +01:00
initproduct()
2023-12-17 19:19:10 +01:00
updateproduct()
2023-12-18 08:02:35 +01:00
// console.log('Load', myproduct.value.name)
2023-12-10 15:15:27 +01:00
// console.log('created Cproductcard', code)
2021-09-16 21:08:02 +02:00
2023-12-18 13:04:44 +01:00
arrordersCart.value = products.getOrdersCartInAttesaByIdProduct(myproduct.value._id)
2023-12-10 15:15:27 +01:00
if (!!myproduct.value) {
2023-12-13 19:18:00 +01:00
if (myproduct.value.storehouses && myproduct.value.storehouses.length === 1) {
2023-12-27 02:58:23 +01:00
// Se solo 1 presente, metto fisso l'unico negozio !
myorder.idStorehouse = myproduct.value.storehouses[0]._id
2023-12-10 15:15:27 +01:00
}
2021-09-16 21:08:02 +02:00
if (myproduct.value.gasordines && myproduct.value.gasordines.length === 1) {
myorder.idGasordine = myproduct.value.gasordines[0]._id
2023-12-28 21:00:10 +01:00
gasordineSelected.value = myorder.idGasordine!
2023-12-29 01:27:59 +01:00
}
const ord = products.getOrderProductInCart(myproduct.value._id)
if (ord) {
myorder.quantity = ord.quantity
2023-12-20 21:56:30 +01:00
myorder.quantitypreordered = ord.quantitypreordered
2023-12-28 21:00:10 +01:00
if (ord.idGasordine) {
gasordineSelected.value = ord.idGasordine!
2023-12-28 21:00:10 +01:00
recgasordineSelected.value = myproduct.value.gasordines.find((rec: IGasordine) => rec._id === ord.idGasordine!)
}
2023-12-27 02:58:23 +01:00
// Seleziona il Negozio che avevo già scelto nell'ordine !
if (ord.idStorehouse)
storeSelected.value = ord.idStorehouse
}
2023-12-28 21:00:10 +01:00
if (gasordineSelected.value) {
recgasordineSelected.value = myproduct.value.gasordines.find((rec: IGasordine) => rec._id === gasordineSelected.value)
}
2023-12-10 15:15:27 +01:00
}
2021-09-16 21:08:02 +02:00
2023-12-29 01:27:59 +01:00
updateLabel()
2023-12-18 08:02:35 +01:00
// console.log('°°° ENDLOAD °°°')
endload.value = true
2021-09-16 21:08:02 +02:00
}
2023-12-10 15:15:27 +01:00
function getclimgproduct() {
return 'myimgproduct centermydiv'
2023-12-10 15:15:27 +01:00
}
2023-12-11 16:51:51 +01:00
2023-12-21 01:34:39 +01:00
function visuListDisponibili() {
openlistorders.value = true
sumval.value = products.getSumQtyOrderProductInOrdersCart(myproduct.value._id)
listord.value = arrordersCart.value.filter((ordercart: IOrderCart) => ordercart.items!.reduce((accumulator, item) => {
return accumulator + item.order.quantity
}, 0))
}
function visuListBookable() {
openlistorders.value = true
sumval.value = products.getSumQtyPreOrderInOrdersCart(myproduct.value._id)
listord.value = arrordersCart.value.filter((ordercart: IOrderCart) => ordercart.items!.reduce((accumulator, item) => {
return accumulator + item.order.quantitypreordered
}, 0))
}
2023-12-28 21:00:10 +01:00
onMounted(mounted)
onBeforeUnmount(beforeDestroy)
2023-12-10 15:15:27 +01:00
return {
2023-12-21 01:34:39 +01:00
visuListDisponibili,
visuListBookable,
2023-12-10 15:15:27 +01:00
addtoCart,
iconWhishlist,
getclimgproduct,
2023-12-10 15:15:27 +01:00
getnumstore,
getSingleStorehouse,
getSingleGasordine,
2023-12-10 15:15:27 +01:00
getStorehouses,
checkifCartDisable,
myproduct,
myorder,
tools,
t,
storeSelected,
gasordineSelected,
2023-12-11 16:51:51 +01:00
getQtyWarn,
openlistorders,
func_tools,
toolsext,
products,
arrordersCart,
endload,
shared_consts,
2023-12-15 23:36:48 +01:00
site,
2023-12-20 21:56:30 +01:00
getQtyWarnPreOrdered,
2023-12-21 01:34:39 +01:00
listord,
sumval,
getGasordines,
2023-12-28 21:00:10 +01:00
recgasordineSelected,
timerLabelScadenza,
2023-12-28 23:48:09 +01:00
labelDataRitiro,
labelDataArrivoMerce,
2023-12-10 15:15:27 +01:00
}
}
2021-09-16 21:08:02 +02:00
})