34394-vm/dashboard.php
2025-09-26 19:57:10 +00:00

279 lines
14 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
// If user is not logged in, redirect to login page
if (!isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
$displayName = $_SESSION['user_display_name'] ?? 'User';
$pdo = db();
$viewingClient = null;
$clients = [];
if (isset($_GET['client_id'])) {
$stmt = $pdo->prepare("SELECT * FROM clients WHERE client_id = ? AND user_id = ?");
$stmt->execute([$_GET['client_id'], $_SESSION['user_id']]);
$viewingClient = $stmt->fetch(PDO::FETCH_ASSOC);
$credentials = [];
$notes = [];
if ($viewingClient) {
$credStmt = $pdo->prepare("SELECT * FROM credentials WHERE client_id = ? ORDER BY name ASC");
$credStmt->execute([$viewingClient['client_id']]);
$credentials = $credStmt->fetchAll(PDO::FETCH_ASSOC);
$noteStmt = $pdo->prepare(
"SELECT n.*, u.display_name FROM notes n " .
"JOIN users u ON n.user_id = u.user_id " .
"WHERE n.client_id = ? ORDER BY n.created_at DESC"
);
$noteStmt->execute([$viewingClient['client_id']]);
$notes = $noteStmt->fetchAll(PDO::FETCH_ASSOC);
}
} else {
$stmt = $pdo->prepare("SELECT * FROM clients WHERE user_id = ? ORDER BY name ASC");
$stmt->execute([$_SESSION['user_id']]);
$clients = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard - FlexPass</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="dashboard.php">
<i class="bi bi-shield-lock"></i> ClientManager
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="dashboard.php">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link" href="audit-log.php">Audit Log</a>
</li>
</ul>
<div class="d-flex">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle"></i> <?php echo htmlspecialchars($displayName); ?>
</a>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="logout.php">Logout</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
<div class="container mt-4">
<?php if ($viewingClient): ?>
<!-- Client Detail View -->
<div class="d-flex justify-content-between align-items-center mb-3">
<a href="dashboard.php" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left"></i> Back to Client List</a>
<a href="add-credential.php?client_id=<?php echo htmlspecialchars($viewingClient['client_id']); ?>" class="btn btn-sm btn-primary"><i class="bi bi-plus-circle"></i> Add Credential</a>
</div>
<?php if (isset($_GET['status']) && $_GET['status'] === 'credential_added'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
New credential has been added successfully.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (isset($_GET['status']) && $_GET['status'] === 'credential_updated'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
Credential has been updated successfully.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (isset($_GET['status']) && $_GET['status'] === 'credential_deleted'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
Credential has been deleted successfully.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (isset($_GET['status']) && $_GET['status'] === 'note_added'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
New note has been added successfully.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (isset($_GET['status']) && $_GET['status'] === 'note_deleted'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
Note has been deleted successfully.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="card">
<div class="card-header">
<h2 class="mb-0">
<?php echo htmlspecialchars($viewingClient['name']); ?>
<span class="badge bg-secondary ms-2"><?php echo htmlspecialchars($viewingClient['client_id']); ?></span>
</h2>
</div>
<div class="card-body">
<p><strong>Status:</strong> <span class="badge bg-<?php echo $viewingClient['status'] === 'active' ? 'success' : 'danger'; ?>"><?php echo htmlspecialchars($viewingClient['status']); ?></span></p>
<hr>
<h4>Credentials</h4>
<table class="table table-sm table-vcenter">
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>Password</th>
<th>Last Modified</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($credentials as $cred): ?>
<tr>
<td><?php echo htmlspecialchars($cred['name']); ?></td>
<td><?php echo htmlspecialchars($cred['username']); ?></td>
<td>
<div class="input-group input-group-sm">
<input type="password" class="form-control form-control-sm" readonly value="<?php echo htmlspecialchars($cred['password']); ?>" id="cred-pass-<?php echo $cred['credential_id']; ?>">
<button class="btn btn-outline-secondary btn-toggle-password" type="button" data-target="#cred-pass-<?php echo $cred['credential_id']; ?>"><i class="bi bi-eye-slash"></i></button>
<button class="btn btn-outline-secondary btn-copy-password" type="button" data-target="#cred-pass-<?php echo $cred['credential_id']; ?>"><i class="bi bi-clipboard"></i></button>
</div>
</td>
<td><?php echo htmlspecialchars(date('Y-m-d H:i', strtotime($cred['updated_at']))); ?></td>
<td>
<a href="edit-credential.php?credential_id=<?php echo $cred['credential_id']; ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil-square"></i> Edit</a>
<a href="delete-credential.php?credential_id=<?php echo $cred['credential_id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this credential?');"><i class="bi bi-trash"></i> Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($credentials)): ?>
<tr>
<td colspan="5" class="text-center text-muted">No credentials found for this client.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<hr>
<h4>Notes</h4>
<div id="notes-section">
<div class="card mb-3">
<div class="card-body">
<form id="add-note-form" action="add-note.php" method="POST">
<input type="hidden" name="client_id" value="<?php echo htmlspecialchars($viewingClient['client_id']); ?>">
<div class="mb-3">
<label for="note" class="form-label">Add a new note</label>
<textarea class="form-control" id="note" name="note" rows="3" required></textarea>
</div>
<button type="submit" class="btn btn-sm btn-primary">Add Note</button>
</form>
</div>
</div>
<div id="notes-list">
<?php foreach ($notes as $note): ?>
<div class="card mb-2">
<div class="card-body p-2">
<p class="card-text mb-1">"<?php echo nl2br(htmlspecialchars($note['note'])); ?>"</p>
<small class="text-muted">
Added by <?php echo htmlspecialchars($note['display_name']); ?> on <?php echo htmlspecialchars(date('Y-m-d H:i', strtotime($note['created_at']))); ?>
</small>
</div>
<div class="card-footer bg-transparent border-top-0 text-end p-1">
<a href="delete-note.php?note_id=<?php echo $note['note_id']; ?>" class="btn btn-sm btn-outline-danger delete-note-btn" data-note-id="<?php echo $note['note_id']; ?>"><i class="bi bi-trash"></i></a>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if (empty($notes)): ?>
<p id="no-notes-message" class="text-center text-muted">No notes found for this client.</p>
<?php else: ?>
<p id="no-notes-message" class="text-center text-muted d-none">No notes found for this client.</p>
<?php endif; ?>
</div>
</div>
</div>
<?php else: ?>
<!-- Client List View -->
<div class="d-flex justify-content-between align-items-center mb-3">
<h1 class="mb-0">Clients</h1>
<a href="add-client.php" class="btn btn-primary"><i class="bi bi-plus-circle"></i> Add New Client</a>
</div>
<?php if (isset($_GET['status']) && $_GET['status'] === 'client_added'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
New client has been added successfully.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="mb-3">
<input type="text" id="client-search" class="form-control" placeholder="Search clients...">
</div>
<div class="card">
<div class="card-body">
<table class="table table-hover">
<thead>
<tr>
<th>Client ID</th>
<th>Name</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="client-list">
<?php foreach ($clients as $client): ?>
<tr>
<td><?php echo htmlspecialchars($client['client_id']); ?></td>
<td><?php echo htmlspecialchars($client['name']); ?></td>
<td><span class="badge bg-<?php echo $client['status'] === 'active' ? 'success' : 'danger'; ?>"><?php echo htmlspecialchars($client['status']); ?></span></td>
<td>
<a href="dashboard.php?client_id=<?php echo htmlspecialchars($client['client_id']); ?>" class="btn btn-sm btn-outline-primary">View</a>
<a href="edit-client.php?client_id=<?php echo htmlspecialchars($client['client_id']); ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
<a href="delete-client.php?client_id=<?php echo htmlspecialchars($client['client_id']); ?>" class="btn btn-sm btn-outline-danger">Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($clients)): ?>
<tr>
<td colspan="4" class="text-center text-muted">No clients found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<?php endif; ?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>