2025-04-13 22:30:05 -03:00
{% extends "base.html" %}
2025-04-03 15:58:07 -03:00
{% block title %}Dashboard Administrativo{% endblock %}
{% block content %}
2025-04-13 22:30:05 -03:00
< div class = "container mt-4" >
< div class = "card" >
< div class = "card-header bg-dark text-white" >
< h3 class = "mb-0" > < i class = "fas fa-users-cog" > < / i > Administração de Usuários< / h3 >
2025-04-03 15:58:07 -03:00
< / div >
< div class = "card-body" >
2025-04-13 22:30:05 -03:00
< div class = "alert alert-info" role = "alert" >
< i class = "fas fa-info-circle" > < / i > Aqui você pode gerenciar todos os usuários do sistema. Use os controles abaixo para ativar/desativar contas ou alterar níveis de acesso.
< / div >
2025-04-03 15:58:07 -03:00
< div class = "table-responsive" >
2025-04-13 22:30:05 -03:00
< table class = "table table-hover" >
< thead class = "thead-light" >
2025-04-03 15:58:07 -03:00
< tr >
< th > Usuário< / th >
< th > Email< / th >
2025-04-13 22:30:05 -03:00
< th > Nome< / th >
< th > Último Acesso< / th >
< th > Status< / th >
< th > Nível< / th >
2025-04-03 15:58:07 -03:00
< th > Ações< / th >
< / tr >
< / thead >
< tbody >
{% for usuario in usuarios %}
< tr >
< td > {{ usuario.username }}< / td >
< td > {{ usuario.email }}< / td >
2025-04-13 22:30:05 -03:00
< td > {{ usuario.nome }}< / td >
< td > {{ usuario.last_login }}< / td >
2025-04-03 15:58:07 -03:00
< td >
2025-04-13 22:30:05 -03:00
< span class = "badge {% if usuario.ativo %}badge-success{% else %}badge-danger{% endif %}" >
{{ "Ativo" if usuario.ativo else "Inativo" }}
< / span >
2025-04-03 15:58:07 -03:00
< / td >
< td >
2025-04-13 22:30:05 -03:00
{% if usuario.is_admin %}
Administrador
2025-04-03 15:58:07 -03:00
{% else %}
2025-04-13 22:30:05 -03:00
{{ usuario.nivel }}
2025-04-03 15:58:07 -03:00
{% endif %}
< / td >
< td >
2025-04-13 22:30:05 -03:00
< div class = "btn-group" role = "group" >
< button class = "btn btn-sm btn-outline-primary"
onclick="toggleStatus('{{ usuario.id }}')"
data-toggle="tooltip"
title="{{ 'Desativar' if usuario.ativo else 'Ativar' }} usuário">
< i class = "fas {% if usuario.ativo %}fa-user-times{% else %}fa-user-check{% endif %}" > < / i >
< / button >
< button class = "btn btn-sm btn-outline-warning"
onclick="resetarSenha('{{ usuario.id }}')"
data-toggle="tooltip"
title="Resetar senha">
< i class = "fas fa-key" > < / i >
2025-04-03 15:58:07 -03:00
< / button >
2025-04-13 22:30:05 -03:00
{% if not usuario.is_admin %}
< button class = "btn btn-sm btn-outline-info"
onclick="alterarNivel('{{ usuario.id }}')"
data-toggle="tooltip"
title="Alterar nível">
< i class = "fas fa-level-up-alt" > < / i >
< / button >
{% endif %}
< / div >
2025-04-03 15:58:07 -03:00
< / td >
< / tr >
{% endfor %}
< / tbody >
< / table >
< / div >
< / div >
< / div >
2025-04-13 22:30:05 -03:00
< / div >
<!-- Modal de Feedback -->
< div class = "modal fade" id = "feedbackModal" tabindex = "-1" role = "dialog" >
< div class = "modal-dialog" role = "document" >
< div class = "modal-content" >
< div class = "modal-header" >
< h5 class = "modal-title" > Aviso< / h5 >
< button type = "button" class = "close" data-dismiss = "modal" >
< span > × < / span >
< / button >
< / div >
< div class = "modal-body" >
< p id = "feedbackMessage" > < / p >
< / div >
< div class = "modal-footer" >
< button type = "button" class = "btn btn-secondary" data-dismiss = "modal" > Fechar< / button >
2025-04-03 15:58:07 -03:00
< / div >
< / div >
< / div >
< / div >
2025-04-13 22:30:05 -03:00
{% endblock %}
{% block scripts %}
< script >
function showFeedback(message, type = 'info') {
const modal = document.getElementById('feedbackModal');
const messageElement = document.getElementById('feedbackMessage');
messageElement.textContent = message;
messageElement.className = `alert alert-${type}`;
$(modal).modal('show');
}
function handleResponse(response) {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
function toggleStatus(userId) {
if (!confirm('Tem certeza que deseja alterar o status deste usuário?')) {
return;
}
fetch(`/usuarios/${userId}/toggle_status`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').content
}
})
.then(handleResponse)
.then(data => {
showFeedback(data.message || 'Status alterado com sucesso!', data.success ? 'success' : 'danger');
if (data.success) {
setTimeout(() => location.reload(), 1500);
}
})
.catch(error => {
console.error('Error:', error);
showFeedback('Erro ao alterar status do usuário. Por favor, tente novamente.', 'danger');
});
}
function resetarSenha(userId) {
if (!confirm('Tem certeza que deseja resetar a senha deste usuário?')) {
return;
}
fetch(`/reset_password/${userId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').content
}
})
.then(handleResponse)
.then(data => {
showFeedback(data.message || 'Senha resetada com sucesso!', data.success ? 'success' : 'danger');
})
.catch(error => {
console.error('Error:', error);
showFeedback('Erro ao resetar senha. Por favor, tente novamente.', 'danger');
});
}
function alterarNivel(userId) {
const novoNivel = prompt('Digite o novo nível do usuário (1-5):');
if (!novoNivel) return;
if (!/^[1-5]$/.test(novoNivel)) {
showFeedback('Por favor, insira um nível válido entre 1 e 5.', 'warning');
return;
}
fetch(`/usuarios/${userId}/alterar_nivel`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').content
},
body: JSON.stringify({ nivel: parseInt(novoNivel) })
})
.then(handleResponse)
.then(data => {
showFeedback(data.message || 'Nível alterado com sucesso!', data.success ? 'success' : 'danger');
if (data.success) {
setTimeout(() => location.reload(), 1500);
}
})
.catch(error => {
console.error('Error:', error);
showFeedback('Erro ao alterar nível. Por favor, tente novamente.', 'danger');
});
}
// Inicializa os tooltips do Bootstrap
$(function () {
$('[data-toggle="tooltip"]').tooltip();
});
< / script >
2025-04-03 15:58:07 -03:00
{% endblock %}