34660-vm/dashboard.php
Flatlogic Bot 758f3e1226 v01.5
2025-10-04 17:40:17 +00:00

136 lines
5.3 KiB
PHP

<?php
require_once 'db/config.php';
try {
$pdo = db();
$reports = $pdo->query("SELECT * FROM expense_reports ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
function getStatusBadgeClass($status) {
switch ($status) {
case 'Approved': return 'badge-approved';
case 'Rejected': return 'badge-rejected';
case 'Pending':
default:
return 'badge-pending';
}
}
$page_title = 'Manager Dashboard';
require_once 'includes/header.php';
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>Expense Reports</h1>
<a href="submit_expense.php" class="btn btn-primary">Submit New Expense</a>
</div>
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="table align-middle">
<thead>
<tr>
<th>Agent Name</th>
<th>Amount</th>
<th>Date</th>
<th>Status</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($reports)):
<tr>
<td colspan="6" class="text-center text-muted pt-4 pb-4">No expense reports submitted yet.</td>
</tr>
<?php else:
foreach ($reports as $report):
?>
<tr>
<td>
<a href="my_reports.php?agent=<?php echo urlencode($report['agent_name']); ?>" class="text-decoration-none">
<?php echo htmlspecialchars($report['agent_name']); ?>
</a>
</td>
<td>$<?php echo number_format($report['amount'], 2); ?></td>
<td><?php echo date('M d, Y', strtotime($report['created_at'])); ?></td>
<td>
<span class="badge-status <?php echo getStatusBadgeClass($report['status']); ?>">
<?php echo htmlspecialchars($report['status']); ?>
</span>
</td>
<td><?php echo htmlspecialchars($report['description']); ?></td>
<td>
<div class="btn-group" role="group">
<button class="btn btn-sm btn-success btn-action" data-id="<?php echo $report['id']; ?>" data-status="Approved">Approve</button>
<button class="btn btn-sm btn-danger btn-action" data-id="<?php echo $report['id']; ?>" data-status="Rejected">Reject</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const actionButtons = document.querySelectorAll('.btn-action');
actionButtons.forEach(button => {
button.addEventListener('click', function() {
const reportId = this.dataset.id;
const newStatus = this.dataset.status;
const row = this.closest('tr');
if (!confirm(`Are you sure you want to ${newStatus.toLowerCase()} this report?`)) {
return;
}
const formData = new FormData();
formData.append('id', reportId);
formData.append('status', newStatus);
fetch('api/update_status.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
const statusCell = row.querySelector('.badge-status');
if (statusCell) {
statusCell.textContent = newStatus;
statusCell.className = 'badge-status'; // Reset classes
if (newStatus === 'Approved') {
statusCell.classList.add('badge-approved');
} else if (newStatus === 'Rejected') {
statusCell.classList.add('badge-rejected');
} else {
statusCell.classList.add('badge-pending');
}
}
// Disable buttons after action
row.querySelectorAll('.btn-action').forEach(btn => {
btn.disabled = true;
});
} else {
alert('Error: ' + data.message);
}
})
.catch(error => {
console.error('Fetch error:', error);
alert('An unexpected error occurred. Please try again.');
});
});
});
});
</script>
<?php
require_once 'includes/footer.php';
?>