<?php
/**
 * Modern Admin Dashboard
 */

session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/includes/auth.php';

checkAdminAuth();

$pdo = getDBConnection();

// Get comprehensive statistics
$stats = [
    'contacts' => [
        'total' => $pdo->query("SELECT COUNT(*) FROM contact_submissions")->fetchColumn(),
        'new' => $pdo->query("SELECT COUNT(*) FROM contact_submissions WHERE status = 'new'")->fetchColumn(),
        'change' => '+12%'
    ],
    'gallery' => [
        'total' => $pdo->query("SELECT COUNT(*) FROM gallery_images WHERE status = 'active'")->fetchColumn(),
        'new' => $pdo->query("SELECT COUNT(*) FROM gallery_images WHERE DATE(created_at) = CURDATE()")->fetchColumn(),
        'change' => '+5'
    ],
    'projects' => [
        'total' => $pdo->query("SELECT COUNT(*) FROM projects WHERE status = 'published'")->fetchColumn(),
        'new' => $pdo->query("SELECT COUNT(*) FROM projects WHERE DATE(created_at) = CURDATE()")->fetchColumn(),
        'change' => '+2'
    ],
    'newsletter' => [
        'total' => $pdo->query("SELECT COUNT(*) FROM newsletter_subscribers WHERE status = 'active'")->fetchColumn(),
        'new' => $pdo->query("SELECT COUNT(*) FROM newsletter_subscribers WHERE DATE(subscribed_at) = CURDATE()")->fetchColumn(),
        'change' => '+8'
    ]
];

