<?php
/**
 * Admin — Live Chat Management
 *
 * Lists all chat conversations with status, visitor info, and last message.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>

<div class="admin-section">
    <!-- Stats Row -->
    <div class="admin-stats-row" style="display:flex; gap:16px; margin-bottom:24px; flex-wrap:wrap;">
        <div class="admin-stat-card" style="flex:1; min-width:200px; background:linear-gradient(135deg,#4A6CF7,#6C3FE8); color:#fff; border-radius:12px; padding:20px;">
            <div style="font-size:13px; opacity:0.85; margin-bottom:4px;">Active Conversations</div>
            <div style="font-size:28px; font-weight:700;"><?= (int) $activeCount ?></div>
        </div>
        <div class="admin-stat-card" style="flex:1; min-width:200px; background:linear-gradient(135deg,#FF4757,#FF6B81); color:#fff; border-radius:12px; padding:20px;">
            <div style="font-size:13px; opacity:0.85; margin-bottom:4px;">Unread Messages</div>
            <div style="font-size:28px; font-weight:700;"><?= (int) $totalUnread ?></div>
        </div>
        <div class="admin-stat-card" style="flex:1; min-width:200px; background:linear-gradient(135deg,#2ED573,#7BED9F); color:#fff; border-radius:12px; padding:20px;">
            <div style="font-size:13px; opacity:0.85; margin-bottom:4px;">Total Conversations</div>
            <div style="font-size:28px; font-weight:700;"><?= count($conversations) ?></div>
        </div>
    </div>

    <!-- Conversations Table -->
    <?php if (empty($conversations)): ?>
    <div class="admin-empty-state" style="text-align:center; padding:60px 20px; color:#999;">
        <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#ccc" stroke-width="1.5" style="margin-bottom:12px;">
            <path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/>
        </svg>
        <p style="font-size:16px; margin:0;">No chat conversations yet.</p>
        <p style="font-size:13px; margin:4px 0 0;">Conversations will appear here when visitors start chatting.</p>
    </div>
    <?php else: ?>
    <div class="admin-table-wrap" style="overflow-x:auto;">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Visitor</th>
                    <th>Status</th>
                    <th>Messages</th>
                    <th>Unread</th>
                    <th>Last Message</th>
                    <th>Started</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($conversations as $conv): ?>
                <tr class="<?= $conv['admin_unread'] > 0 ? 'admin-row-unread' : '' ?>">
                    <td>
                        <div style="font-weight:600;">
                            <?= htmlspecialchars($conv['visitor_name'] ?? 'Anonymous', ENT_QUOTES, 'UTF-8') ?>
                        </div>
                        <?php if ($conv['visitor_email']): ?>
                        <div style="font-size:12px; color:#888;"><?= htmlspecialchars($conv['visitor_email'], ENT_QUOTES, 'UTF-8') ?></div>
                        <?php endif; ?>
                        <?php if ($conv['visitor_phone']): ?>
                        <div style="font-size:12px; color:#888;"><?= htmlspecialchars($conv['visitor_phone'], ENT_QUOTES, 'UTF-8') ?></div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php
                        $statusColors = ['pending' => '#FFA502', 'active' => '#2ED573', 'closed' => '#999'];
                        $statusColor = $statusColors[$conv['status']] ?? '#999';
                        ?>
                        <span style="display:inline-block; padding:3px 10px; border-radius:20px; font-size:12px; font-weight:600; color:#fff; background:<?= $statusColor ?>;">
                            <?= ucfirst($conv['status']) ?>
                        </span>
                    </td>
                    <td><?= (int) ($conv['message_count'] ?? 0) ?></td>
                    <td>
                        <?php if ($conv['admin_unread'] > 0): ?>
                        <span style="display:inline-flex; align-items:center; justify-content:center; min-width:22px; height:22px; border-radius:11px; background:#FF4757; color:#fff; font-size:12px; font-weight:700; padding:0 6px;">
                            <?= (int) $conv['admin_unread'] ?>
                        </span>
                        <?php else: ?>
                        <span style="color:#ccc;">—</span>
                        <?php endif; ?>
                    </td>
                    <td style="max-width:200px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; color:#666; font-size:13px;">
                        <?= htmlspecialchars(mb_substr($conv['last_message'] ?? '—', 0, 60), ENT_QUOTES, 'UTF-8') ?>
                    </td>
                    <td style="font-size:13px; color:#888;">
                        <?= date('M j, g:i A', strtotime($conv['created_at'])) ?>
                    </td>
                    <td>
                        <div style="display:flex; gap:6px; flex-wrap:wrap;">
                            <a href="<?= $bp ?>/admin/chat/<?= (int) $conv['id'] ?>"
                               class="admin-btn admin-btn-sm" style="background:#4A6CF7; color:#fff; text-decoration:none; padding:5px 12px; border-radius:6px; font-size:12px;">
                                Open Chat
                            </a>
                            <?php if ($conv['status'] !== 'closed'): ?>
                            <form method="POST" action="<?= $bp ?>/admin/chat/<?= (int) $conv['id'] ?>/close" style="display:inline;" onsubmit="return confirm('Close this conversation?');">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="admin-btn admin-btn-sm" style="background:#FFA502; color:#fff; padding:5px 12px; border-radius:6px; font-size:12px; border:none; cursor:pointer;">Close</button>
                            </form>
                            <?php endif; ?>
                            <form method="POST" action="<?= $bp ?>/admin/chat/<?= (int) $conv['id'] ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this conversation permanently?');">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" style="padding:5px 12px; border-radius:6px; font-size:12px; border:none; cursor:pointer;">Delete</button>
                            </form>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

<style>
    .admin-row-unread {
        background: #FFF8E1 !important;
        font-weight: 500;
    }
    .admin-row-unread td {
        border-bottom-color: #FFE082;
    }
</style>
