<?php
session_start();
require_once '../db_connect.php';

if(!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header('Location: ../login');
    exit;
}

$admin_id = $_SESSION['user_id'];

// Get counts
$pending_users = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM users WHERE status = 'pending'"))['count'];
$total_users = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM users WHERE role != 'admin'"))['count'];
$total_lands = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM lands"))['count'];
$pending_alerts_count = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM overlap_alerts WHERE is_resolved = 0"))['count'] ?? 0;
$total_brokers = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM users WHERE role = 'broker' AND status = 'approved'"))['count'];
$total_clients = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM clients"))['count'] ?? 0;

// Recent registrations
$recent_users = mysqli_query($conn, "SELECT * FROM users WHERE role != 'admin' ORDER BY created_at DESC LIMIT 5");

// Recent alerts
$recent_alerts = mysqli_query($conn, "
    SELECT oa.*, l1.land_name as land1_name, l2.land_name as land2_name,
           u1.username as user1, u2.username as user2
    FROM overlap_alerts oa
    JOIN lands l1 ON oa.land_id_1 = l1.id
    JOIN lands l2 ON oa.land_id_2 = l2.id
    JOIN users u1 ON l1.user_id = u1.id
    JOIN users u2 ON l2.user_id = u2.id
    ORDER BY oa.created_at DESC LIMIT 5
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Land Secure</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../assets/font-awesome/css/all.min.css">
    <link href="../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <!-- Page Header -->
        <div class="page-header">
            <div class="page-header-content">
                <h1>Dashboard</h1>
                <p>Welcome back, <?php echo htmlspecialchars($_SESSION['full_name'] ?? $_SESSION['username']); ?>!</p>
            </div>
            <div class="page-header-actions">
                <span class="date-display">
                    <i class="fas fa-calendar-alt"></i>
                    <?php echo date('l, F j, Y'); ?>
                </span>
            </div>
        </div>

        <!-- Stats Grid -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon pending">
                    <i class="fas fa-user-clock"></i>
                </div>
                <div class="stat-content">
                    <span class="stat-value"><?php echo $pending_users; ?></span>
                    <span class="stat-label">Pending Approvals</span>
                </div>
                <a href="users?filter=pending" class="stat-link">
                    <i class="fas fa-arrow-right"></i>
                </a>
            </div>

            <div class="stat-card">
                <div class="stat-icon users">
                    <i class="fas fa-users"></i>
                </div>
                <div class="stat-content">
                    <span class="stat-value"><?php echo $total_users; ?></span>
                    <span class="stat-label">Total Users</span>
                </div>
                <a href="users" class="stat-link">
                    <i class="fas fa-arrow-right"></i>
                </a>
            </div>

            <div class="stat-card">
                <div class="stat-icon brokers">
                    <i class="fas fa-user-tie"></i>
                </div>
                <div class="stat-content">
                    <span class="stat-value"><?php echo $total_brokers; ?></span>
                    <span class="stat-label">Active Brokers</span>
                </div>
                <a href="users?filter=broker" class="stat-link">
                    <i class="fas fa-arrow-right"></i>
                </a>
            </div>

            <div class="stat-card">
                <div class="stat-icon clients">
                    <i class="fas fa-user-friends"></i>
                </div>
                <div class="stat-content">
                    <span class="stat-value"><?php echo $total_clients; ?></span>
                    <span class="stat-label">Total Clients</span>
                </div>
                <a href="clients" class="stat-link">
                    <i class="fas fa-arrow-right"></i>
                </a>
            </div>

            <div class="stat-card">
                <div class="stat-icon lands">
                    <i class="fas fa-map"></i>
                </div>
                <div class="stat-content">
                    <span class="stat-value"><?php echo $total_lands; ?></span>
                    <span class="stat-label">Total Lands</span>
                </div>
                <a href="lands" class="stat-link">
                    <i class="fas fa-arrow-right"></i>
                </a>
            </div>

            <div class="stat-card">
                <div class="stat-icon alerts">
                    <i class="fas fa-exclamation-triangle"></i>
                </div>
                <div class="stat-content">
                    <span class="stat-value"><?php echo $pending_alerts_count; ?></span>
                    <span class="stat-label">Pending Alerts</span>
                </div>
                <a href="alerts" class="stat-link">
                    <i class="fas fa-arrow-right"></i>
                </a>
            </div>
        </div>

        <!-- Content Grid -->
        <div class="content-grid">
            <!-- Recent Users -->
            <div class="content-card">
                <div class="card-header">
                    <h3><i class="fas fa-user-plus"></i> Recent Registrations</h3>
                    <a href="users" class="view-all-btn">View All</a>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="modern-table">
                            <thead>
                                <tr>
                                    <th>User</th>
                                    <th>Role</th>
                                    <th>Status</th>
                                    <th>Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while($user = mysqli_fetch_assoc($recent_users)): ?>
                                <tr>
                                    <td>
                                        <div class="user-cell">
                                            <div class="user-avatar-table">
                                                <?php echo strtoupper(substr($user['full_name'] ?: $user['username'], 0, 1)); ?>
                                            </div>
                                            <div class="user-details">
                                                <strong><?php echo htmlspecialchars($user['full_name'] ?: $user['username']); ?></strong>
                                                <span><?php echo $user['email']; ?></span>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="role-badge <?php echo $user['role']; ?>">
                                            <?php echo ucfirst($user['role']); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <span class="status-badge <?php echo $user['status']; ?>">
                                            <?php echo ucfirst($user['status']); ?>
                                        </span>
                                    </td>
                                    <td class="date-cell"><?php echo date('M d, Y', strtotime($user['created_at'])); ?></td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <!-- Recent Alerts -->
            <div class="content-card">
                <div class="card-header">
                    <h3><i class="fas fa-exclamation-triangle"></i> Recent Overlap Alerts</h3>
                    <a href="alerts" class="view-all-btn">View All</a>
                </div>
                <div class="card-body">
                    <?php if($recent_alerts && mysqli_num_rows($recent_alerts) > 0): ?>
                    <div class="alerts-list">
                        <?php while($alert = mysqli_fetch_assoc($recent_alerts)): ?>
                        <div class="alert-item <?php echo $alert['is_resolved'] ? 'resolved' : 'pending'; ?>">
                            <div class="alert-icon">
                                <i class="fas fa-<?php echo $alert['is_resolved'] ? 'check-circle' : 'exclamation-circle'; ?>"></i>
                            </div>
                            <div class="alert-content">
                                <strong><?php echo htmlspecialchars($alert['land1_name']); ?></strong>
                                <span class="vs">vs</span>
                                <strong><?php echo htmlspecialchars($alert['land2_name']); ?></strong>
                                <p>
                                    <span class="status-badge <?php echo $alert['is_resolved'] ? 'approved' : 'pending'; ?>">
                                        <?php echo $alert['is_resolved'] ? 'Resolved' : 'Pending'; ?>
                                    </span>
                                    <span class="alert-date"><?php echo date('M d, Y', strtotime($alert['created_at'])); ?></span>
                                </p>
                            </div>
                        </div>
                        <?php endwhile; ?>
                    </div>
                    <?php else: ?>
                    <div class="empty-state">
                        <i class="fas fa-check-circle"></i>
                        <p>No overlap alerts at the moment</p>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="quick-actions">
            <h3>Quick Actions</h3>
            <div class="actions-grid">
                <a href="users?filter=pending" class="action-card">
                    <div class="action-icon pending">
                        <i class="fas fa-user-check"></i>
                    </div>
                    <span>Approve Users</span>
                </a>
                <a href="lands" class="action-card">
                    <div class="action-icon lands">
                        <i class="fas fa-map-marked-alt"></i>
                    </div>
                    <span>Manage Lands</span>
                </a>
                <a href="reports" class="action-card">
                    <div class="action-icon reports">
                        <i class="fas fa-chart-line"></i>
                    </div>
                    <span>View Reports</span>
                </a>
                <a href="activity_logs" class="action-card">
                    <div class="action-icon logs">
                        <i class="fas fa-history"></i>
                    </div>
                    <span>Activity Logs</span>
                </a>
            </div>
        </div>
    </main>

    <style>
    /* Page Header */
    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 30px;
        flex-wrap: wrap;
        gap: 20px;
    }

    .page-header h1 {
        font-size: 28px;
        font-weight: 700;
        color: #1a1d21;
        margin: 0 0 8px 0;
    }

    .page-header p {
        color: #6b7280;
        margin: 0;
        font-size: 15px;
    }

    .date-display {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 10px 16px;
        background: #fff;
        border-radius: 10px;
        font-size: 14px;
        color: #6b7280;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    }

    /* Stats Grid */
    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 20px;
        margin-bottom: 30px;
    }

    .stat-card {
        background: #fff;
        border-radius: 16px;
        padding: 24px;
        display: flex;
        align-items: center;
        gap: 16px;
        box-shadow: 0 4px 15px rgba(0,0,0,0.05);
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
    }

    .stat-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 25px rgba(0,0,0,0.1);
    }

    .stat-icon {
        width: 56px;
        height: 56px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 22px;
        color: #fff;
        flex-shrink: 0;
    }

    .stat-icon.pending { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); }
    .stat-icon.users { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
    .stat-icon.brokers { background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); }
    .stat-icon.clients { background: linear-gradient(135deg, #fa709a 0%, #fee140 100%); }
    .stat-icon.lands { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); }
    .stat-icon.alerts { background: linear-gradient(135deg, #f5af19 0%, #f12711 100%); }

    .stat-content {
        flex: 1;
    }

    .stat-value {
        display: block;
        font-size: 28px;
        font-weight: 700;
        color: #1a1d21;
        line-height: 1.2;
    }

    .stat-label {
        font-size: 13px;
        color: #6b7280;
    }

    .stat-link {
        width: 36px;
        height: 36px;
        border-radius: 10px;
        background: #f0f2f5;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #6b7280;
        transition: all 0.3s ease;
    }

    .stat-link:hover {
        background: var(--primary-gradient, linear-gradient(135deg, #667eea 0%, #764ba2 100%));
        color: #fff;
    }

    /* Content Grid */
    .content-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
        gap: 24px;
        margin-bottom: 30px;
    }

    .content-card {
        background: #fff;
        border-radius: 16px;
        box-shadow: 0 4px 15px rgba(0,0,0,0.05);
        overflow: hidden;
    }

    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20px 24px;
        border-bottom: 1px solid #f0f2f5;
    }

    .card-header h3 {
        font-size: 16px;
        font-weight: 600;
        color: #1a1d21;
        margin: 0;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .card-header h3 i {
        color: #667eea;
    }

    .view-all-btn {
        font-size: 13px;
        font-weight: 500;
        color: #667eea;
        text-decoration: none;
        padding: 6px 12px;
        border-radius: 8px;
        transition: all 0.3s ease;
    }

    .view-all-btn:hover {
        background: rgba(102, 126, 234, 0.1);
        color: #667eea;
        text-decoration: none;
    }

    .card-body {
        padding: 0;
    }

    /* Modern Table */
    .modern-table {
        width: 100%;
        border-collapse: collapse;
    }

    .modern-table th {
        padding: 14px 24px;
        text-align: left;
        font-size: 12px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: #6b7280;
        background: #f9fafb;
    }

    .modern-table td {
        padding: 16px 24px;
        border-top: 1px solid #f0f2f5;
    }

    .user-cell {
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .user-avatar-table {
        width: 40px;
        height: 40px;
        border-radius: 10px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        font-weight: 600;
        font-size: 14px;
    }

    .user-details strong {
        display: block;
        font-size: 14px;
        color: #1a1d21;
    }

    .user-details span {
        font-size: 12px;
        color: #6b7280;
    }

    .role-badge {
        display: inline-block;
        padding: 5px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 500;
    }

    .role-badge.broker { background: #dbeafe; color: #1e40af; }
    .role-badge.user { background: #f3f4f6; color: #4b5563; }

    .status-badge {
        display: inline-block;
        padding: 5px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 500;
    }

    .status-badge.pending { background: #fef3c7; color: #92400e; }
    .status-badge.approved { background: #d1fae5; color: #065f46; }
    .status-badge.rejected { background: #fee2e2; color: #991b1b; }
    .status-badge.suspended { background: #e5e7eb; color: #374151; }

    .date-cell {
        color: #6b7280;
        font-size: 13px;
    }

    /* Alerts List */
    .alerts-list {
        padding: 12px;
    }

    .alert-item {
        display: flex;
        align-items: flex-start;
        gap: 14px;
        padding: 16px;
        border-radius: 12px;
        margin-bottom: 8px;
        background: #f9fafb;
        transition: all 0.3s ease;
    }

    .alert-item:hover {
        background: #f3f4f6;
    }

    .alert-item.pending {
        border-left: 3px solid #f59e0b;
    }

    .alert-item.resolved {
        border-left: 3px solid #10b981;
    }

    .alert-icon {
        width: 40px;
        height: 40px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 18px;
        flex-shrink: 0;
    }

    .alert-item.pending .alert-icon {
        background: #fef3c7;
        color: #f59e0b;
    }

    .alert-item.resolved .alert-icon {
        background: #d1fae5;
        color: #10b981;
    }

    .alert-content {
        flex: 1;
    }

    .alert-content strong {
        font-size: 14px;
        color: #1a1d21;
    }

    .alert-content .vs {
        color: #9ca3af;
        font-size: 12px;
        margin: 0 6px;
    }

    .alert-content p {
        margin: 8px 0 0 0;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .alert-date {
        font-size: 12px;
        color: #9ca3af;
    }

    .empty-state {
        text-align: center;
        padding: 50px 20px;
        color: #9ca3af;
    }

    .empty-state i {
        font-size: 48px;
        margin-bottom: 16px;
        color: #d1d5db;
    }

    .empty-state p {
        margin: 0;
        font-size: 14px;
    }

    /* Quick Actions */
    .quick-actions {
        margin-top: 10px;
    }

    .quick-actions h3 {
        font-size: 18px;
        font-weight: 600;
        color: #1a1d21;
        margin: 0 0 20px 0;
    }

    .actions-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        gap: 16px;
    }

    .action-card {
        background: #fff;
        border-radius: 14px;
        padding: 24px;
        text-align: center;
        text-decoration: none;
        box-shadow: 0 4px 15px rgba(0,0,0,0.05);
        transition: all 0.3s ease;
    }

    .action-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 25px rgba(0,0,0,0.1);
        text-decoration: none;
    }

    .action-icon {
        width: 56px;
        height: 56px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 22px;
        color: #fff;
        margin: 0 auto 14px;
    }

    .action-icon.pending { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); }
    .action-icon.lands { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); }
    .action-icon.reports { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
    .action-icon.logs { background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); }

    .action-card span {
        font-size: 14px;
        font-weight: 500;
        color: #374151;
    }

    /* Responsive */
    @media (max-width: 768px) {
        .page-header {
            flex-direction: column;
            align-items: flex-start;
        }

        .stats-grid {
            grid-template-columns: 1fr;
        }

        .content-grid {
            grid-template-columns: 1fr;
        }

        .stat-card {
            padding: 20px;
        }

        .stat-value {
            font-size: 24px;
        }

        .modern-table th,
        .modern-table td {
            padding: 12px 16px;
        }

        .actions-grid {
            grid-template-columns: repeat(2, 1fr);
        }
    }
    </style>

    <script src="../assets/vendor/jquery/jquery.min.js"></script>
    <script src="../assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>
