1- // Não definimos API_BASE_URL — usamos as rotas do Flask como proxy
2- const API_BASE_URL = "" ;
1+ // Configuração da API
2+ const API_BASE_URL = "http://localhost:8000" ;
3+
4+ // Conjunto de carrinhos selecionados
5+ let selectedCarts = new Set ( ) ;
36
47// Helper para obter token de autenticação
58function getAuthToken ( ) {
@@ -46,8 +49,11 @@ function toggleSelect(cartId, cartName) {
4649 }
4750}
4851
49- // Conjunto de carrinhos selecionados
50- let selectedCarts = new Set ( ) ;
52+ // Mostrar/esconder itens simples (não usado, mas mantido)
53+ function toggleItemList ( cartId ) {
54+ const itemsDiv = document . getElementById ( `items-${ cartId } ` ) ;
55+ itemsDiv . style . display = itemsDiv . style . display === 'none' ? 'block' : 'none' ;
56+ }
5157
5258// Finalizar carrinho
5359function finalizeSelection ( cartId ) {
@@ -75,16 +81,87 @@ function showPopup(message) {
7581 } , 3000 ) ;
7682}
7783
78- // Abrir pergaminho com itens (AGORA CHAMA O FLASK, NÃO O FASTAPI DIRETAMENTE)
84+ // Ações da navbar (não usada atualmente, mas mantida)
85+ function handleNavAction ( action ) {
86+ if ( selectedCarts . size === 0 ) {
87+ showPopup ( "⚠️ Selecione um carrinho antes de realizar esta ação!" ) ;
88+ return ;
89+ }
90+
91+ const cartId = Array . from ( selectedCarts ) [ 0 ] ;
92+
93+ if ( action === "view" ) {
94+ window . location . href = "/cart/" + cartId ;
95+ } else if ( action === "edit" ) {
96+ window . location . href = "/edit/" + cartId ;
97+ } else if ( action === "delete" ) {
98+ const skipConfirm = localStorage . getItem ( "skipDeleteConfirm" ) ;
99+ if ( skipConfirm === "true" ) {
100+ confirmDelete ( cartId ) ;
101+ return ;
102+ }
103+
104+ const confirmBox = document . createElement ( "div" ) ;
105+ confirmBox . className = "popup-confirm" ;
106+ confirmBox . innerHTML = `
107+ <p>Tem certeza que deseja deletar este carrinho?</p>
108+ <label><input type="checkbox" id="skipConfirm"> Não mostrar este aviso novamente</label>
109+ <div class="actions">
110+ <button onclick="confirmDelete(${ cartId } )">Sim</button>
111+ <button onclick="cancelDelete(this)">Cancelar</button>
112+ </div>
113+ ` ;
114+ document . body . appendChild ( confirmBox ) ;
115+ }
116+ }
117+
118+ // Confirmar exclusão de carrinho
119+ function confirmDelete ( cartId ) {
120+ const skip = document . getElementById ( "skipConfirm" ) ?. checked ;
121+ if ( skip ) localStorage . setItem ( "skipDeleteConfirm" , "true" ) ;
122+
123+ fetch ( `${ API_BASE_URL } /api/carrinhos/${ cartId } ` , {
124+ method : "DELETE" ,
125+ headers : {
126+ 'Authorization' : `Bearer ${ getAuthToken ( ) } `
127+ }
128+ } )
129+ . then ( response => {
130+ if ( response . ok ) {
131+ showPopup ( `🗑️ Carrinho deletado com sucesso!` ) ;
132+ finalizeSelection ( cartId ) ;
133+ setTimeout ( ( ) => location . reload ( ) , 1000 ) ;
134+ } else {
135+ if ( response . status === 401 ) {
136+ showPopup ( '🔒 Sessão expirada. Faça login novamente.' ) ;
137+ setTimeout ( ( ) => window . location . href = '/login' , 2000 ) ;
138+ } else {
139+ showPopup ( "❌ Erro ao deletar carrinho." ) ;
140+ }
141+ }
142+ } )
143+ . catch ( ( ) => showPopup ( "❌ Erro de conexão com o servidor." ) ) ;
144+
145+ cancelDelete ( document . querySelector ( ".popup-confirm .actions button:last-child" ) ) ;
146+ }
147+
148+ // Cancelar exclusão
149+ function cancelDelete ( el ) {
150+ el . closest ( ".popup-confirm" ) . remove ( ) ;
151+ }
152+
153+ // Abrir pergaminho com itens (SEM DUPLICAÇÃO)
154+ // Abrir pergaminho com itens (CORRIGIDA)
79155function abrirPergaminho ( cartId ) {
80156 const modal = document . getElementById ( 'pergaminho-modal' ) ;
81157 const overlay = document . getElementById ( 'pergaminho-overlay' ) ;
82158 const lista = document . getElementById ( 'lista-itens-modal' ) ;
83159 const som = document . getElementById ( 'som-pergaminho' ) ;
84160
161+ // Limpa a lista e mostra loading
85162 lista . innerHTML = '<li style="text-align:center; color:#3e2f1c; font-style:italic;">Carregando...</li>' ;
86163
87- fetch ( `/api/carrinhos/${ cartId } /itens` , {
164+ fetch ( `${ API_BASE_URL } /api/carrinhos/${ cartId } /itens` , {
88165 method : 'GET' ,
89166 headers : {
90167 'Authorization' : `Bearer ${ getAuthToken ( ) } `
@@ -96,6 +173,7 @@ function abrirPergaminho(cartId) {
96173 } )
97174 . then ( data => {
98175 lista . innerHTML = '' ;
176+
99177 if ( data . length === 0 ) {
100178 lista . innerHTML = '<li style="text-align:center; color:#3e2f1c;">Nenhum item encontrado</li>' ;
101179 } else {
@@ -122,7 +200,7 @@ function abrirPergaminho(cartId) {
122200 } ) ;
123201}
124202
125- // Deletar item específico (CHAMA O FLASK )
203+ // Deletar item específico (COM AUTENTICAÇÃO E URL CORRETA )
126204function deletarItem ( itemId , liElement ) {
127205 if ( ! confirm ( 'Tem certeza que deseja deletar este item?' ) ) return ;
128206
@@ -133,7 +211,7 @@ function deletarItem(itemId, liElement) {
133211 return ;
134212 }
135213
136- fetch ( `/api/itens/${ itemId } ` , {
214+ fetch ( `${ API_BASE_URL } /api/itens/${ itemId } ` , {
137215 method : 'DELETE' ,
138216 headers : {
139217 'Authorization' : `Bearer ${ token } `
@@ -143,6 +221,7 @@ function deletarItem(itemId, liElement) {
143221 if ( res . ok ) {
144222 liElement . remove ( ) ;
145223 showPopup ( '✅ Item deletado!' ) ;
224+
146225 const lista = document . getElementById ( 'lista-itens-modal' ) ;
147226 if ( lista && lista . children . length === 0 ) {
148227 lista . innerHTML = '<li style="text-align:center; color:#3e2f1c;">Nenhum item encontrado</li>' ;
@@ -185,5 +264,7 @@ document.addEventListener("DOMContentLoaded", function () {
185264
186265// Expõe funções globais
187266window . toggleSelect = toggleSelect ;
267+ window . toggleItemList = toggleItemList ;
188268window . finalizeSelection = finalizeSelection ;
269+ window . handleNavAction = handleNavAction ;
189270window . abrirPergaminho = abrirPergaminho ;
0 commit comments