-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlista.php
380 lines (348 loc) · 16.5 KB
/
lista.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
session_start();
require_once 'config/database.php';
require_once 'models/Lista.php';
// Verificar autenticação
if (!isset($_SESSION['user_id'])) {
error_log("Usuário não autenticado tentando acessar lista.php");
header('Location: login.php');
exit();
}
// Verificar se ID da lista foi passado
if (!isset($_GET['id']) || empty($_GET['id'])) {
error_log("Tentativa de acesso à lista.php sem ID");
header('Location: dashboard.php');
exit();
}
error_log("Acessando lista.php - ID: " . $_GET['id'] . " - Usuário: " . $_SESSION['user_id']);
// Inicializar lista
$lista = new Lista();
$lista->id = $_GET['id'];
$lista->usuario_id = $_SESSION['user_id'];
// Buscar detalhes da lista
$detalhesLista = $lista->obterDetalhesLista();
error_log("Detalhes da lista: " . print_r($detalhesLista, true));
if (!$detalhesLista || $detalhesLista['usuario_id'] != $_SESSION['user_id']) {
error_log("Lista não encontrada ou sem permissão - ID: " . $_GET['id'] . " - Usuário: " . $_SESSION['user_id']);
header('Location: dashboard.php');
exit();
}
// Buscar itens da lista
$itens = $lista->buscarItens();
error_log("Itens da lista: " . print_r($itens, true));
?>
<!DOCTYPE html>
<html lang="pt-BR" class="scroll-smooth">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($detalhesLista['nome']); ?> - MyList</title>
<link href="assets/css/tailwind.min.css" rel="stylesheet">
<link href="assets/css/custom.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
</head>
<body class="bg-gray-50 antialiased">
<div class="container mx-auto px-4 py-8">
<div class="max-w-2xl mx-auto bg-white shadow-lg rounded-xl p-6">
<div class="flex justify-between items-center mb-6">
<div>
<h1 class="text-3xl font-bold text-gray-800">
<?php echo htmlspecialchars($detalhesLista['nome']); ?>
</h1>
<p class="text-gray-600">
<?php echo htmlspecialchars($detalhesLista['descricao'] ?? 'Sem descrição'); ?>
</p>
</div>
<a href="dashboard.php" class="text-blue-600 hover:text-blue-800">
<i data-feather="arrow-left" class="w-6 h-6"></i>
</a>
</div>
<!-- Adicionar Item -->
<form id="formulario-adicionar-item" class="mb-6 flex space-x-2">
<input type="hidden" name="lista_id" value="<?php echo htmlspecialchars($detalhesLista['id']); ?>">
<input
type="text"
name="nome"
id="nomeItem"
placeholder="Nome do item"
required
class="flex-grow px-4 py-2 rounded-lg bg-gray-100 border focus:border-blue-500 focus:outline-none"
>
<input
type="number"
name="quantidade"
id="quantidadeItem"
placeholder="Qtd"
min="0.1"
step="0.1"
required
class="w-24 px-4 py-2 rounded-lg bg-gray-100 border focus:border-blue-500 focus:outline-none"
>
<select
name="unidade"
id="unidadeItem"
required
class="w-32 px-4 py-2 rounded-lg bg-gray-100 border focus:border-blue-500 focus:outline-none"
>
<option value="un">Unidade</option>
<option value="kg">Kg</option>
<option value="g">Gramas</option>
<option value="l">Litros</option>
<option value="ml">Mililitros</option>
</select>
<button
type="submit"
class="btn-primary px-4 py-2 rounded-lg flex items-center space-x-2"
>
<i data-feather="plus" class="w-5 h-5"></i>
</button>
</form>
<!-- Lista de Itens -->
<div id="listaItens" class="space-y-2">
<?php if(empty($itens)): ?>
<div class="text-center text-gray-500 py-4">
Nenhum item adicionado ainda
</div>
<?php else: ?>
<?php foreach($itens as $item): ?>
<div class="flex items-center justify-between bg-gray-50 p-3 rounded-lg">
<div class="flex items-center space-x-3">
<input
type="checkbox"
class="item-checkbox"
data-item-id="<?php echo $item['id']; ?>"
<?php echo $item['comprado'] ? 'checked' : ''; ?>
>
<span class="<?php echo $item['comprado'] ? 'line-through text-gray-500' : ''; ?>">
<?php echo htmlspecialchars($item['nome']); ?>
</span>
</div>
<div class="flex items-center space-x-4">
<span class="text-gray-600">
<?php echo htmlspecialchars($item['quantidade'] . ' ' . $item['unidade']); ?>
</span>
<button class="text-red-500 hover:text-red-700 excluir-item" data-item-id="<?php echo $item['id']; ?>">
<i data-feather="trash-2" class="w-4 h-4"></i>
</button>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Contadores -->
<div class="mt-6 flex justify-between text-sm text-gray-600">
<div>
Total de itens: <span id="totalItens"><?php echo count($itens); ?></span>
</div>
<div>
Itens comprados: <span id="itensConcluidos"><?php echo array_reduce($itens, function($carry, $item) {
return $carry + ($item['comprado'] ? 1 : 0);
}, 0); ?></span>
</div>
</div>
</div>
</div>
<script>
// Inicializar ícones Feather
feather.replace();
const listaId = <?php echo $detalhesLista['id']; ?>;
const totalItensElement = document.getElementById('totalItens');
const itensConcluidosElement = document.getElementById('itensConcluidos');
const listaItensElement = document.getElementById('listaItens');
// Função para mostrar toast de notificação
function showToast(message, type = 'success') {
const toast = document.createElement('div');
toast.className = `fixed top-4 right-4 p-4 rounded-lg shadow-lg z-50 ${
type === 'success' ? 'bg-green-500 text-white' : 'bg-red-500 text-white'
}`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
document.body.removeChild(toast);
}, 3000);
}
// Adicionar item
document.getElementById('formulario-adicionar-item').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
// Log para debug
console.log('Enviando dados:', {
lista_id: formData.get('lista_id'),
nome: formData.get('nome'),
quantidade: formData.get('quantidade'),
unidade: formData.get('unidade')
});
fetch('ajax/adicionar_item.php', {
method: 'POST',
body: formData
})
.then(async response => {
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
// Se a resposta não for JSON, lê como texto para debug
const text = await response.text();
console.error('Resposta não-JSON recebida:', text);
throw new Error('Resposta inválida do servidor');
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log('Resposta:', data);
if (data.sucesso) {
// Criar novo elemento de lista
const novoItem = document.createElement('div');
novoItem.className = 'flex items-center justify-between bg-gray-50 p-3 rounded-lg';
novoItem.innerHTML = `
<div class="flex items-center space-x-3">
<input type="checkbox"
class="form-checkbox h-5 w-5 text-blue-600 rounded border-gray-300 focus:ring-blue-500 item-checkbox"
data-item-id="${data.item_id}"
>
<span class="text-gray-700">${formData.get('nome')}</span>
</div>
<div class="flex items-center space-x-4">
<span class="text-gray-600">
${formData.get('quantidade')} ${formData.get('unidade')}
</span>
<button class="text-red-500 hover:text-red-700 excluir-item" data-item-id="${data.item_id}">
<i data-feather="trash-2" class="w-4 h-4"></i>
</button>
</div>
`;
// Remover mensagem de "nenhum item" se existir
const mensagemVazia = document.querySelector('.text-gray-500.py-4');
if (mensagemVazia) {
mensagemVazia.remove();
}
// Adicionar à lista
const listaItens = document.querySelector('#listaItens');
if (!listaItens) {
console.error('Elemento #listaItens não encontrado');
throw new Error('Elemento #listaItens não encontrado');
}
listaItens.appendChild(novoItem);
// Atualizar contadores
const totalItens = document.getElementById('totalItens');
if (totalItens) {
totalItens.textContent = parseInt(totalItens.textContent || '0') + 1;
}
// Reinicializar ícones
feather.replace();
// Limpar formulário
this.reset();
// Configurar listeners para o novo item
setupItemListeners(novoItem);
// Mostrar mensagem de sucesso
showToast(data.mensagem || 'Item adicionado com sucesso', 'success');
} else {
throw new Error(data.mensagem || 'Erro ao adicionar item');
}
})
.catch(error => {
console.error('Erro:', error);
showToast(error.message, 'error');
});
});
// Função para configurar listeners de itens
function setupItemListeners(itemElement) {
if (!itemElement) {
console.error('Item element não encontrado');
return;
}
// Marcar/desmarcar item
const checkbox = itemElement.querySelector('.item-checkbox');
if (!checkbox) {
console.error('Checkbox não encontrado no item');
return;
}
const span = itemElement.querySelector('span');
const excluirBotao = itemElement.querySelector('.excluir-item');
checkbox.addEventListener('change', function() {
const itemId = this.dataset.itemId;
const comprado = this.checked;
fetch('ajax/marcar_item.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `item_id=${itemId}&comprado=${comprado ? 1 : 0}`
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
if (data.sucesso) {
// Atualizar visual do item
if (comprado) {
span.classList.add('line-through', 'text-gray-500');
const itensConcluidos = document.getElementById('itensConcluidos');
if (itensConcluidos) {
itensConcluidos.textContent = parseInt(itensConcluidos.textContent) + 1;
}
} else {
span.classList.remove('line-through', 'text-gray-500');
const itensConcluidos = document.getElementById('itensConcluidos');
if (itensConcluidos) {
itensConcluidos.textContent = parseInt(itensConcluidos.textContent) - 1;
}
}
showToast('Status do item atualizado');
} else {
throw new Error(data.mensagem || 'Erro ao atualizar status');
}
})
.catch(error => {
console.error('Erro:', error);
showToast(error.message, 'error');
// Reverter checkbox para estado anterior
this.checked = !comprado;
});
});
if (excluirBotao) {
excluirBotao.addEventListener('click', function() {
const itemId = this.dataset.itemId;
if (confirm('Tem certeza que deseja excluir este item?')) {
fetch('ajax/remover_item.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `item_id=${itemId}`
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
if (data.sucesso) {
itemElement.remove();
const totalItens = document.getElementById('totalItens');
if (totalItens) {
totalItens.textContent = parseInt(totalItens.textContent) - 1;
}
showToast('Item removido com sucesso');
} else {
throw new Error(data.mensagem || 'Erro ao remover item');
}
})
.catch(error => {
console.error('Erro:', error);
showToast(error.message, 'error');
});
}
});
}
}
// Configurar listeners para itens existentes
document.querySelectorAll('#listaItens > div').forEach(setupItemListeners);
</script>
</body>
</html>