Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/messaging/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/messaging/index.php

<?php
require_once '../../config/config.php';
checkLogin();

$pageTitle = 'Messaging & Chats - ' . APP_NAME;
$db = Database::getInstance()->getConnection();
$success = '';
$error = '';

$userId = $_SESSION['user_id'] ?? null;

// Load conversations (public + member_admin types)
$sql = "SELECT c.*, 
               m.first_name, m.last_name, m.email,
               (SELECT message_text FROM chat_messages WHERE conversation_id = c.id ORDER BY id DESC LIMIT 1) AS last_message,
               (SELECT created_at FROM chat_messages WHERE conversation_id = c.id ORDER BY id DESC LIMIT 1) AS last_message_time,
               (SELECT COUNT(*) FROM chat_messages WHERE conversation_id = c.id AND is_read = 0 AND sender_type != 'admin') AS unread_count
        FROM chat_conversations c
        LEFT JOIN members m ON c.member_id = m.id
        WHERE c.type IN ('public', 'member_admin') AND c.is_closed = 0
        ORDER BY (last_message_time IS NULL), last_message_time DESC, c.created_at DESC
        LIMIT 100";

$stmt = $db->prepare($sql);
$stmt->execute();
$conversations = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get total unread count
$unreadStmt = $db->query("SELECT COUNT(*) as cnt FROM chat_messages WHERE is_read = 0 AND sender_type != 'admin'");
$unreadTotal = (int)($unreadStmt->fetch()['cnt'] ?? 0);

include '../../includes/header.php';
include '../../includes/sidebar.php';
?>

<main class="flex-1 md:ml-64 mt-16">
<div class="container mx-auto px-4 py-8">
    <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6 gap-4">
        <div>
            <h1 class="text-3xl font-bold text-gray-800">
                <i class="fas fa-comments mr-2 text-blue-500"></i>Messaging & Chats
            </h1>
            <p class="text-gray-600 mt-2">Manage conversations with members and public visitors</p>
        </div>
        <div class="flex gap-3">
            <a href="broadcasts.php" class="btn-gradient-orange text-white px-6 py-3 rounded-full font-semibold transition shadow-lg hover:shadow-xl">
                <i class="fas fa-bullhorn mr-2"></i>Send Broadcast
            </a>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 rounded-lg mb-4">
            <i class="fas fa-check-circle mr-2"></i><?php echo $success; ?>
        </div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded-lg mb-4">
            <i class="fas fa-exclamation-circle mr-2"></i><?php echo $error; ?>
        </div>
    <?php endif; ?>

    <!-- Stats Cards -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
        <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-blue-500">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-600 mb-1">Total Conversations</p>
                    <p class="text-3xl font-bold text-gray-800"><?php echo count($conversations); ?></p>
                </div>
                <div class="w-16 h-16 rounded-full flex items-center justify-center" style="background: linear-gradient(135deg, #1E40AF 0%, #9333EA 100%);">
                    <i class="fas fa-comments text-white text-2xl"></i>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-orange-500">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-600 mb-1">Unread Messages</p>
                    <p class="text-3xl font-bold text-gray-800"><?php echo $unreadTotal; ?></p>
                </div>
                <div class="w-16 h-16 rounded-full flex items-center justify-center" style="background: linear-gradient(135deg, #F97316 0%, #FBBF24 100%);">
                    <i class="fas fa-envelope text-white text-2xl"></i>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-purple-500">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-600 mb-1">Active Chats</p>
                    <p class="text-3xl font-bold text-gray-800"><?php echo count(array_filter($conversations, fn($c) => ($c['unread_count'] ?? 0) > 0)); ?></p>
                </div>
                <div class="w-16 h-16 rounded-full flex items-center justify-center" style="background: linear-gradient(135deg, #9333EA 0%, #F97316 100%);">
                    <i class="fas fa-user-friends text-white text-2xl"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Conversations List -->
    <div class="bg-white rounded-lg shadow-lg overflow-hidden">
        <div class="px-4 py-4 border-b border-gray-200 bg-gray-50">
            <h2 class="text-lg font-semibold text-gray-800">Active Conversations</h2>
        </div>
        <div class="divide-y divide-gray-200">
            <?php if (empty($conversations)): ?>
                <div class="px-6 py-12 text-center text-gray-500">
                    <i class="fas fa-inbox text-4xl mb-3 text-gray-300"></i>
                    <p>No conversations yet.</p>
                </div>
            <?php else: ?>
                <?php foreach ($conversations as $conv): ?>
                    <div class="px-6 py-4 hover:bg-gray-50 transition cursor-pointer" onclick="window.location='chat.php?id=<?php echo $conv['id']; ?>'">
                        <div class="flex items-center justify-between">
                            <div class="flex items-center space-x-4 flex-1">
                                <div class="w-12 h-12 rounded-full flex items-center justify-center text-white font-bold" style="background: linear-gradient(135deg, #1E40AF 0%, #F97316 100%);">
                                    <?php if ($conv['member_id']): ?>
                                        <?php echo strtoupper(substr($conv['first_name'] ?? 'M', 0, 1)); ?>
                                    <?php else: ?>
                                        <i class="fas fa-user-circle"></i>
                                    <?php endif; ?>
                                </div>
                                <div class="flex-1 min-w-0">
                                    <div class="flex items-center space-x-2">
                                        <p class="text-sm font-semibold text-gray-800 truncate">
                                            <?php if ($conv['member_id']): ?>
                                                <?php echo htmlspecialchars(trim(($conv['first_name'] ?? '') . ' ' . ($conv['last_name'] ?? ''))); ?>
                                            <?php else: ?>
                                                Guest <?php echo substr($conv['guest_token'] ?? '', 0, 8); ?>
                                            <?php endif; ?>
                                        </p>
                                        <?php if (($conv['unread_count'] ?? 0) > 0): ?>
                                            <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-red-500 text-white">
                                                <?php echo $conv['unread_count']; ?>
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                    <p class="text-sm text-gray-600 truncate mt-1">
                                        <?php echo htmlspecialchars(substr($conv['last_message'] ?? 'No messages yet', 0, 80)); ?>
                                    </p>
                                    <p class="text-xs text-gray-400 mt-1">
                                        <?php echo $conv['last_message_time'] ? date('M j, Y g:i A', strtotime($conv['last_message_time'])) : 'Just now'; ?>
                                    </p>
                                </div>
                            </div>
                            <div>
                                <i class="fas fa-chevron-right text-gray-400"></i>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</div>
</main>

<?php include '../../includes/footer.php'; ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists