2025-03-31 23:55:53 +02:00
|
|
|
import { defineComponent, onMounted, ref, watch } from "vue";
|
|
|
|
|
import draggable from 'vuedraggable'
|
|
|
|
|
|
|
|
|
|
import { tools } from '@tools'
|
|
|
|
|
|
|
|
|
|
import { useGlobalStore } from '@src/store/globalStore'
|
|
|
|
|
|
2025-04-01 18:36:45 +02:00
|
|
|
import { CSearchProduct } from '@src/components/CSearchProduct'
|
|
|
|
|
import { CMyDialog } from '@src/components/CMyDialog'
|
|
|
|
|
|
2025-03-31 23:55:53 +02:00
|
|
|
import { costanti } from '@costanti'
|
|
|
|
|
import { IAuthor, ICatProd } from "app/src/model";
|
|
|
|
|
|
2025-04-01 18:36:45 +02:00
|
|
|
import type {
|
|
|
|
|
IProduct
|
|
|
|
|
} from '@src/model';
|
|
|
|
|
|
|
|
|
|
|
2025-03-31 23:55:53 +02:00
|
|
|
export default defineComponent({
|
|
|
|
|
name: "CProductTable",
|
|
|
|
|
components: {
|
2025-04-01 18:36:45 +02:00
|
|
|
draggable, CSearchProduct, CMyDialog,
|
2025-03-31 23:55:53 +02:00
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
lista_prodotti: {
|
|
|
|
|
type: Array,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
emits: ["update:lista_prodotti"],
|
|
|
|
|
setup(props, { emit }) {
|
|
|
|
|
// Copia locale della lista_prodotti per manipolazione interna
|
|
|
|
|
const internalProducts = ref([...props.lista_prodotti]);
|
|
|
|
|
|
|
|
|
|
const globalStore = useGlobalStore()
|
|
|
|
|
|
2025-04-01 18:36:45 +02:00
|
|
|
const showProd = ref(false)
|
|
|
|
|
const selProd = ref(<IProduct>{})
|
|
|
|
|
|
2025-03-31 23:55:53 +02:00
|
|
|
async function mounted() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Aggiorna la copia locale quando il prop cambia
|
|
|
|
|
watch(
|
|
|
|
|
() => props.lista_prodotti,
|
|
|
|
|
(newVal) => {
|
|
|
|
|
internalProducts.value = [...newVal];
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Colonne della tabella
|
|
|
|
|
const allColumns = [
|
2025-04-01 18:36:45 +02:00
|
|
|
{ name: "drag", label: "Ordinamento", field: "", align: "left", style: "width: 50px" },
|
|
|
|
|
{ name: "image", label: "Copertina", field: "image", align: "center" },
|
|
|
|
|
{ name: "name", label: "Titolo", field: "name", align: "left" },
|
|
|
|
|
{ name: "authors", label: "Autore", field: "authors", align: "left" },
|
|
|
|
|
{ name: "catprods", label: "Argomento", field: "catprods", align: "left" },
|
|
|
|
|
{ name: "quantity", label: "Disponibilità", field: "quantity", align: "left" },
|
2025-03-31 23:55:53 +02:00
|
|
|
{ name: "isbn", label: "ISBN", field: "isbn", align: "left" },
|
|
|
|
|
{ name: "actions", label: "Azioni", field: "", align: "center" },
|
|
|
|
|
];
|
|
|
|
|
|
2025-04-01 18:36:45 +02:00
|
|
|
let cookieValue: string | null = null;
|
|
|
|
|
try {
|
|
|
|
|
cookieValue = tools.getCookie("selColCat");
|
|
|
|
|
// Se il cookie esiste e contiene una stringa JSON valida
|
|
|
|
|
cookieValue = cookieValue ? cookieValue : [];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Errore durante la lettura del cookie 'selColCat'", error);
|
|
|
|
|
cookieValue = []; // In caso di errore, inizializza come array vuoto
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectedColumns = ref(cookieValue.length > 0 ? cookieValue : ["drag", "image", "name", "authors", "catprods", "isbn", "actions"]);
|
|
|
|
|
|
2025-03-31 23:55:53 +02:00
|
|
|
|
|
|
|
|
// 3. Funzione per verificare se una colonna è visibile (isColumnVisible)
|
|
|
|
|
const isColumnVisible = (column) => {
|
|
|
|
|
return selectedColumns.value.includes(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Funzione per eliminare un prodotto
|
|
|
|
|
const removeProduct = (product) => {
|
|
|
|
|
internalProducts.value = internalProducts.value.filter((p: any) => p._id !== product._id);
|
|
|
|
|
emit("update:lista_prodotti", internalProducts.value); // Notifica il parent del cambiamento
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 8. Salvataggio delle colonne selezionate in un cookie
|
|
|
|
|
const saveSelectedColumns = () => {
|
|
|
|
|
tools.setCookie("selColCat", JSON.stringify(selectedColumns.value));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 9. Watcher per salvare automaticamente le preferenze quando cambiano
|
|
|
|
|
watch(() => selectedColumns.value, () => {
|
|
|
|
|
saveSelectedColumns();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Funzione chiamata alla fine del drag-and-drop
|
|
|
|
|
const onDragEnd = () => {
|
|
|
|
|
// console.log("Nuovo ordine:", internalProducts.value);
|
|
|
|
|
emit("update:lista_prodotti", internalProducts.value); // Notifica il parent del cambiamento
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatAuthors(authors: IAuthor[] | undefined | null): string {
|
|
|
|
|
if (!authors || !Array.isArray(authors)) {
|
|
|
|
|
return ""; // Restituisci una stringa vuota se authors non è un array valido
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Estrai il nome e il cognome di ogni autore e uniscili con ', '
|
|
|
|
|
return authors
|
|
|
|
|
.map((author) => `${author.name ?? ""} ${author.surname ?? ""}`.trim())
|
|
|
|
|
.filter((name) => name.length > 0) // Filtra eventuali nomi vuoti
|
|
|
|
|
.join(", ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatCatProds(catprods: ICatProd[] | undefined | null): string {
|
|
|
|
|
if (!catprods || !Array.isArray(catprods)) {
|
2025-04-01 18:36:45 +02:00
|
|
|
return "";
|
2025-03-31 23:55:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Estrai il nome e il cognome di ogni autore e uniscili con ', '
|
|
|
|
|
return catprods
|
|
|
|
|
.map((catprod) => `${catprod.name ?? ""}`.trim())
|
|
|
|
|
.filter((name) => name.length > 0) // Filtra eventuali nomi vuoti
|
|
|
|
|
.join(", ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Caricamento delle preferenze al mount del componente
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
const savedColumns = tools.getCookie("selColCat");
|
|
|
|
|
if (savedColumns) {
|
2025-04-01 18:36:45 +02:00
|
|
|
selectedColumns.value = savedColumns;
|
2025-03-31 23:55:53 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-01 18:36:45 +02:00
|
|
|
function showProduct(element: any) {
|
|
|
|
|
selProd.value = element
|
|
|
|
|
|
|
|
|
|
showProd.value = true
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-31 23:55:53 +02:00
|
|
|
return {
|
|
|
|
|
allColumns,
|
|
|
|
|
selectedColumns,
|
|
|
|
|
isColumnVisible,
|
|
|
|
|
internalProducts,
|
|
|
|
|
formatAuthors,
|
|
|
|
|
formatCatProds,
|
|
|
|
|
removeProduct,
|
|
|
|
|
tools,
|
|
|
|
|
globalStore,
|
|
|
|
|
costanti,
|
|
|
|
|
onDragEnd,
|
2025-04-01 18:36:45 +02:00
|
|
|
showProduct,
|
|
|
|
|
showProd,
|
|
|
|
|
selProd,
|
2025-03-31 23:55:53 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|