38808-vm/user_dashboard.php
2026-02-27 18:38:01 +00:00

306 lines
15 KiB
PHP

<?php
require_once __DIR__ . '/includes/header.php';
// Check if user has view permission
if (!canView()) {
// If they can't even view, they shouldn't be here, but header.php already handles basic login.
// We'll let them see their profile at least, but maybe not this dashboard.
}
$user_id = $_SESSION['user_id'];
$user_role = $_SESSION['user_role'];
$is_admin = isAdmin();
$is_clerk = ($user_role === 'clerk');
// Stats for this specific user
$stmt = db()->prepare("SELECT COUNT(*) FROM mailbox WHERE assigned_to = ?");
$stmt->execute([$user_id]);
$my_total_assignments = $stmt->fetchColumn();
$stmt = db()->prepare("SELECT COUNT(*) FROM mailbox WHERE assigned_to = ? AND status != 'closed'");
$stmt->execute([$user_id]);
$my_pending_tasks = $stmt->fetchColumn();
// Global Stats (for Clerks or if we want to show them)
$total_inbound = db()->query("SELECT COUNT(*) FROM mailbox WHERE type = 'inbound'")->fetchColumn();
$total_outbound = db()->query("SELECT COUNT(*) FROM mailbox WHERE type = 'outbound'")->fetchColumn();
// Fetch statuses for badge and count
$statuses_data = db()->query("SELECT * FROM mailbox_statuses")->fetchAll(PDO::FETCH_UNIQUE);
// My Assignments
$my_assignments = db()->prepare("SELECT m.*, s.name as status_name, s.color as status_color
FROM mailbox m
LEFT JOIN mailbox_statuses s ON m.status_id = s.id
WHERE m.assigned_to = ?
ORDER BY m.created_at DESC LIMIT 10");
$my_assignments->execute([$user_id]);
$my_assignments = $my_assignments->fetchAll();
// Recent Activity
$recent_query = "SELECT m.*, s.name as status_name, s.color as status_color, u.full_name as assigned_to_name
FROM mailbox m
LEFT JOIN mailbox_statuses s ON m.status_id = s.id
LEFT JOIN users u ON m.assigned_to = u.id";
if ($is_admin || $is_clerk) {
// Admins and Clerks see all recent activity if they have view permission
$recent_stmt = db()->prepare($recent_query . " ORDER BY m.updated_at DESC LIMIT 10");
$recent_stmt->execute();
} else {
// Staff see only theirs
$recent_stmt = db()->prepare($recent_query . " WHERE m.assigned_to = ? OR m.created_by = ? ORDER BY m.updated_at DESC LIMIT 10");
$recent_stmt->execute([$user_id, $user_id]);
}
$recent_activity = $recent_stmt->fetchAll();
function getStatusBadge($mail) {
$status_name = $mail['status_name'] ?? 'غير معروف';
$status_color = $mail['status_color'] ?? '#6c757d';
$display_name = $status_name;
if ($status_name == 'received') $display_name = 'تم الاستلام';
if ($status_name == 'in_progress') $display_name = 'قيد المعالجة';
if ($status_name == 'closed') $display_name = 'مكتمل';
return '<span class="badge" style="background-color: ' . $status_color . ';">' . htmlspecialchars($display_name) . '</span>';
}
?>
<div class="row mb-4">
<div class="col-md-12">
<div class="card bg-dark text-white p-4 shadow-lg border-0 overflow-hidden position-relative">
<div class="position-absolute end-0 top-0 p-3 opacity-10">
<i class="fas fa-envelope-open-text fa-10x" style="transform: rotate(-15deg);"></i>
</div>
<div class="d-flex justify-content-between align-items-center position-relative">
<div>
<h2 class="fw-bold mb-1">مرحباً، <?= htmlspecialchars($current_user['full_name'] ?? $_SESSION['username']) ?>!</h2>
<p class="mb-0 opacity-75">
أنت مسجل كـ <strong>
<?php
if ($is_admin) echo 'مدير النظام';
elseif ($is_clerk) echo 'كاتب';
else echo 'موظف';
?>
</strong>.
<?php if ($is_admin || $is_clerk): ?>
يمكنك متابعة كافة المراسلات وإدارة المهام.
<?php else: ?>
تابع مهامك المسندة إليك هنا.
<?php endif; ?>
</p>
</div>
<div class="d-none d-md-block">
<?php if ($current_user['profile_image']): ?>
<img src="<?= $current_user['profile_image'] ?>?v=<?= time() ?>" alt="Profile" class="rounded-circle border border-3 border-white shadow" style="width: 100px; height: 100px; object-fit: cover;">
<?php else: ?>
<div class="bg-white bg-opacity-25 rounded-circle d-flex align-items-center justify-content-center border border-3 border-white shadow" style="width: 100px; height: 100px;">
<i class="fas fa-user fa-3x text-white"></i>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<div class="row g-4 mb-4">
<!-- Stats for everyone -->
<div class="col-md-3">
<div class="card h-100 p-3 shadow-sm border-0 border-start border-primary border-4">
<div class="d-flex align-items-center">
<div class="bg-primary bg-opacity-10 p-3 rounded-3 me-3">
<i class="fas fa-tasks text-primary fs-4"></i>
</div>
<div>
<h6 class="text-muted mb-1">مهامي</h6>
<h3 class="fw-bold mb-0"><?= $my_total_assignments ?></h3>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card h-100 p-3 shadow-sm border-0 border-start border-warning border-4">
<div class="d-flex align-items-center">
<div class="bg-warning bg-opacity-10 p-3 rounded-3 me-3">
<i class="fas fa-clock text-warning fs-4"></i>
</div>
<div>
<h6 class="text-muted mb-1">قيد التنفيذ</h6>
<h3 class="fw-bold mb-0"><?= $my_pending_tasks ?></h3>
</div>
</div>
</div>
</div>
<?php if ($is_admin || $is_clerk): ?>
<!-- Admin/Clerk specific stats -->
<div class="col-md-3">
<div class="card h-100 p-3 shadow-sm border-0 border-start border-info border-4">
<div class="d-flex align-items-center">
<div class="bg-info bg-opacity-10 p-3 rounded-3 me-3">
<i class="fas fa-download text-info fs-4"></i>
</div>
<div>
<h6 class="text-muted mb-1">إجمالي الوارد</h6>
<h3 class="fw-bold mb-0"><?= $total_inbound ?></h3>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card h-100 p-3 shadow-sm border-0 border-start border-success border-4">
<div class="d-flex align-items-center">
<div class="bg-success bg-opacity-10 p-3 rounded-3 me-3">
<i class="fas fa-upload text-success fs-4"></i>
</div>
<div>
<h6 class="text-muted mb-1">إجمالي الصادر</h6>
<h3 class="fw-bold mb-0"><?= $total_outbound ?></h3>
</div>
</div>
</div>
</div>
<?php else: ?>
<!-- Staff specific stats -->
<div class="col-md-3">
<div class="card h-100 p-3 shadow-sm border-0 border-start border-info border-4">
<div class="d-flex align-items-center">
<div class="bg-info bg-opacity-10 p-3 rounded-3 me-3">
<i class="fas fa-envelope-open text-info fs-4"></i>
</div>
<div>
<h6 class="text-muted mb-1">وارد من قبلي</h6>
<?php
$stmt = db()->prepare("SELECT COUNT(*) FROM mailbox WHERE created_by = ? AND type = 'inbound'");
$stmt->execute([$user_id]);
$my_in_count = $stmt->fetchColumn();
?>
<h3 class="fw-bold mb-0"><?= $my_in_count ?></h3>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card h-100 p-3 shadow-sm border-0 border-start border-success border-4">
<div class="d-flex align-items-center">
<div class="bg-success bg-opacity-10 p-3 rounded-3 me-3">
<i class="fas fa-paper-plane text-success fs-4"></i>
</div>
<div>
<h6 class="text-muted mb-1">صادر من قبلي</h6>
<?php
$stmt = db()->prepare("SELECT COUNT(*) FROM mailbox WHERE created_by = ? AND type = 'outbound'");
$stmt->execute([$user_id]);
$my_out_count = $stmt->fetchColumn();
?>
<h3 class="fw-bold mb-0"><?= $my_out_count ?></h3>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
<div class="row">
<!-- Assignments Table -->
<div class="col-lg-8">
<div class="card shadow-sm border-0 mb-4 h-100">
<div class="card-header bg-white py-3 border-bottom d-flex justify-content-between align-items-center">
<h5 class="mb-0 fw-bold"><i class="fas fa-clipboard-list me-2 text-primary"></i> مهامي المسندة</h5>
<div class="btn-group">
<?php if (canAdd()): ?>
<a href="inbound.php?action=add" class="btn btn-sm btn-outline-primary">إضافة وارد</a>
<a href="outbound.php" class="btn btn-sm btn-outline-success">إضافة صادر</a>
<?php endif; ?>
</div>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr>
<th class="ps-4">رقم القيد</th>
<th>الموضوع</th>
<th>الموعد النهائي</th>
<th>الحالة</th>
<th class="pe-4 text-center">الإجراء</th>
</tr>
</thead>
<tbody>
<?php if ($my_assignments): ?>
<?php foreach ($my_assignments as $mail): ?>
<tr style="cursor: pointer;" onclick="window.location='view_mail.php?id=<?= $mail['id'] ?>'">
<td class="ps-4 fw-bold text-primary"><?= $mail['ref_no'] ?></td>
<td><?= htmlspecialchars($mail['subject']) ?></td>
<td>
<?php if ($mail['due_date']): ?>
<small class="<?= (strtotime($mail['due_date']) < time() && $mail['status_name'] != 'closed') ? 'text-danger fw-bold' : 'text-muted' ?>">
<?= $mail['due_date'] ?>
</small>
<?php else: ?>
<small class="text-muted">-</small>
<?php endif; ?>
</td>
<td><?= getStatusBadge($mail) ?></td>
<td class="pe-4 text-center">
<a href="view_mail.php?id=<?= $mail['id'] ?>" class="btn btn-sm btn-light rounded-pill px-3">عرض</a>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="5" class="text-center py-5 text-muted">
<i class="fas fa-check-double fa-3x mb-3 d-block text-success opacity-25"></i>
أنت على اطلاع بكافة مهامك! لا توجد مهام معلقة.
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Recent Activity Sidebar -->
<div class="col-lg-4">
<div class="card shadow-sm border-0 mb-4 h-100">
<div class="card-header bg-white py-3 border-bottom">
<h5 class="mb-0 fw-bold"><i class="fas fa-bell me-2 text-warning"></i> <?= ($is_admin || $is_clerk) ? 'آخر المراسلات' : 'نشاطاتي الأخيرة' ?></h5>
</div>
<div class="card-body p-0" style="max-height: 500px; overflow-y: auto;">
<div class="list-group list-group-flush">
<?php if ($recent_activity): ?>
<?php foreach ($recent_activity as $act): ?>
<a href="view_mail.php?id=<?= $act['id'] ?>" class="list-group-item list-group-item-action p-3 border-0 border-bottom">
<div class="d-flex w-100 justify-content-between mb-1">
<h6 class="mb-1 fw-bold text-truncate" title="<?= htmlspecialchars($act['subject']) ?>"><?= htmlspecialchars($act['subject']) ?></h6>
<small class="text-muted"><?= date('m-d', strtotime($act['updated_at'])) ?></small>
</div>
<div class="d-flex justify-content-between align-items-center">
<small class="text-muted">
<i class="fas <?= $act['type'] == 'inbound' ? 'fa-arrow-down text-primary' : 'fa-arrow-up text-success' ?> me-1"></i>
<?= $act['ref_no'] ?>
</small>
<?= getStatusBadge($act) ?>
</div>
</a>
<?php endforeach; ?>
<?php else: ?>
<div class="text-center py-5 text-muted">
لا يوجد نشاط مسجل
</div>
<?php endif; ?>
</div>
</div>
<div class="card-footer bg-light text-center py-2">
<a href="inbound.php" class="small text-decoration-none">عرض كافة المراسلات <i class="fas fa-chevron-left ms-1"></i></a>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>