<?php
require_once '../includes/functions.php';
requireLogin();

// Update Session Balance (common for both)
if (!isAdmin()) {
    $user_id = $_SESSION['user_id'];
    $bal = $pdo->prepare("SELECT balance FROM users WHERE id = ?");
    $bal->execute([$user_id]);
    $res = $bal->fetch();
    $_SESSION['balance'] = $res['balance'] ?? 0;
}

include '../includes/header.php';

if (isAdmin()):
    // Admin Dashboard Content
    $users_count = $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'user'")->fetchColumn();
    $certs_count = $pdo->query("SELECT COUNT(*) FROM certificates")->fetchColumn();
    $pending_wallet = $pdo->query("SELECT COUNT(*) FROM wallet_requests WHERE status = 'pending'")->fetchColumn();
    
    // Fetch Recent Requests
    $recent_requests = $pdo->query("SELECT wr.*, u.name FROM wallet_requests wr JOIN users u ON wr.user_id = u.id ORDER BY wr.request_date DESC LIMIT 5")->fetchAll();
?>
    <div class="animate-fade-in">
        <div class="mb-4 d-flex justify-content-between align-items-end">
            <div>
                <h1 class="fw-800 mb-1">Admin <span class="text-primary">Console</span></h1>
                <p class="text-muted mb-0">Platform Overview & Management Hub</p>
            </div>
            <div class="text-end">
                <span class="badge bg-light text-dark rounded-pill px-3 py-2 border shadow-sm">
                    <i class="fas fa-calendar-day me-2 text-primary"></i> <?php echo date('D, d M Y'); ?>
                </span>
            </div>
        </div>

        <div class="row g-4 mb-5">
            <div class="col-md-4">
                <div class="glass-card p-4 h-100 position-relative overflow-hidden border-0 shadow-lg">
                    <div class="position-absolute top-0 end-0 p-3 opacity-10">
                        <i class="fas fa-users fa-4x text-primary"></i>
                    </div>
                    <p class="text-uppercase small fw-bold text-muted mb-1">Total Verified Users</p>
                    <h2 class="fw-800 mb-0"><?php echo number_format($users_count); ?></h2>
                    <div class="mt-3">
                        <a href="manage_users.php" class="text-primary text-decoration-none small fw-bold">Manage Users <i class="fas fa-chevron-right ms-1"></i></a>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="glass-card p-4 h-100 position-relative overflow-hidden border-0 shadow-lg">
                    <div class="position-absolute top-0 end-0 p-3 opacity-10">
                        <i class="fas fa-file-invoice fa-4x text-success"></i>
                    </div>
                    <p class="text-uppercase small fw-bold text-muted mb-1">Total Certificates</p>
                    <h2 class="fw-800 mb-0"><?php echo number_format($certs_count); ?></h2>
                    <div class="mt-3">
                        <a href="manage_certificates.php" class="text-success text-decoration-none small fw-bold">Global List <i class="fas fa-chevron-right ms-1"></i></a>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="glass-card p-4 h-100 position-relative overflow-hidden border-0 shadow-lg" style="background: rgba(255, 193, 7, 0.03);">
                    <div class="position-absolute top-0 end-0 p-3 opacity-10">
                        <i class="fas fa-clock fa-4x text-warning"></i>
                    </div>
                    <p class="text-uppercase small fw-bold text-muted mb-1">Wallet Requests</p>
                    <h2 class="fw-800 mb-0 text-warning"><?php echo number_format($pending_wallet); ?> <small class="text-muted fw-normal" style="font-size: 0.5em;">PENDING</small></h2>
                    <div class="mt-3">
                        <a href="manage_wallet.php" class="text-warning text-decoration-none small fw-bold">Verification Center <i class="fas fa-chevron-right ms-1"></i></a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row g-4">
            <div class="col-lg-8">
                <div class="glass-card h-100 border-0 shadow-sm overflow-hidden">
                    <div class="p-4 bg-light border-bottom d-flex justify-content-between align-items-center">
                        <h5 class="fw-bold mb-0">Recent Activity</h5>
                        <a href="manage_wallet.php" class="btn btn-sm btn-white border rounded-pill px-3">View All</a>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <tbody>
                                <?php if (empty($recent_requests)): ?>
                                    <tr><td class="text-center py-5 text-muted">No recent wallet activity.</td></tr>
                                <?php endif; ?>
                                <?php foreach($recent_requests as $req): ?>
                                <tr>
                                    <td class="px-4 py-3">
                                        <div class="fw-bold text-dark"><?php echo htmlspecialchars($req['name']); ?></div>
                                        <div class="small text-muted"><?php echo date('d M, h:i A', strtotime($req['request_date'])); ?></div>
                                    </td>
                                    <td class="px-4 py-3 text-center">
                                        <span class="fw-bold text-primary">₹<?php echo number_format($req['amount'], 2); ?></span>
                                    </td>
                                    <td class="px-4 py-3 text-end">
                                        <span class="badge <?php echo $req['status'] === 'approved' ? 'bg-success' : ($req['status'] === 'pending' ? 'bg-warning' : 'bg-danger'); ?> rounded-pill px-3 py-2">
                                            <?php echo strtoupper($req['status']); ?>
                                        </span>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-lg-4">
                <div class="glass-card p-4 border-0 shadow-sm bg-primary text-white h-100" style="background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);">
                    <h5 class="fw-bold mb-4">Quick Shortcuts</h5>
                    <div class="d-grid gap-2">
                        <a href="settings.php" class="btn btn-white text-primary fw-bold text-start py-3 rounded-4 px-4 shadow-sm border-0">
                            <i class="fas fa-cog me-2"></i> System Settings
                        </a>
                        <a href="apply.php" class="btn btn-white text-primary fw-bold text-start py-3 rounded-4 px-4 shadow-sm border-0">
                            <i class="fas fa-plus-circle me-2"></i> New Application
                        </a>
                        <a href="profile.php" class="btn btn-white text-primary fw-bold text-start py-3 rounded-4 px-4 shadow-sm border-0">
                            <i class="fas fa-user-circle me-2"></i> Admin Profile
                        </a>
                    </div>
                    <div class="mt-5 p-3 rounded-4 bg-white bg-opacity-10">
                        <p class="small mb-0 opacity-75"><i class="fas fa-info-circle me-1"></i> Global analytics and API controls are available in the settings panel.</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

<?php else: 
    // User Dashboard Content
    $user_id = $_SESSION['user_id'];
    $certs_count = $pdo->query("SELECT COUNT(*) FROM certificates WHERE user_id = $user_id")->fetchColumn();
    $recent_certs = $pdo->query("SELECT * FROM certificates WHERE user_id = $user_id ORDER BY created_at DESC LIMIT 5")->fetchAll();
?>
    <div class="animate-fade-in">
        <!-- Hero Section -->
        <div class="p-4 p-md-5 rounded-4 shadow-lg border-0 mb-4 mb-md-5 position-relative overflow-hidden text-white" 
             style="background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);">
            <div class="hero-icon-container position-absolute bottom-0 end-0 opacity-10 p-2 p-md-4">
                <i class="fas fa-certificate fa-8x fa-md-10x"></i>
            </div>
            <div class="row align-items-center position-relative">
                <div class="col-md-8">
                    <h1 class="fw-800 display-6 display-md-5 mb-2">Jai Hind!</h1>
                    <p class="mb-3 mb-md-4 opacity-75 small-md">Your official document portal is ready. Create, manage, and verify your certificates instantly.</p>
                    <div class="d-flex flex-wrap gap-2">
                        <a href="apply.php" class="btn btn-white btn-sm btn-md-lg text-primary fw-800 px-4 px-md-5 rounded-pill shadow">
                            <i class="fas fa-plus me-1 me-md-2"></i> New Apply
                        </a>
                        <a href="my_certificates.php" class="btn btn-outline-white btn-sm btn-md-lg rounded-pill px-4 px-md-5">
                            <i class="fas fa-list-ul me-1 me-md-2"></i> My Records
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row g-3 g-md-4 mb-4 mb-md-5">
            <div class="col-md-6">
                <div class="glass-card p-3 p-md-4 h-100 border-0 shadow-sm d-flex align-items-center">
                    <div class="icon-box p-3 p-md-4 rounded-4 bg-primary bg-opacity-10 me-3 me-md-4 text-center">
                        <i class="fas fa-vault text-primary fa-2x fa-md-3x"></i>
                    </div>
                    <div class="flex-grow-1">
                        <p class="text-muted small fw-bold text-uppercase mb-0 mb-md-1" style="font-size: 0.75rem;">Funds</p>
                        <h3 class="fw-800 mb-1 mb-md-2">₹<?php echo number_format($_SESSION['balance'], 2); ?></h3>
                        <a href="my_wallet.php" class="text-primary text-decoration-none small fw-bold">Load Wallet <i class="fas fa-plus ms-1"></i></a>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="glass-card p-3 p-md-4 h-100 border-0 shadow-sm d-flex align-items-center">
                    <div class="icon-box p-3 p-md-4 rounded-4 bg-success bg-opacity-10 me-3 me-md-4 text-center">
                        <i class="fas fa-award text-success fa-2x fa-md-3x"></i>
                    </div>
                    <div class="flex-grow-1">
                        <p class="text-muted small fw-bold text-uppercase mb-0 mb-md-1" style="font-size: 0.75rem;">History</p>
                        <h3 class="fw-800 mb-1 mb-md-2"><?php echo $certs_count; ?> <small class="text-muted fw-normal" style="font-size: 0.6em;">ITEMS</small></h3>
                        <a href="my_certificates.php" class="text-success text-decoration-none small fw-bold">View History <i class="fas fa-arrow-right ms-1"></i></a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-12">
                <div class="glass-card border-0 shadow-sm overflow-hidden mb-5">
                    <div class="p-4 bg-white border-bottom d-flex justify-content-between align-items-center">
                        <h5 class="fw-bold mb-0"><i class="fas fa-history me-2 text-primary"></i> Recent Certificates</h5>
                        <a href="my_certificates.php" class="text-primary text-decoration-none small fw-bold">See All</a>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Name</th>
                                    <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Reg. No</th>
                                    <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold text-end">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($recent_certs)): ?>
                                    <tr><td colspan="3" class="text-center py-5 text-muted">No certificates generated yet.</td></tr>
                                <?php endif; ?>
                                <?php foreach($recent_certs as $cert): ?>
                                <tr>
                                    <td class="px-4 py-3">
                                        <div class="fw-bold"><?php echo htmlspecialchars($cert['name']); ?></div>
                                        <div class="small text-muted"><?php echo date('d M, Y', strtotime($cert['created_at'])); ?></div>
                                    </td>
                                    <td class="px-4 py-3">
                                        <code class="text-primary fw-bold"><?php echo htmlspecialchars($cert['registration_no']); ?></code>
                                    </td>
                                    <td class="px-4 py-3 text-end">
                                        <a href="../generate_pdf.php?id=<?php echo $cert['id']; ?>" class="btn btn-sm btn-outline-primary rounded-pill px-3" target="_blank">
                                            <i class="fas fa-download"></i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>

<style>
    .btn-white { background: white; color: var(--primary-color) !important; }
    .btn-white:hover { background: #f8fafc; transform: translateY(-2px); box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
    .btn-outline-white { border: 2px solid white; color: white !important; }
    .btn-outline-white:hover { background: white; color: var(--primary-color) !important; }
    .display-5 { font-size: 2.5rem; letter-spacing: -0.05em; }
    .fw-800 { font-weight: 800; }
</style>

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