11let docIdEBD = 0 ;
22let idsSetedEBD = new Set ( ) ;
33let dataSetedEBD = new Set ( ) ;
4- let fileCodeEBD = '' ;
54
65function editarBorrarDocumentoInit ( ) {
7- document . title = "Editar/Borrar Documento" ;
8- document . getElementById ( 'slugTitle' ) . innerHTML = '<h2 class="text-xl font-semibold mb-4">Editar/Borrar Documento</h2>' ;
9-
10- initPageEBD ( ) ;
11- }
12-
13- function initPageEBD ( ) {
146 let pageRoute = parseHashRoute ( ) ;
157 docIdEBD = pageRoute . params . doc_id ;
168
17- getDepartmentTagsCND ( ) ;
18- getDocumentById ( ) ;
9+ document . title = "Editar/borrar documento" ;
10+ document . getElementById ( 'slugTitle' ) . innerHTML = '<h2 class="text-xl font-semibold mb-4">Editar/borrar documento</h2>' ;
11+
12+ getDepartmentTagsEBD ( ) ;
13+ getDocumentByIdEBD ( ) ;
1914}
2015
2116function paintDocEBD ( doc ) {
22- document . getElementById ( 'file_name_edb' ) . innerHTML = doc . file_name ;
2317 document . getElementById ( 'doc_title_edb' ) . value = doc . title ;
18+ document . getElementById ( 'slugTitle' ) . innerHTML = `<h2 class="text-xl font-semibold mb-4">Editar/borrar ${ doc . title } </h2>` ;
2419 document . getElementById ( 'doc_description_edb' ) . value = doc . descrption ;
2520 document . getElementById ( 'doc_date_edb' ) . value = doc . expiration_date ;
2621 document . getElementById ( 'email_aviso_edb' ) . value = doc . notification_emails ;
2722}
2823
29- function paintTagsEBD ( tags ) {
30- if ( tags && tags . length > 0 ) { // console.log(tags)
24+ function paintDocTagsEBD ( tags ) {
25+ if ( tags && tags . length > 0 ) {
3126 for ( let i = 0 ; i < tags . length ; i ++ ) {
3227 if ( idsSetedEBD . has ( tags [ i ] . tag_id ) ) { continue ; }
3328 idsSetedEBD . add ( tags [ i ] . tag_id ) ;
@@ -37,20 +32,25 @@ function paintTagsEBD(tags){
3732 }
3833}
3934
40- function getDocumentById ( ) {
41- onlyGet ( 'public/doc/get/doc_by_id/?doc_id=' + docIdEBD , ( r ) => {
35+ function getDocumentByIdEBD ( ) {
36+ onlyGet ( 'public/doc/get/doc_by_id/?doc_id=' + docIdEBD + '&user_id=' + window . localStorage . getItem ( 'user_id' ) , ( r ) => {
37+ console . log ( r . data )
38+ /*
39+ I only allow the user who created the document to edit it
40+ Solo permito que el usuario que creó el documento lo edite,
41+ quizas habria que hacer desde el usuario que tiene action_pass
42+ */
4243 if ( r && r . data && r . data . doc && r . data . doc && r . data . doc . user_id == window . localStorage . getItem ( 'user_id' ) ) {
43- fileCodeEBD = r . data . doc . code ;
4444 paintDocEBD ( r . data . doc ) ;
45- paintTagsEBD ( r . data . tags ) ;
46- showInframeEBD ( r . data . doc . file_name , r . data . doc . code ) ;
45+ paintDocTagsEBD ( r . data . doc_tags ) ;
46+ showInframeEBD ( r . data . doc_lines ) ;
4747 } else {
4848 showM ( 'No hay suficientes permisos..' , 'warning' )
4949 }
5050 } ) ;
5151}
5252
53- function getDepartmentTagsCND ( ) {
53+ function getDepartmentTagsEBD ( ) {
5454 suzdalenkoGet ( 'documentor/tag/get/get_department_tags/' , function ( response ) {
5555 if ( response && response . data && response . data ) {
5656 document . getElementById ( 'tagsListEBD' ) . innerHTML = '' ;
@@ -89,13 +89,58 @@ function showTagsInDocEBD(){
8989 document . getElementById ( 'selectedTagsEDB' ) . innerHTML = htmlTagsEDB ;
9090}
9191
92- function showPrevieEBD ( ) {
93- window . open ( HTTP_HOST + 'public/doc/get/serve_document/?code=' + fileCodeEBD , '_blank' ) ;
92+ function showInframeEBD ( doc_tags ) {
93+ let fileNames = '' ;
94+ let framesHtml = '' ;
95+ doc_tags . map ( l => {
96+ let url = HTTP_HOST + 'public/doc/get/serve_document/?code=' + l . code ;
97+ let url2 = HTTP_HOST + 'public/doc/get/serve_document/?code=' + l . code ;
98+ if ( ! l . file_name . includes ( '.pdf' ) ) { url = 'https://suzdalenko-dev.github.io/html-documentor/assets/img/template.png' ; }
99+ framesHtml += `<div>
100+ <iframe src="${ url } " class="doc-preview"></iframe>
101+ <div class="doc-actions"><button class="btn-view" onclick="window.open('${ url2 } ', '_blank')">👁️ Ver </button></div>
102+ </div>` ;
103+ fileNames += `<a href="${ url2 } " target="_blank" class="link_id_dlg ml-3"> ${ l . file_name } </a>` ;
104+ } ) ;
105+ document . getElementById ( 'link_files_ebd' ) . innerHTML = fileNames ;
106+ document . getElementById ( 'iframeContainerEBD' ) . innerHTML = framesHtml ;
94107}
95108
96- function showInframeEBD ( fileName , code ) {
97- if ( fileName . includes ( ".pdf" ) ) {
98- document . getElementById ( 'iframeEBD' ) . style . display = 'block' ;
99- document . getElementById ( 'iframeEBD' ) . src = HTTP_HOST + 'public/doc/get/serve_document/?code=' + fileCodeEBD ;
109+ /* UPDATE BUTTON */
110+ function updateEBD ( ) {
111+ let title = document . getElementById ( 'doc_title_edb' ) . value . trim ( ) ;
112+ let description = document . getElementById ( 'doc_description_edb' ) . value . trim ( ) ;
113+ let expiration_date = document . getElementById ( 'doc_date_edb' ) . value ;
114+ let email_aviso = document . getElementById ( 'email_aviso_edb' ) . value . trim ( ) ;
115+ let fileInput = document . getElementById ( 'doc_file_edb' ) ;
116+ let files = fileInput . files ;
117+ let tags = Array . from ( dataSetedEBD ) . map ( tag => tag . id ) . join ( ',' ) ;
118+
119+ if ( ! title || ! expiration_date || ! email_aviso ) {
120+ showM ( 'Por favor, completa todos los campos del formulario.' , 'warning' ) ;
121+ return ;
100122 }
123+ let formData = new FormData ( ) ;
124+ formData . append ( 'doc_id' , docIdEBD )
125+ formData . append ( 'title' , title ) ;
126+ formData . append ( 'description' , description ) ;
127+ formData . append ( 'expiration_date' , expiration_date ) ;
128+ formData . append ( 'email_aviso' , email_aviso ) ;
129+ formData . append ( 'tags' , tags ) ;
130+
131+ // 🔹 Añadir cada archivo al FormData
132+ for ( let i = 0 ; i < files . length ; i ++ ) { formData . append ( 'files' , files [ i ] ) ; }
133+
134+ fetch ( HTTP_HOST + 'documentor/doc/post/update_old_doc/' , {
135+ method : 'POST' ,
136+ body : formData ,
137+ headers : { 'Authorization' : 'Bearer ' + ( window . localStorage . getItem ( 'token' ) || 'xxx' ) }
138+ } ) . then ( res => res . json ( ) ) . then ( res => {
139+ if ( res && res . data && res . data . error == 'no' && res . data . id > 0 ) {
140+ showM ( 'Documento guardado correctamente ✅' , 'success' ) ;
141+ setTimeout ( ( ) => { window . location . href = '/dashboard/#ver-detalle-documento?doc_id=' + res . data . id ; } , 1000 ) ;
142+ } else {
143+ showM ( 'Error al guardar: ' + ( res . message || 'Error desconocido' ) , 'error' ) ;
144+ }
145+ } ) . catch ( err => showM ( 'Error de conexión: ' + err , 'error' ) ) ;
101146}
0 commit comments