<?php
// gmail_inbox.php
$inboxFile = 'inbox/all_emails.json';
$emails = [];

if (file_exists($inboxFile)) {
    $emails = json_decode(file_get_contents($inboxFile), true);
    if (!is_array($emails)) $emails = [];
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gmail Buatan - Inbox</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background: #f4f6f9;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .inbox-container {
            max-width: 1200px;
            margin: 20px auto;
        }
        .email-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            margin-bottom: 15px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        .email-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 4px 15px rgba(0,0,0,0.15);
        }
        .email-card.unread {
            border-left: 4px solid #1a73e8;
            background: #f8f9ff;
        }
        .email-subject {
            font-weight: 600;
            color: #202124;
        }
        .email-from {
            color: #5f6368;
            font-size: 0.9rem;
        }
        .email-date {
            color: #9aa0a6;
            font-size: 0.8rem;
            text-align: right;
        }
        .modal-body {
            white-space: pre-wrap;
            line-height: 1.6;
        }
        .header {
            background: white;
            padding: 20px;
            border-radius: 12px;
            margin-bottom: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .btn-refresh {
            background: #1a73e8;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
        }
    </style>
</head>
<body>
    <div class="inbox-container">
        <div class="header d-flex justify-content-between align-items-center">
            <h2><i class="fas fa-inbox me-2"></i>Gmail Buatan - Inbox</h2>
            <button class="btn-refresh" onclick="location.reload()">
                <i class="fas fa-sync-alt me-2"></i>Refresh
            </button>
        </div>
        
        <div class="stats mb-3">
            <span class="badge bg-primary">Total: <?= count($emails) ?></span>
            <span class="badge bg-warning ms-2">Belum dibaca: <?= count(array_filter($emails, fn($e) => !$e['read'])) ?></span>
        </div>
        
        <?php if (empty($emails)): ?>
            <div class="alert alert-info text-center">
                <i class="fas fa-envelope-open-text fa-3x mb-3"></i>
                <h5>Belum ada email</h5>
                <p>Email yang dikirim dari panel akan muncul di sini</p>
            </div>
        <?php else: ?>
            <?php foreach ($emails as $index => $email): ?>
                <div class="email-card p-3 <?= $email['read'] ? '' : 'unread' ?>" onclick="openEmail(<?= $index ?>)">
                    <div class="row align-items-center">
                        <div class="col-md-1 text-center">
                            <i class="fas <?= $email['read'] ? 'fa-envelope-open' : 'fa-envelope' ?> fa-lg" style="color: #1a73e8;"></i>
                        </div>
                        <div class="col-md-3">
                            <div class="email-from">
                                <i class="fas fa-user me-1"></i>
                                <?= htmlspecialchars($email['from'] ?? 'Unknown') ?>
                            </div>
                            <small class="text-muted"><?= htmlspecialchars($email['to'] ?? '-') ?></small>
                        </div>
                        <div class="col-md-5">
                            <div class="email-subject">
                                <?= htmlspecialchars($email['subject'] ?? '(tanpa subjek)') ?>
                            </div>
                            <small class="text-muted">
                                <?= htmlspecialchars(substr($email['message'] ?? '', 0, 60)) ?>...
                            </small>
                        </div>
                        <div class="col-md-3">
                            <div class="email-date">
                                <i class="far fa-clock me-1"></i>
                                <?= date('d/m/Y H:i', strtotime($email['date'])) ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
    
    <!-- Modal -->
    <div class="modal fade" id="emailModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modalSubject"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div id="modalFrom" class="mb-3"></div>
                    <div id="modalDate" class="mb-3 text-muted"></div>
                    <hr>
                    <div id="modalMessage" class="mt-3"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        const emails = <?= json_encode($emails) ?>;
        
        function openEmail(index) {
            const email = emails[index];
            if (!email) return;
            
            // Mark as read
            if (!email.read) {
                fetch('mark_inbox_read.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ index: index })
                });
                email.read = true;
                const card = document.querySelectorAll('.email-card')[index];
                if (card) card.classList.remove('unread');
            }
            
            document.getElementById('modalSubject').innerHTML = '<i class="fas fa-tag me-2"></i>' + escapeHtml(email.subject || '(tanpa subjek)');
            document.getElementById('modalFrom').innerHTML = '<i class="fas fa-user me-2"></i><strong>Dari:</strong> ' + escapeHtml(email.from || 'Unknown');
            document.getElementById('modalDate').innerHTML = '<i class="far fa-calendar-alt me-2"></i>' + email.date;
            document.getElementById('modalMessage').innerHTML = escapeHtml(email.message || '').replace(/\n/g, '<br>');
            
            new bootstrap.Modal(document.getElementById('emailModal')).show();
        }
        
        function escapeHtml(str) {
            if (!str) return '';
            return str.replace(/[&<>]/g, function(m) {
                if (m === '&') return '&amp;';
                if (m === '<') return '&lt;';
                if (m === '>') return '&gt;';
                return m;
            });
        }
    </script>
</body>
</html>