import type { PropType } from 'vue'; import { defineComponent, onMounted, ref, watch, computed } from 'vue'; import { tools } from '@tools'; import { useUserStore } from '@store/UserStore'; import { useCatalogStore } from '@store/CatalogStore' import { useRouter } from 'vue-router'; import { useGlobalStore } from '@store/globalStore'; import { useProducts } from '@store/Products'; import { useI18n } from 'vue-i18n'; import { toolsext } from '@store/Modules/toolsext'; import { useQuasar } from 'quasar'; import { costanti } from '@costanti'; import { shared_consts } from '@src/common/shared_vuejs'; import { CProductCard } from '@src/components/CProductCard'; import { CMySelect } from '@src/components/CMySelect'; import { CContainerCatalogoCard } from '@src/components/CContainerCatalogoCard'; import { CSelectUserActive } from '@src/components/CSelectUserActive'; import type { IOptCatalogo, IProduct, ISearchList } from 'model'; import { fieldsTable } from '@store/Modules/fieldsTable'; import Products from 'app/src/rootgen/admin/products/products.vue'; export default defineComponent({ name: 'CSearchProduct', components: { CContainerCatalogoCard, CProductCard, CSelectUserActive, CMySelect }, emits: ['insert', 'close', 'updateproductmodif'], props: { modelValue: { type: Object as PropType, required: false, default: null, }, table: { type: String, required: true, }, idprodtoshow: { type: String, required: false, default: '', }, empty: { type: Boolean, required: false, default: false, }, nameLinkTemplate: { type: String, required: false, default: '', }, visu: { type: Number, required: false, default: shared_consts.VISU_SEARCHPROD_MODE.VISU, }, }, setup(props, { emit }) { const userStore = useUserStore(); const globalStore = useGlobalStore(); const productStore = useProducts(); const catalogStore = useCatalogStore(); const router = useRouter(); const $q = useQuasar(); const { t } = useI18n(); const search = ref(''); const focus = ref(false); const loadpage = ref(false); const refreshpage = ref(false); const show_hide = ref(false); const mycolumns = ref([]); const where = ref('') const idPage = ref(''); const selauthor = ref(''); const searchList = ref([]); const optcatalogo = ref(null); const myproduct = ref({}); const addstr = ref('') const labelcombo = computed(() => (item: any) => { let lab = item.label; if (item.showcount) lab += ' (' + valoriopt.value(item, false, false).length + ')'; return lab; }); const listaRicerca = computed(() => { let key = '' if (props.table === shared_consts.TABLES_CATALOG) { key = 'title' } else { key = 'titolo' } const mylist = searchList.value.find((rec: any) => rec.table === props.table && rec.key === key); return mylist; }); const searchText = computed(() => { const lista = listaRicerca.value; let key = '' if (props.table === shared_consts.TABLES_CATALOG) { key = 'title' } else { key = 'name' } return lista && lista.value && tools.existProp(lista.value, key) ? lista.value[key] : ''; }); const valoriopt = computed(() => (item: any, addall: boolean, addnone: boolean = false) => { // console.log('valoriopt', item.table) return globalStore.getTableJoinByName(item.table, addall, addnone, item.filter); }); watch( () => searchText.value, (newval, oldval) => { console.log('searchText=', searchText.value); const id = getSearchId(); loadProduct(id); } ); watch( () => myproduct.value, (newval, oldval) => { // console.log('myproduct', myproduct.value); // loadProduct(myproduct.value._id) updateproductmodif(myproduct.value); } ); async function loadProduct(id: string) { // Carica il prodotto console.log('loadProduct', id); if (props.table === 'products') { if (id) { myproduct.value = await productStore.loadProductById(id); } else { myproduct.value = null; } } else if (props.table === 'catalogs') { if (id) { myproduct.value = catalogStore.getCatalogById(id); if (!myproduct.value) { await catalogStore.fetchCatalogById(id) } } else { myproduct.value = null; } } saveSearch(); // console.log('myproduct.value', myproduct.value); } watch( () => where.value, (newval, oldval) => { tools.setCookie(addstr.value + 'WHE', newval); } ); function saveSearch() { if (!props.idprodtoshow && !props.empty) { if (myproduct.value) { tools.setCookie(addstr.value + tools.COOK_LAST_PROD_SEARCH, myproduct.value._id.toString()); } else { tools.setCookie(addstr.value + tools.COOK_LAST_PROD_SEARCH, ''); } } if (!myproduct.value) { tools.setCookie(addstr.value + tools.COOK_LAST_PROD_SEARCH, ''); } } function resetSearch() { const mialista = listaRicerca.value; if (mialista && mialista.value && tools.existProp(mialista.value, 'name')) { mialista.value = null; } search.value = ''; } function getSearchId(): string { const lista = listaRicerca.value; return lista && lista.value && lista.value._id ? lista.value._id : ''; } function isProductLibro() { return props.table === 'products'; } async function mounted() { // console.log('mounted Catalogo') addstr.value = tools.addstrCookie(props.table) where.value = tools.getCookie(addstr.value + 'WHE', shared_consts.WHERE_INSERT.ONTOP); if (props.modelValue) { optcatalogo.value = props.modelValue; } else { optcatalogo.value = globalStore.createCatalogoVuoto(); productStore.addNewScheda(optcatalogo.value); if (props.nameLinkTemplate) { const linkIdTemplate = globalStore.getLinkIdTemplateByName(props.nameLinkTemplate); if (linkIdTemplate) optcatalogo.value.arrSchede[0].scheda.linkIdTemplate = linkIdTemplate; } else { optcatalogo.value.arrSchede[0].scheda.name = 'SEARCH_NEW'; } } let id = null; if (props.table === 'products') { id = props.idprodtoshow || (!props.empty ? tools.getCookie(addstr.value + tools.COOK_LAST_PROD_SEARCH, '') : ''); } if (props.nameLinkTemplate) { optcatalogo.value = productStore.populateDataWithlinkIdTemplate(optcatalogo.value); } if (props.visu === shared_consts.VISU_SEARCHPROD_MODE.INSERT) { focus.value = true; } loadpage.value = false; if (id) { await loadProduct(id); } mycolumns.value = fieldsTable.getArrColsByTable(props.table); if (isProductLibro()) { searchList.value = [ { visible: true, label: 'Cerca un Titolo o un Autore', table: 'products', key: 'titolo', type: costanti.FieldType.select_by_server, value: myproduct.value, collabel: collabel, arrvalue: [], useinput: true, filter: null, tablesel: 'products', dense: false, }, ]; } else { searchList.value = [ { visible: true, label: 'Cerca un Catalogo', table: 'catalogs', key: 'title', type: costanti.FieldType.select_by_server, value: myproduct.value, collabel: 'title', arrvalue: [], useinput: true, filter: null, tablesel: 'catalogs', dense: false, }, ]; } // Inizializza loadpage.value = true; } function naviga(path: string) { router.push(path); } function collabel(rec: any) { let label = ''; if (rec && rec.productInfo && props.table === 'products') { if (productStore.isNovitaById(rec.productInfo.date_pub)) label += `🌟 `; label += `${rec.productInfo.name}`; if (Array.isArray(rec.productInfo.authors)) { const authors = rec.productInfo.authors.map((a: any) => `${a.name} ${a.surname}`).join(', '); label += ` - (${authors})`; } else { label += rec.productInfo.authors ? ` - (${rec.productInfo.authors.name} ${rec.productInfo.authors.surname}) ` : ''; } if (rec.productInfo?.idStatoProdotto) { if (!productStore.isPubblicatoById(rec.productInfo?.idStatoProdotto)) label += ' (' + productStore.getDescrStatiProdottoByIdStatoProdotto(rec.productInfo?.idStatoProdotto || '') + ')'; } if (productStore.isEsaurito(rec)) { label += ' (Attualmente non disponibile)'; } // console.log('Computed label:', label) } else { if (rec && rec.name) label = rec.name; else label = ''; } return label; } function insertProd() { console.log('insertProd', myproduct.value) emit('insert', myproduct.value, where.value); } function clickClose() { searchList.value[0].value = ''; myproduct.value = null; emit('close'); } function updateproductmodif(element: IProduct) { // console.log('CSEARCHPRODUCT: updateproductmodif'); emit('updateproductmodif', element); } async function searchOnGM(mystr: string) { // refreshSingleBookFromGM({usaDBGMLocale: false}) const options = {}; const ris = await globalStore.updateAllBookFromGM_T_Web_Articoli({ sku: '', isbn: mystr, ...options }); if (ris) { const id = getSearchId(); loadProduct(id); updateproductmodif(myproduct.value); // await updateproduct(false) if (ris.error) { tools.showNegativeNotif($q, ris.error); } else { tools.showPositiveNotif($q, t('dbgm.updateLocalDb_OK')); } // updatefromgm.value = false } } onMounted(mounted); return { userStore, costanti, tools, toolsext, search, shared_consts, productStore, t, loadpage, refreshpage, show_hide, searchList, fieldsTable, mycolumns, naviga, myproduct, optcatalogo, idPage, selauthor, valoriopt, labelcombo, searchText, insertProd, focus, clickClose, saveSearch, updateproductmodif, searchOnGM, where, }; }, });