// Get recent contacts
$recentContacts = $pdo->query("
    SELECT id, name, email, service, status, created_at
    FROM contact_submissions
    ORDER BY created_at DESC
    LIMIT 5
")->fetchAll();

// Get recent activity
$recentProjects = $pdo->query("
    SELECT id, title, status, created_at
    FROM projects
    ORDER BY created_at DESC
    LIMIT 5
")->fetchAll();

$pageTitle = 'Dashboard';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - Admin Panel</title>
    <link rel="stylesheet" href="assets/admin-modern.css">
</head>
<body>
    <?php 
    $pdo = getDBConnection(); // For sidebar badge counts
    include 'includes/sidebar.php'; 
    ?>
    
    <div class="admin-main">
        <?php include 'includes/header-new.php'; ?>
        
        <div class="admin-container">
            <!-- Welcome Section -->
            <div class="card slide-up" style="background: linear-gradient(135deg, #FF6B35 0%, #00B4D8 100%); color: white; margin-bottom: 2rem;">
                <div style="display: flex; align-items: center; justify-content: space-between;">
                    <div>
                        <h2 style="margin: 0 0 0.5rem 0; font-size: 1.75rem;">Welcome back, <?php echo htmlspecialchars($_SESSION['admin_name'] ?? 'Admin'); ?>! 👋</h2>
                        <p style="margin: 0; opacity: 0.9;">Here's what's happening with your website today.</p>
                    </div>
                    <div style="font-size: 3rem;">📊</div>
                </div>
            </div>
            
            <!-- Stats Grid -->
            <div class="stats-grid">
                <div class="stat-card fade-in" style="animation-delay: 0.1s">
                    <div class="stat-header">
                        <div class="stat-icon">📧</div>
                    </div>
                    <div class="stat-value"><?php echo $stats['contacts']['total']; ?></div>
                    <div class="stat-label">Total Contacts</div>
                    <div class="stat-change positive">
                        <span><?php echo $stats['contacts']['new']; ?> new today</span>
                    </div>
                </div>
                
                <div class="stat-card fade-in" style="animation-delay: 0.2s">
                    <div class="stat-header">
                        <div class="stat-icon">🖼️</div>
                    </div>
                    <div class="stat-value"><?php echo $stats['gallery']['total']; ?></div>
                    <div class="stat-label">Gallery Images</div>
                    <div class="stat-change positive">
                        <span><?php echo $stats['gallery']['new']; ?> added today</span>
                    </div>
                </div>
                
                <div class="stat-card fade-in" style="animation-delay: 0.3s">
                    <div class="stat-header">
                        <div class="stat-icon">📁</div>
                    </div>
                    <div class="stat-value"><?php echo $stats['projects']['total']; ?></div>
                    <div class="stat-label">Published Projects</div>
                    <div class="stat-change positive">
                        <span><?php echo $stats['projects']['new']; ?> new today</span>
                    </div>
                </div>
                
                <div class="stat-card fade-in" style="animation-delay: 0.4s">
                    <div class="stat-header">
                        <div class="stat-icon">📬</div>
                    </div>
                    <div class="stat-value"><?php echo $stats['newsletter']['total']; ?></div>
                    <div class="stat-label">Newsletter Subscribers</div>
                    <div class="stat-change positive">
                        <span><?php echo $stats['newsletter']['new']; ?> new today</span>
                    </div>
                </div>
            </div>
            
            <!-- Recent Activity -->
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; margin-top: 2rem;">
                <!-- Recent Contacts -->
                <div class="card slide-up" style="animation-delay: 0.5s">
                    <div class="card-header">
                        <h3 class="card-title">Recent Contacts</h3>
                        <a href="contacts.php" class="btn btn-sm btn-secondary">View All</a>
                    </div>
                    <div style="max-height: 400px; overflow-y: auto;">
                        <?php if (empty($recentContacts)): ?>
                            <p style="color: var(--text-secondary); text-align: center; padding: 2rem;">No contacts yet</p>
                        <?php else: ?>
                            <?php foreach ($recentContacts as $contact): ?>
                            <div style="padding: 1rem; border-bottom: 1px solid var(--border-color); transition: var(--transition);" 
                                 onmouseover="this.style.background='var(--bg-primary)'" 
                                 onmouseout="this.style.background='transparent'">
                                <div style="display: flex; justify-content: space-between; align-items: start; margin-bottom: 0.5rem;">
                                    <div>
                                        <div style="font-weight: 600; color: var(--text-primary);"><?php echo htmlspecialchars($contact['name']); ?></div>
                                        <div style="font-size: 0.8125rem; color: var(--text-secondary);"><?php echo htmlspecialchars($contact['email']); ?></div>
                                    </div>
                                    <span class="badge badge-<?php echo $contact['status'] === 'new' ? 'info' : 'success'; ?>">
                                        <?php echo ucfirst($contact['status']); ?>
                                    </span>
                                </div>
                                <?php if ($contact['service']): ?>
                                <div style="font-size: 0.8125rem; color: var(--text-secondary); margin-top: 0.25rem;">
                                    Service: <?php echo htmlspecialchars($contact['service']); ?>
                                </div>
                                <?php endif; ?>
                                <div style="font-size: 0.75rem; color: var(--text-secondary); margin-top: 0.5rem;">
                                    <?php echo date('M d, Y H:i', strtotime($contact['created_at'])); ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- Recent Projects -->
                <div class="card slide-up" style="animation-delay: 0.6s">
                    <div class="card-header">
                        <h3 class="card-title">Recent Projects</h3>
                        <a href="projects.php" class="btn btn-sm btn-secondary">View All</a>
                    </div>
                    <div style="max-height: 400px; overflow-y: auto;">
                        <?php if (empty($recentProjects)): ?>
                            <p style="color: var(--text-secondary); text-align: center; padding: 2rem;">No projects yet</p>
                        <?php else: ?>
                            <?php foreach ($recentProjects as $project): ?>
                            <div style="padding: 1rem; border-bottom: 1px solid var(--border-color); transition: var(--transition);" 
                                 onmouseover="this.style.background='var(--bg-primary)'" 
                                 onmouseout="this.style.background='transparent'">
                                <div style="display: flex; justify-content: space-between; align-items: start; margin-bottom: 0.5rem;">
                                    <div style="font-weight: 600; color: var(--text-primary);"><?php echo htmlspecialchars($project['title']); ?></div>
                                    <span class="badge badge-<?php echo $project['status'] === 'published' ? 'success' : 'warning'; ?>">
                                        <?php echo ucfirst($project['status']); ?>
                                    </span>
                                </div>
                                <div style="font-size: 0.75rem; color: var(--text-secondary);">
                                    <?php echo date('M d, Y', strtotime($project['created_at'])); ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script src="assets/admin.js"></script>
</body>
</html>

