2025-09-27 17:24:40 +02:00
import { PropType , computed , defineComponent , onMounted , ref , watch } from 'vue' ;
2025-05-20 12:21:31 +02:00
import draggable from 'vuedraggable' ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
import { tools } from '@tools' ;
2025-04-30 13:27:47 +02:00
2025-12-05 17:56:05 +01:00
import { useGlobalStore } from '@/store/globalStore' ;
2025-04-30 13:27:47 +02:00
2025-12-05 17:56:05 +01:00
import { CMyEditorAI } from '@/components/CMyEditorAI' ;
import { CAITools } from '@/components/CAITools' ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
import { costanti } from '@costanti' ;
2025-04-30 13:27:47 +02:00
2025-12-05 17:56:05 +01:00
import type { IMyScheda , IProduct , IRecFields } from '@/model' ;
2025-05-20 12:21:31 +02:00
import { shared_consts } from 'app/src/common/shared_vuejs' ;
import { useProducts } from 'app/src/store/Products' ;
import { useI18n } from 'vue-i18n' ;
import { useQuasar } from 'quasar' ;
2025-04-30 13:27:47 +02:00
export default defineComponent ( {
2025-05-20 12:21:31 +02:00
name : 'CModifTrafiletto' ,
2025-04-30 13:27:47 +02:00
emits : [ 'updateproductmodif' , 'close' , 'Savedb' ] ,
components : {
2025-05-20 12:21:31 +02:00
CMyEditorAI ,
CAITools ,
2025-04-30 13:27:47 +02:00
} ,
props : {
modelValue : {
type : Object as PropType < IProduct > ,
required : true ,
} ,
table : {
type : String ,
required : true ,
} ,
mykey : {
type : String ,
required : true ,
} ,
mysubkey : {
type : String ,
required : false ,
default : '' ,
} ,
titolo : {
type : String ,
required : false ,
default : '' ,
} ,
type : {
2025-05-16 10:26:29 +02:00
type : Number ,
2025-04-30 13:27:47 +02:00
required : false ,
2025-05-16 10:26:29 +02:00
default : 0 ,
2025-04-30 13:27:47 +02:00
} ,
canModify : {
type : Boolean ,
required : false ,
default : false ,
} ,
maxlength : {
type : Number ,
required : false ,
default : 0 ,
2025-05-20 12:21:31 +02:00
} ,
2025-04-30 13:27:47 +02:00
} ,
setup ( props , { emit } ) {
// Copia locale della lista_prodotti per manipolazione interna
2025-05-20 12:21:31 +02:00
const $q = useQuasar ( ) ;
const { t } = useI18n ( ) ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
const globalStore = useGlobalStore ( ) ;
const products = useProducts ( ) ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
const mytab = ref ( 'descr' ) ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
const loading = ref ( false ) ;
2025-05-21 12:06:09 +02:00
const myscrapingbook = ref ( null ) ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
const updatetogm = ref ( false ) ;
const field_updated_toGM = ref ( '' ) ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
const myproduct = ref < IProduct > ( { . . . props . modelValue } ) ;
2025-04-30 13:27:47 +02:00
2025-09-27 17:24:40 +02:00
const id = computed ( ( ) = > myproduct . value . _id ) ;
2025-04-30 13:27:47 +02:00
const myvalue = computed < string > ( ( ) = > {
2025-09-27 17:24:40 +02:00
if ( props . mysubkey ) {
return myproduct . value [ props . mykey ] [ props . mysubkey ] ;
} else {
return myproduct . value [ props . mykey ] ;
}
2025-05-20 12:21:31 +02:00
} ) ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
watch (
( ) = > props . modelValue ,
( newVal ) = > {
myproduct . value = { . . . newVal } ;
} ,
{ deep : false }
) ;
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
const loadMyScrapingBook = async ( ) = > {
loading . value = true ;
try {
2025-05-21 12:06:09 +02:00
const myscrapingbookrec = await products . loadMyScrapingBook (
2025-09-27 17:24:40 +02:00
myproduct . value . isbn ,
false
2025-05-20 12:21:31 +02:00
) ;
2025-05-21 12:06:09 +02:00
myscrapingbook . value = myscrapingbookrec ;
2025-05-20 12:21:31 +02:00
} catch ( error ) {
console . error ( 'Errore caricamento MyScrapingBook:' , error ) ;
} finally {
loading . value = false ;
}
} ;
2025-04-30 13:27:47 +02:00
async function mounted() {
2025-05-20 12:21:31 +02:00
// carico anche altri dati del product
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
await loadMyScrapingBook ( ) ;
2025-04-30 13:27:47 +02:00
}
/ *
// Aggiorna la copia locale quando il prop cambia
watch (
( ) = > props . lista_prodotti ,
( newVal ) = > {
internalProducts . value = [ . . . newVal ] ;
}
) ;
* /
function updateproductmodif ( element : any ) {
// console.log('CModifTrafiletto updateproductmodif ', element)
2025-05-20 12:21:31 +02:00
emit ( 'updateproductmodif' , element ) ;
2025-04-30 13:27:47 +02:00
}
function Savedb ( element : any ) {
2025-05-20 12:21:31 +02:00
tools . saveInDBForTypes (
$q ,
props . mykey ,
element ,
props . type ,
true ,
props . table ,
props . mysubkey ,
id . value ,
null ,
''
) ;
2025-04-30 13:27:47 +02:00
}
async function updateproduct ( load? : boolean ) {
2025-09-27 17:24:40 +02:00
myproduct . value = await products . getProductById ( myproduct . value . _id , load ) ;
2025-04-30 13:27:47 +02:00
}
const copyToClipboard = ( text ) = > {
2025-05-20 12:21:31 +02:00
navigator . clipboard
. writeText ( text )
2025-04-30 13:27:47 +02:00
. then ( ( ) = > {
$q . notify ( {
message : 'Testo copiato negli appunti!' ,
color : 'positive' ,
icon : 'check' ,
2025-05-20 12:21:31 +02:00
position : 'top' ,
2025-04-30 13:27:47 +02:00
} ) ;
} )
2025-05-20 12:21:31 +02:00
. catch ( ( err ) = > {
2025-04-30 13:27:47 +02:00
console . error ( 'Errore durante la copia:' , err ) ;
$q . notify ( {
message : 'Errore nella copia' ,
color : 'negative' ,
icon : 'error' ,
2025-05-20 12:21:31 +02:00
position : 'top' ,
2025-04-30 13:27:47 +02:00
} ) ;
} ) ;
2025-05-20 12:21:31 +02:00
} ;
2025-04-30 13:27:47 +02:00
2025-05-16 10:26:29 +02:00
function handleShowAndSave ( payload : any ) {
Savedb ( payload ) ;
updateproductmodif ( payload ) ;
}
2025-04-30 13:27:47 +02:00
2025-05-20 12:21:31 +02:00
function getPrompt() {
// Prompt:
2025-09-27 17:24:40 +02:00
let mydescr =
"Scrivimi la sinossi del libro che ti indicherò, che andrà in un catalogo libri, la lunghezza del testo finale dev'essere compresa tra 760 e 780 caratteri, senza spiegazione, senza titolo iniziale, solo la sinossi. Togli eventuali riferimenti a chi ha fatto la prefazione. Rendilo un po' accattivante, ma non troppo. Senza immagine. Non inserire nessun link o riferimenti esterni o citazioni esterne. L'output è solo la sinossi, senza altre spiegazioni. \n\n" ;
return mydescr ;
2025-05-20 12:21:31 +02:00
}
function copyDescrizioneFromScrapingData() {
let mydescr = '' ;
2025-05-21 12:06:09 +02:00
const data = myscrapingbook . value ;
2025-05-20 12:21:31 +02:00
if ( ! data ) return false ;
2025-09-27 17:24:40 +02:00
mydescr = getPrompt ( ) ;
2025-05-20 12:21:31 +02:00
mydescr += 'Titolo Libro: ' + data . titolo + '\n' ;
2025-09-27 17:24:40 +02:00
mydescr +=
'Autore Libro: ' +
( data ? . autore . trim ( )
? data ? . autore . trim ( )
: products . getAutoriByArrayAuthors ( myproduct ? . value . productInfo ? . authors ) ) +
'\n' ;
2025-05-20 12:21:31 +02:00
mydescr += 'DESCRIZIONE LIBRO: \n' ;
mydescr += data . descrizione_lunga ;
tools . copyStringToClipboard ( $q , mydescr , false ) ;
}
function copyDescrizioneFromGruppoMacro() {
let mydescr = '' ;
2025-09-27 17:24:40 +02:00
mydescr = getPrompt ( ) ;
2025-05-20 12:21:31 +02:00
mydescr += 'Titolo Libro: ' + myproduct ? . value . productInfo ? . name + '\n' ;
2025-09-27 17:24:40 +02:00
mydescr +=
'Autore Libro: ' +
products . getAutoriByArrayAuthors ( myproduct ? . value . productInfo ? . authors ) +
'\n' ;
2025-05-20 12:21:31 +02:00
mydescr += 'DESCRIZIONE LIBRO: \n' ;
mydescr += myproduct ? . value . productInfo ? . descrizione_completa_macro ;
tools . copyStringToClipboard ( $q , mydescr , false ) ;
}
onMounted ( mounted ) ;
2025-04-30 13:27:47 +02:00
return {
tools ,
globalStore ,
costanti ,
shared_consts ,
t ,
products ,
mytab ,
myproduct ,
updateproductmodif ,
Savedb ,
updatetogm ,
field_updated_toGM ,
loading ,
copyToClipboard ,
myvalue ,
2025-05-16 10:26:29 +02:00
handleShowAndSave ,
2025-05-20 12:21:31 +02:00
copyDescrizioneFromScrapingData ,
copyDescrizioneFromGruppoMacro ,
2025-05-21 12:06:09 +02:00
myscrapingbook ,
2025-05-20 12:21:31 +02:00
} ;
} ,
} ) ;