<?php
/**
 * Admin Dashboard - View Submissions
 * Displays all newsletters, feedback, and contact messages
 */

session_start();

// Check if admin (you can customize this authentication)
$isAdmin = isset($_GET['admin_key']) && $_GET['admin_key'] === 'emporio123';

// Load database helper
require_once __DIR__ . '/config/database_helper.php';

// Get data
$newsletters = DatabaseHelper::loadJson('newsletter_subscribers.json');
$feedback = DatabaseHelper::loadJson('customer_feedback.json');
$messages = DatabaseHelper::loadJson('contact_messages.json');

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Submissions</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        .admin-dashboard {
            max-width: 1400px;
            margin: 0 auto;
            padding: 40px 20px;
        }
        .dashboard-header {
            background: linear-gradient(135deg, #F07E2D 0%, #e06815 100%);
            color: white;
            padding: 30px;
            border-radius: 8px;
            margin-bottom: 40px;
        }
        .dashboard-header h1 {
            margin: 0 0 10px 0;
            font-size: 32px;
        }
        .dashboard-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        .stat-card {
            background: white;
            padding: 24px;
            border-radius: 8px;
            border-left: 4px solid #F07E2D;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .stat-card h3 {
            margin: 0 0 10px 0;
            color: #333;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        .stat-number {
            font-size: 36px;
            font-weight: 700;
            color: #F07E2D;
            margin: 0;
        }
        .submissions-section {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        .submissions-section h2 {
            margin: 0 0 20px 0;
            color: #333;
            border-bottom: 2px solid #F07E2D;
            padding-bottom: 15px;
        }
        .submission-item {
            padding: 15px;
            border-bottom: 1px solid #eee;
            margin-bottom: 15px;
        }
        .submission-item:last-child {
            border-bottom: none;
            margin-bottom: 0;
        }
        .submission-item p {
            margin: 5px 0;
            color: #666;
            font-size: 14px;
        }
        .submission-label {
            font-weight: 600;
            color: #333;
            display: inline-block;
            min-width: 120px;
        }
        .rating {
            color: #F07E2D;
            font-size: 18px;
        }
        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            background: #F07E2D;
            color: white;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }
        .empty-message {
            text-align: center;
            color: #999;
            padding: 40px 20px;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="admin-dashboard">
        <div class="dashboard-header">
            <h1>📊 Admin Dashboard</h1>
            <p>View all newsletter subscriptions, customer feedback, and contact messages</p>
        </div>

        <!-- Stats Cards -->
        <div class="dashboard-grid">
            <div class="stat-card">
                <h3>📧 Newsletter Subscribers</h3>
                <p class="stat-number"><?php echo count($newsletters); ?></p>
            </div>
            <div class="stat-card">
                <h3>⭐ Customer Feedback</h3>
                <p class="stat-number"><?php echo count($feedback); ?></p>
            </div>
            <div class="stat-card">
                <h3>💬 Contact Messages</h3>
                <p class="stat-number"><?php echo count($messages); ?></p>
            </div>
        </div>

        <!-- Newsletter Section -->
        <div class="submissions-section">
            <h2>📧 Newsletter Subscribers (<?php echo count($newsletters); ?>)</h2>
            <?php if (empty($newsletters)): ?>
                <div class="empty-message">No subscribers yet</div>
            <?php else: ?>
                <?php foreach (array_reverse($newsletters) as $sub): ?>
                <div class="submission-item">
                    <p><span class="submission-label">Email:</span> <?php echo htmlspecialchars($sub['email']); ?></p>
                    <p><span class="submission-label">Subscribed:</span> <?php echo htmlspecialchars($sub['subscribed_date']); ?></p>
                    <p><span class="submission-label">IP:</span> <small><?php echo htmlspecialchars($sub['ip_address']); ?></small></p>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <!-- Feedback Section -->
        <div class="submissions-section">
            <h2>⭐ Customer Feedback (<?php echo count($feedback); ?>)</h2>
            <?php if (empty($feedback)): ?>
                <div class="empty-message">No feedback yet</div>
            <?php else: ?>
                <?php foreach (array_reverse($feedback) as $item): ?>
                <div class="submission-item">
                    <p>
                        <span class="submission-label">Rating:</span>
                        <span class="rating"><?php echo str_repeat('★', $item['rating']); ?></span>
                    </p>
                    <p><span class="submission-label">Name:</span> <?php echo htmlspecialchars($item['name']); ?></p>
                    <p><span class="submission-label">Email:</span> <?php echo htmlspecialchars($item['email']); ?></p>
                    <p><span class="submission-label">Feedback:</span></p>
                    <p style="margin-left: 120px; color: #333; font-weight: 500;">
                        "<?php echo htmlspecialchars($item['feedback']); ?>"
                    </p>
                    <p><span class="submission-label">Date:</span> <?php echo htmlspecialchars($item['submitted_date']); ?></p>
                    <p><span class="submission-label">Status:</span> <span class="status-badge"><?php echo htmlspecialchars($item['status']); ?></span></p>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <!-- Contact Messages Section -->
        <div class="submissions-section">
            <h2>💬 Contact Messages (<?php echo count($messages); ?>)</h2>
            <?php if (empty($messages)): ?>
                <div class="empty-message">No messages yet</div>
            <?php else: ?>
                <?php foreach (array_reverse($messages) as $msg): ?>
                <div class="submission-item">
                    <p><span class="submission-label">Subject:</span> <strong><?php echo htmlspecialchars($msg['subject']); ?></strong></p>
                    <p><span class="submission-label">From:</span> <?php echo htmlspecialchars($msg['name']); ?> (<?php echo htmlspecialchars($msg['email']); ?>)</p>
                    <p><span class="submission-label">Message:</span></p>
                    <p style="margin-left: 120px; color: #333; font-weight: 500; white-space: pre-wrap;">
                        <?php echo htmlspecialchars($msg['message']); ?>
                    </p>
                    <p><span class="submission-label">Date:</span> <?php echo htmlspecialchars($msg['submitted_date']); ?></p>
                    <p><span class="submission-label">Status:</span> <span class="status-badge"><?php echo htmlspecialchars($msg['status']); ?></span></p>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <div style="text-align: center; margin-top: 40px; color: #999; font-size: 12px;">
            <p>Last refreshed: <?php echo date('Y-m-d H:i:s'); ?></p>
            <p>Data stored in: /data/ directory</p>
        </div>
    </div>
</body>
</html>
