<?php
$pageTitle = 'Dashboard';
require_once 'includes/header.php';

$stats = getAdminDashboardStats();
$recentActivities = getRecentActivities(8);

// Recent bookings
$recentBookings = $db->query("
    SELECT b.*, u.name as user_name, p.title as package_title 
    FROM bookings b 
    JOIN users u ON b.user_id = u.id 
    JOIN packages p ON b.package_id = p.id 
    ORDER BY b.created_at DESC LIMIT 5
")->fetchAll();
?>

<div class="page-header">
    <div>
        <h1>Dashboard</h1>
        <p style="color:var(--gray);font-size:0.9rem;">Welcome back, <?= sanitize($adminUser['name']) ?>! Here's what's happening.</p>
    </div>
    <div>
        <span style="color:var(--gray);font-size:0.85rem;"><i class="fas fa-calendar"></i> <?= date('l, d M Y') ?></span>
    </div>
</div>

<!-- Stats Cards -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon blue"><i class="fas fa-calendar-check"></i></div>
        <div class="stat-info">
            <h3><?= $stats['today_bookings'] ?></h3>
            <p>Today's Bookings</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon green"><i class="fas fa-box-open"></i></div>
        <div class="stat-info">
            <h3><?= $stats['total_packages'] ?></h3>
            <p>Total Packages</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon orange"><i class="fas fa-check-circle"></i></div>
        <div class="stat-info">
            <h3><?= $stats['active_packages'] ?></h3>
            <p>Active Packages</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon purple"><i class="fas fa-shopping-cart"></i></div>
        <div class="stat-info">
            <h3><?= $stats['total_bookings'] ?></h3>
            <p>Total Bookings</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon green"><i class="fas fa-rupee-sign"></i></div>
        <div class="stat-info">
            <h3><?= formatCurrency($stats['total_revenue']) ?></h3>
            <p>Total Revenue</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon blue"><i class="fas fa-users"></i></div>
        <div class="stat-info">
            <h3><?= $stats['total_users'] ?></h3>
            <p>Registered Users</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon yellow"><i class="fas fa-clock"></i></div>
        <div class="stat-info">
            <h3><?= $stats['pending_bookings'] ?></h3>
            <p>Pending Bookings</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon red"><i class="fas fa-envelope"></i></div>
        <div class="stat-info">
            <h3><?= $stats['new_enquiries'] ?></h3>
            <p>New Enquiries</p>
        </div>
    </div>
</div>

<div style="display:grid;grid-template-columns:1.5fr 1fr;gap:25px;">
    <!-- Recent Bookings -->
    <div class="admin-card">
        <div class="card-header">
            <h3><i class="fas fa-calendar-check text-primary"></i> Recent Bookings</h3>
            <a href="<?= ADMIN_URL ?>bookings.php" class="btn btn-sm btn-outline">View All</a>
        </div>
        <div class="card-body" style="padding:0;">
            <div class="table-responsive">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Booking ID</th>
                            <th>Customer</th>
                            <th>Package</th>
                            <th>Amount</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentBookings as $booking): ?>
                            <tr>
                                <td><strong><?= $booking['booking_id'] ?></strong></td>
                                <td><?= sanitize($booking['user_name']) ?></td>
                                <td><?= sanitize(truncateText($booking['package_title'], 30)) ?></td>
                                <td><?= formatCurrency($booking['final_amount']) ?></td>
                                <td><span class="status-badge status-<?= $booking['status'] ?>"><?= ucfirst($booking['status']) ?></span></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($recentBookings)): ?>
                            <tr><td colspan="5" style="text-align:center;color:var(--gray);">No bookings yet</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <!-- Recent Activities -->
    <div class="admin-card">
        <div class="card-header">
            <h3><i class="fas fa-history text-primary"></i> Recent Activities</h3>
        </div>
        <div class="card-body">
            <ul class="activity-list">
                <?php foreach ($recentActivities as $activity): ?>
                    <li class="activity-item">
                        <div class="activity-icon" style="background:<?= 
                            strpos($activity['action'], 'login') !== false ? 'rgba(59,130,246,0.1);color:#3B82F6' :
                            (strpos($activity['action'], 'booking') !== false ? 'rgba(16,185,129,0.1);color:#10B981' :
                            (strpos($activity['action'], 'payment') !== false ? 'rgba(139,92,246,0.1);color:#8B5CF6' :
                            'rgba(255,107,53,0.1);color:#FF6B35'))
                        ?>">
                            <i class="fas fa-<?= 
                                strpos($activity['action'], 'login') !== false ? 'sign-in-alt' :
                                (strpos($activity['action'], 'booking') !== false ? 'calendar-check' :
                                (strpos($activity['action'], 'payment') !== false ? 'credit-card' :
                                (strpos($activity['action'], 'package') !== false ? 'box-open' : 'circle')))
                            ?>"></i>
                        </div>
                        <div class="activity-content">
                            <p><?= sanitize($activity['description']) ?></p>
                            <span class="activity-time"><i class="fas fa-clock"></i> <?= formatDate($activity['created_at'], 'd M Y, h:i A') ?></span>
                        </div>
                    </li>
                <?php endforeach; ?>
                <?php if (empty($recentActivities)): ?>
                    <li style="text-align:center;color:var(--gray);padding:20px;">No recent activities</li>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</div>

<!-- Quick Actions -->
<div class="admin-card" style="margin-top:25px;">
    <div class="card-header">
        <h3><i class="fas fa-bolt text-primary"></i> Quick Actions</h3>
    </div>
    <div class="card-body">
        <div style="display:flex;gap:12px;flex-wrap:wrap;">
            <a href="<?= ADMIN_URL ?>package-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> New Package</a>
            <a href="<?= ADMIN_URL ?>place-add.php" class="btn btn-secondary"><i class="fas fa-map-marker-alt"></i> New Place</a>
            <a href="<?= ADMIN_URL ?>bookings.php" class="btn btn-outline"><i class="fas fa-calendar-check"></i> View Bookings</a>
            <a href="<?= ADMIN_URL ?>users.php" class="btn btn-outline"><i class="fas fa-users"></i> Manage Users</a>
            <a href="<?= ADMIN_URL ?>settings.php" class="btn btn-outline"><i class="fas fa-cog"></i> Settings</a>
        </div>
    </div>
</div>

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