366 lines
20 KiB
PHP
366 lines
20 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'partner') {
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
$current_user_id = $_SESSION['user_id'];
|
|
|
|
// Get partner details from user_id
|
|
$stmt = $pdo->prepare("SELECT * FROM partners WHERE user_id = ?");
|
|
$stmt->execute([$current_user_id]);
|
|
$partner = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$partner) {
|
|
$error_message = "Could not find a partner profile linked to your user account.";
|
|
} else {
|
|
$partner_id = $partner['id'];
|
|
|
|
// -- Performance Summary --
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM residents WHERE partner_id = ?");
|
|
$stmt->execute([$partner_id]);
|
|
$total_assigned = $stmt->fetchColumn();
|
|
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM residents WHERE partner_id = ? AND status = 'Active'");
|
|
$stmt->execute([$partner_id]);
|
|
$active_assigned = $stmt->fetchColumn();
|
|
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM referrals WHERE partner_id = ?");
|
|
$stmt->execute([$partner_id]);
|
|
$total_referrals = $stmt->fetchColumn();
|
|
|
|
// -- Resident Data --
|
|
$resident_sql = "
|
|
SELECT
|
|
r.id, r.first_name, r.last_name, r.program, r.risk_level, r.status,
|
|
(SELECT COUNT(*) FROM action_plans ap WHERE ap.resident_id = r.id AND ap.status != 'Completed') as open_plans_count,
|
|
(SELECT note FROM case_notes WHERE resident_id = r.id ORDER BY created_at DESC LIMIT 1) as last_note,
|
|
(SELECT created_at FROM case_notes WHERE resident_id = r.id ORDER BY created_at DESC LIMIT 1) as last_note_date
|
|
FROM residents r
|
|
WHERE r.partner_id = ?
|
|
ORDER BY r.last_name, r.first_name
|
|
";
|
|
$resident_stmt = $pdo->prepare($resident_sql);
|
|
$resident_stmt->execute([$partner_id]);
|
|
$residents = $resident_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// -- Referral Data --
|
|
$referral_sql = "
|
|
SELECT
|
|
ref.id, ref.referral_date, ref.status, ref.notes,
|
|
res.first_name, res.last_name,
|
|
u.email as staff_email
|
|
FROM referrals ref
|
|
JOIN residents res ON ref.resident_id = res.id
|
|
JOIN users u ON ref.staff_user_id = u.id
|
|
WHERE ref.partner_id = ?
|
|
ORDER BY ref.referral_date DESC
|
|
";
|
|
$referral_stmt = $pdo->prepare($referral_sql);
|
|
$referral_stmt->execute([$partner_id]);
|
|
$referrals = $referral_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
|
// -- Fetch Messages --
|
|
$message_sql = "
|
|
SELECT m.id, m.subject, m.created_at, m.read_at, u.email as sender_email
|
|
FROM messages m
|
|
JOIN users u ON m.sender_user_id = u.id
|
|
WHERE m.recipient_user_id = ?
|
|
ORDER BY m.created_at DESC
|
|
";
|
|
$message_stmt = $pdo->prepare($message_sql);
|
|
$message_stmt->execute([$current_user_id]);
|
|
$messages = $message_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Partner Dashboard - Continuum Nexus</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/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 navbar-dark bg-dark">
|
|
<div class="container-fluid">
|
|
<a class="navbar-brand" href="partner_dashboard.php">Continuum Nexus</a>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
|
<li class="nav-item">
|
|
<a class="nav-link active" href="#">Dashboard</a>
|
|
</li>
|
|
</ul>
|
|
<a href="logout.php" class="btn btn-outline-light">Logout</a>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="container mt-4">
|
|
<?php if (isset($error_message)): ?>
|
|
<div class="alert alert-danger"><?php echo $error_message; ?></div>
|
|
<?php elseif (isset($partner)): ?>
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h2">Partner: <?php echo htmlspecialchars($partner['name']); ?></h1>
|
|
</div>
|
|
|
|
<ul class="nav nav-tabs" id="myTab" role="tablist">
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link active" id="residents-tab" data-bs-toggle="tab" data-bs-target="#residents" type="button" role="tab" aria-controls="residents" aria-selected="true">Assigned Residents</button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="referrals-tab" data-bs-toggle="tab" data-bs-target="#referrals" type="button" role="tab" aria-controls="referrals" aria-selected="false">Referrals</button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="messages-tab" data-bs-toggle="tab" data-bs-target="#messages" type="button" role="tab" aria-controls="messages" aria-selected="false">Messages</button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="documents-tab" data-bs-toggle="tab" data-bs-target="#documents" type="button" role="tab" aria-controls="documents" aria-selected="false">Documents</button>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="tab-content" id="myTabContent">
|
|
<!-- Residents Tab -->
|
|
<div class="tab-pane fade show active" id="residents" role="tabpanel" aria-labelledby="residents-tab">
|
|
<div class="pt-4">
|
|
<!-- Performance Summary -->
|
|
<div class="row mb-4">
|
|
<div class="col-md-4">
|
|
<div class="card text-center h-100">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Total Assigned</h5>
|
|
<p class="card-text fs-4"><?php echo $total_assigned; ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card text-center h-100">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Active Residents</h5>
|
|
<p class="card-text fs-4"><?php echo $active_assigned; ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card text-center h-100">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Total Referrals</h5>
|
|
<p class="card-text fs-4"><?php echo $total_referrals; ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Resident Health Cards -->
|
|
<h3 class="h4 mb-3">Assigned Resident Health Cards</h3>
|
|
<div class="row gy-4">
|
|
<?php if (empty($residents)): ?>
|
|
<div class="col-12">
|
|
<div class="alert alert-info">No residents are currently assigned to you.</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<?php foreach ($residents as $resident): ?>
|
|
<div class="col-md-6 col-lg-4">
|
|
<div class="card h-100 shadow-sm">
|
|
<div class="card-header d-flex justify-content-between align-items-center bg-light">
|
|
<h5 class="mb-0 fs-6"><?php echo htmlspecialchars($resident['first_name'] . ' ' . $resident['last_name']); ?></h5>
|
|
<?php
|
|
$risk_color = 'secondary';
|
|
if ($resident['risk_level'] === 'High') $risk_color = 'danger';
|
|
if ($resident['risk_level'] === 'Medium') $risk_color = 'warning';
|
|
?>
|
|
<span class="badge bg-<?php echo $risk_color; ?>"><?php echo htmlspecialchars($resident['risk_level']); ?></span>
|
|
</div>
|
|
<div class="card-body">
|
|
<p class="card-text mb-2">
|
|
<strong><i class="bi bi-clipboard-check me-2"></i>Program:</strong> <?php echo htmlspecialchars($resident['program']); ?>
|
|
</p>
|
|
<p class="card-text mb-3">
|
|
<strong><i class="bi bi-activity me-2"></i>Status:</strong>
|
|
<?php
|
|
$status_color = 'primary';
|
|
if ($resident['status'] === 'Inactive') $status_color = 'secondary';
|
|
if ($resident['status'] === 'Stabilized') $status_color = 'success';
|
|
?>
|
|
<span class="badge bg-<?php echo $status_color; ?>"><?php echo htmlspecialchars($resident['status']); ?></span>
|
|
</p>
|
|
<div class="d-flex justify-content-between align-items-center border-top pt-2">
|
|
<small class="text-muted"><i class="bi bi-card-checklist me-1"></i> Open Action Plans</small>
|
|
<span class="badge rounded-pill bg-primary-custom"><?php echo $resident['open_plans_count']; ?></span>
|
|
</div>
|
|
<h6 class="card-subtitle mb-2 mt-3 text-muted border-top pt-2"><i class="bi bi-chat-left-text me-2"></i>Last Note</h6>
|
|
<p class="card-text fst-italic small">
|
|
<?php if ($resident['last_note']): ?>
|
|
"<?php echo htmlspecialchars(substr($resident['last_note'], 0, 80)); ?>..."
|
|
<br><small class="text-muted"> on <?php echo date("M j, Y", strtotime($resident['last_note_date'])); ?></small>
|
|
<?php else: ?>
|
|
<span class="text-muted">No notes recorded yet.</span>
|
|
<?php endif; ?>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<!-- Referrals Tab -->
|
|
<div class="tab-pane fade" id="referrals" role="tabpanel" aria-labelledby="referrals-tab">
|
|
<div class="pt-4">
|
|
<h3 class="h4 mb-3">Referral Tracker</h3>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Resident</th>
|
|
<th>Referred By</th>
|
|
<th>Status</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($referrals)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center text-muted">No referrals found.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($referrals as $referral): ?>
|
|
<tr>
|
|
<td><?php echo date("M j, Y", strtotime($referral['referral_date'])); ?></td>
|
|
<td><?php echo htmlspecialchars($referral['first_name'] . ' ' . $referral['last_name']); ?></td>
|
|
<td><?php echo htmlspecialchars($referral['staff_email']); ?></td>
|
|
<td>
|
|
<?php
|
|
$ref_status_color = 'secondary';
|
|
if ($referral['status'] === 'Accepted') $ref_status_color = 'success';
|
|
if ($referral['status'] === 'Rejected') $ref_status_color = 'danger';
|
|
?>
|
|
<span class="badge bg-<?php echo $ref_status_color; ?>"><?php echo htmlspecialchars($referral['status']); ?></span>
|
|
</td>
|
|
<td>
|
|
<?php if ($referral['status'] === 'Pending'): ?>
|
|
<button class="btn btn-success btn-sm btn-action" data-action="Accepted" data-referral-id="<?php echo $referral['id']; ?>">Accept</button>
|
|
<button class="btn btn-danger btn-sm btn-action" data-action="Rejected" data-referral-id="<?php echo $referral['id']; ?>">Reject</button>
|
|
<?php else: ?>
|
|
<button class="btn btn-secondary btn-sm" disabled>Handled</button>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<!-- Messages Tab -->
|
|
<div class="tab-pane fade" id="messages" role="tabpanel" aria-labelledby="messages-tab">
|
|
<div class="pt-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h3 class="h4">Inbox</h3>
|
|
<a href="compose_message.php" class="btn btn-primary-custom"><i class="bi bi-pencil-square me-2"></i>Compose Message</a>
|
|
</div>
|
|
<div class="list-group">
|
|
<?php if (empty($messages)): ?>
|
|
<div class="list-group-item text-center text-muted">You have no messages.</div>
|
|
<?php else: ?>
|
|
<?php foreach ($messages as $message): ?>
|
|
<a href="view_message.php?id=<?php echo $message['id']; ?>" class="list-group-item list-group-item-action <?php echo !$message['read_at'] ? 'fw-bold' : ''; ?>">
|
|
<div class="d-flex w-100 justify-content-between">
|
|
<h6 class="mb-1"><?php echo htmlspecialchars($message['subject']); ?></h6>
|
|
<small><?php echo date("M j, Y", strtotime($message['created_at'])); ?></small>
|
|
</div>
|
|
<p class="mb-1">From: <?php echo htmlspecialchars($message['sender_email']); ?></p>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<!-- Documents Tab -->
|
|
<div class="tab-pane fade" id="documents" role="tabpanel" aria-labelledby="documents-tab">
|
|
<div class="pt-4">
|
|
<h3 class="h4 mb-3">Document Management</h3>
|
|
<div class="list-group">
|
|
<?php if (empty($residents)): ?>
|
|
<div class="list-group-item text-center text-muted">No residents assigned to you.</div>
|
|
<?php else: ?>
|
|
<?php foreach ($residents as $resident): ?>
|
|
<div class="list-group-item d-flex justify-content-between align-items-center">
|
|
<span><?php echo htmlspecialchars($resident['first_name'] . ' ' . $resident['last_name']); ?></span>
|
|
<a href="manage_documents.php?resident_id=<?php echo $resident['id']; ?>" class="btn btn-outline-primary btn-sm">Manage Documents</a>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const actionButtons = document.querySelectorAll('.btn-action');
|
|
|
|
actionButtons.forEach(button => {
|
|
button.addEventListener('click', function () {
|
|
const referralId = this.dataset.referralId;
|
|
const newStatus = this.dataset.action;
|
|
const row = this.closest('tr');
|
|
|
|
if (!confirm(`Are you sure you want to ${newStatus.toLowerCase()} this referral?`)) {
|
|
return;
|
|
}
|
|
|
|
fetch('update_referral_status.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
referral_id: referralId,
|
|
status: newStatus
|
|
}),
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
// Update the UI
|
|
const statusCell = row.querySelector('td:nth-child(4) span');
|
|
statusCell.textContent = newStatus;
|
|
statusCell.className = `badge bg-${newStatus === 'Accepted' ? 'success' : 'danger'}`;
|
|
|
|
const actionCell = row.querySelector('td:nth-child(5)');
|
|
actionCell.innerHTML = '<button class="btn btn-secondary btn-sm" disabled>Handled</button>';
|
|
|
|
// Optional: show a success message
|
|
// alert(data.message);
|
|
} else {
|
|
alert('Error: ' + data.message);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Fetch Error:', error);
|
|
alert('An unexpected error occurred. Please try again.');
|
|
});
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|