332 lines
11 KiB
PHP
332 lines
11 KiB
PHP
<?php
|
|
|
|
// Verificar se o usuário está logado
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
// Definir quantos clientes serão exibidos por página
|
|
define('RESULTS_PER_PAGE', 20);
|
|
|
|
// Obter a página atual a partir do parâmetro 'page' na URL, padrão para 1
|
|
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;
|
|
|
|
// Calcular o deslocamento
|
|
$offset = ($page - 1) * RESULTS_PER_PAGE;
|
|
|
|
// Obter o termo de pesquisa, se houver
|
|
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
|
|
|
|
// Conectar ao banco de dados
|
|
$mysqli = new mysqli("10.15.0.42", "root", "34641747", "valida");
|
|
|
|
// Verificar erros na conexão
|
|
if ($mysqli->connect_error) {
|
|
die("Falha na conexão: " . htmlspecialchars($mysqli->connect_error, ENT_QUOTES, 'UTF-8'));
|
|
}
|
|
|
|
// Preparar a consulta com pesquisa e paginação
|
|
if ($search !== '') {
|
|
// Utilizar consultas preparadas para prevenir SQL Injection
|
|
$stmt = $mysqli->prepare("
|
|
SELECT *
|
|
FROM clients
|
|
WHERE name LIKE CONCAT('%', ?, '%')
|
|
OR cpf LIKE CONCAT('%', ?, '%')
|
|
OR email LIKE CONCAT('%', ?, '%')
|
|
LIMIT ? OFFSET ?
|
|
");
|
|
$stmt->bind_param("sssii", $search, $search, $search, $limit, $offset);
|
|
} else {
|
|
$stmt = $mysqli->prepare("SELECT * FROM clients LIMIT ? OFFSET ?");
|
|
$stmt->bind_param("ii", $limit, $offset);
|
|
}
|
|
|
|
// Definir os parâmetros de limite e offset
|
|
$limit = RESULTS_PER_PAGE;
|
|
|
|
// Executar a consulta
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="pt-BR">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Listagem de Clientes</title>
|
|
<link rel="stylesheet" href="style.css">
|
|
<!-- Inclusão do Font Awesome para ícones -->
|
|
<script src="../script/formatacoes.js"></script>
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
|
|
integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
|
|
crossorigin="anonymous" referrerpolicy="no-referrer" />
|
|
|
|
<!-- Inclusão do jQuery e jQuery UI para autocomplete -->
|
|
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
|
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
|
|
crossorigin="anonymous"></script>
|
|
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"
|
|
crossorigin="anonymous"></script>
|
|
|
|
<style>
|
|
/* Estilização opcional para os ícones de ação */
|
|
.action-icons a, .action-icons button {
|
|
margin-right: 10px;
|
|
color: #333;
|
|
text-decoration: none;
|
|
transition: color 0.3s;
|
|
background: none;
|
|
border: none;
|
|
cursor: pointer;
|
|
font-size: 1em;
|
|
}
|
|
|
|
.action-icons a:hover, .action-icons button:hover {
|
|
color: #007BFF; /* Cor azul ao passar o mouse */
|
|
}
|
|
|
|
.action-icons button.delete {
|
|
color: #DC3545; /* Cor vermelha para excluir */
|
|
}
|
|
|
|
.action-icons button.delete:hover {
|
|
color: #C82333;
|
|
}
|
|
|
|
.action-icons a.edit {
|
|
color: #28A745; /* Cor verde para editar */
|
|
}
|
|
|
|
.action-icons a.edit:hover {
|
|
color: #218838;
|
|
}
|
|
|
|
/* Estilo para o botão de exclusão dentro do formulário */
|
|
.action-icons button.delete {
|
|
padding: 0;
|
|
}
|
|
|
|
/* Opcional: Estilização da tabela */
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
table, th, td {
|
|
border: 1px solid #ddd;
|
|
padding: 8px;
|
|
}
|
|
|
|
th {
|
|
background-color: #f4f4f4;
|
|
text-align: left;
|
|
}
|
|
|
|
tr:nth-child(even) {
|
|
background-color: #f9f9f9;
|
|
}
|
|
|
|
.search-bar-clientes {
|
|
width: 1500px;
|
|
padding: 8px;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
|
|
#search {
|
|
width: 300px;
|
|
padding: 8px;
|
|
font-size: 1em;
|
|
}
|
|
|
|
#search-button {
|
|
padding: 8px 12px;
|
|
font-size: 1em;
|
|
cursor: pointer;
|
|
background-color: #007BFF;
|
|
color: #fff;
|
|
border: none;
|
|
border-radius: 4px;
|
|
transition: background-color 0.3s;
|
|
}
|
|
|
|
#search-button:hover {
|
|
background-color: #0056b3;
|
|
}
|
|
|
|
/* Estilos para a paginação */
|
|
.pagination {
|
|
margin-top: 20px;
|
|
text-align: center;
|
|
}
|
|
|
|
.pagination a, .pagination span {
|
|
display: inline-block;
|
|
margin: 0 5px;
|
|
padding: 8px 12px;
|
|
text-decoration: none;
|
|
color: #007BFF;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
transition: background-color 0.3s, color 0.3s;
|
|
}
|
|
|
|
.pagination a:hover {
|
|
background-color: #007BFF;
|
|
color: #fff;
|
|
}
|
|
|
|
.pagination .current-page {
|
|
background-color: #007BFF;
|
|
color: #fff;
|
|
border-color: #007BFF;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h2>Lista de Clientes</h2>
|
|
<div class="search-bar-clientes">
|
|
<input type="text" id="search" placeholder="Pesquisar por nome, CPF ou email" value="<?php echo htmlspecialchars($search, ENT_QUOTES, 'UTF-8'); ?>">
|
|
<button type="button" id="search-button">Pesquisar</button>
|
|
</div>
|
|
<?php
|
|
if ($result->num_rows > 0) {
|
|
echo "<table>
|
|
<tr>
|
|
<th>Nome</th>
|
|
<th>CPF</th>
|
|
<th>Telefone</th>
|
|
<th>Email</th>
|
|
<th>Ações</th>
|
|
</tr>";
|
|
|
|
// Iterar sobre cada cliente
|
|
while($row = $result->fetch_assoc()) {
|
|
// Escapar saídas para prevenir XSS
|
|
$id = htmlspecialchars($row["id"], ENT_QUOTES, 'UTF-8');
|
|
$nome = htmlspecialchars($row["name"], ENT_QUOTES, 'UTF-8');
|
|
$cpf = htmlspecialchars($row["cpf"], ENT_QUOTES, 'UTF-8');
|
|
$telefone = htmlspecialchars($row["phone"], ENT_QUOTES, 'UTF-8');
|
|
$email = htmlspecialchars($row["email"], ENT_QUOTES, 'UTF-8');
|
|
|
|
echo "<tr>
|
|
<td><a href='index.php?page=client_details&id=$id'>$nome</a></td>
|
|
<td>$cpf</td>
|
|
<td>$telefone</td>
|
|
<td>$email</td>
|
|
<td class='action-icons'>
|
|
<!-- Ícone de Editar -->
|
|
<a href='index.php?page=edit_client&id=$id' class='edit' title='Editar Cliente'><i class='fas fa-edit'></i></a>
|
|
|
|
<!-- Formulário para Excluir com método POST -->
|
|
<form action='clientes/delete_client.php' method='post' onsubmit=\"return confirm('Tem certeza que deseja excluir este cliente?');\" style='display:inline;'>
|
|
<input type='hidden' name='id' value='$id'>
|
|
<button type='submit' class='delete' title='Excluir Cliente'><i class='fas fa-trash'></i></button>
|
|
</form>
|
|
</td>
|
|
</tr>";
|
|
}
|
|
|
|
echo "</table>";
|
|
|
|
// Fechar o statement
|
|
$stmt->close();
|
|
} else {
|
|
echo "<p>Nenhum cliente encontrado.</p>";
|
|
}
|
|
|
|
// Contar o total de clientes para a paginação
|
|
if ($search !== '') {
|
|
$count_stmt = $mysqli->prepare("
|
|
SELECT COUNT(*)
|
|
FROM clients
|
|
WHERE name LIKE CONCAT('%', ?, '%')
|
|
OR cpf LIKE CONCAT('%', ?, '%')
|
|
OR email LIKE CONCAT('%', ?, '%')
|
|
");
|
|
$count_stmt->bind_param("sss", $search, $search, $search);
|
|
} else {
|
|
$count_stmt = $mysqli->prepare("SELECT COUNT(*) FROM clients");
|
|
}
|
|
|
|
$count_stmt->execute();
|
|
$count_stmt->bind_result($total_clients);
|
|
$count_stmt->fetch();
|
|
$count_stmt->close();
|
|
|
|
// Calcular o total de páginas
|
|
$total_pages = ceil($total_clients / RESULTS_PER_PAGE);
|
|
|
|
// Exibir links de paginação se houver mais de uma página
|
|
if ($total_pages > 1) {
|
|
echo "<div class='pagination'>";
|
|
// Link para a página anterior
|
|
if ($page > 1) {
|
|
$prev_page = $page - 1;
|
|
$url = 'list_clients.php?page=' . $prev_page;
|
|
if ($search !== '') {
|
|
$url .= '&search=' . urlencode($search);
|
|
}
|
|
echo "<a href='$url'>« Anterior</a>";
|
|
}
|
|
|
|
// Links para cada página (limitando a 5 páginas visíveis)
|
|
$max_display = 5; // Número máximo de páginas a exibir
|
|
$start = max(1, $page - floor($max_display / 2));
|
|
$end = min($total_pages, $start + $max_display - 1);
|
|
|
|
if ($start > 1) {
|
|
echo "<a href='list_clients.php?page=1" . ($search !== '' ? '&search=' . urlencode($search) : '') . "'>1</a>";
|
|
if ($start > 2) {
|
|
echo "<span>...</span>";
|
|
}
|
|
}
|
|
|
|
for ($i = $start; $i <= $end; $i++) {
|
|
$url = 'list_clients.php?page=' . $i;
|
|
if ($search !== '') {
|
|
$url .= '&search=' . urlencode($search);
|
|
}
|
|
if ($i == $page) {
|
|
echo "<span class='current-page'>$i</span>";
|
|
} else {
|
|
echo "<a href='$url'>$i</a>";
|
|
}
|
|
}
|
|
|
|
if ($end < $total_pages) {
|
|
if ($end < $total_pages - 1) {
|
|
echo "<span>...</span>";
|
|
}
|
|
echo "<a href='list_clients.php?page=$total_pages" . ($search !== '' ? '&search=' . urlencode($search) : '') . "'>$total_pages</a>";
|
|
}
|
|
|
|
// Link para a próxima página
|
|
if ($page < $total_pages) {
|
|
$next_page = $page + 1;
|
|
$url = 'list_clients.php?page=' . $next_page;
|
|
if ($search !== '') {
|
|
$url .= '&search=' . urlencode($search);
|
|
}
|
|
echo "<a href='$url'>Próxima »</a>";
|
|
}
|
|
|
|
echo "</div>";
|
|
}
|
|
|
|
// Fechar a conexão com o banco de dados
|
|
$mysqli->close();
|
|
?>
|
|
</div>
|
|
<script>
|
|
$(document).ready(function() {
|
|
formatarTabela('table', 2, 3); // 'table' é o seletor da sua tabela, 2 e 3 são as colunas de CPF e Telefone
|
|
});
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|