<?php
session_start();
require_once 'config.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: index.php');
    exit();
}

$pdo = getDB();
$user_id = $_SESSION['user_id'];
$user_role = $_SESSION['user_role'];

// Get statistics
$stmt = $pdo->query("SELECT COUNT(*) as total FROM certificates");
$total_certificates = $stmt->fetch()['total'];

$stmt = $pdo->query("SELECT COUNT(*) as generated FROM certificates WHERE certificate_status != 'draft'");
$generated_certificates = $stmt->fetch()['generated'];

$stmt = $pdo->query("SELECT COUNT(*) as issued FROM certificates WHERE certificate_status = 'issued'");
$issued_certificates = $stmt->fetch()['issued'];

// Recent activity
$stmt = $pdo->prepare("
    SELECT c.*, u.full_name as generated_by_name 
    FROM certificates c 
    LEFT JOIN users u ON c.generated_by = u.id 
    ORDER BY c.created_at DESC 
    LIMIT 10
");
$stmt->execute();
$recent_certificates = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - SKUAST-K Certificate Portal</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f5f7fb; }
        .sidebar { width: 250px; background: #2d3748; color: white; height: 100vh; position: fixed; padding: 20px 0; }
        .sidebar h2 { padding: 0 20px 20px; border-bottom: 1px solid #4a5568; font-size: 18px; }
        .sidebar .user-info { padding: 20px; border-bottom: 1px solid #4a5568; margin-bottom: 20px; }
        .sidebar .user-info .name { font-weight: bold; font-size: 16px; }
        .sidebar .user-info .email { font-size: 12px; color: #a0aec0; }
        .sidebar a { display: block; padding: 12px 20px; color: #a0aec0; text-decoration: none; transition: all 0.3s; }
        .sidebar a:hover, .sidebar a.active { background: #4a5568; color: white; }
        .sidebar a i { width: 20px; margin-right: 10px; }
        .main-content { margin-left: 250px; padding: 30px; }
        .header { background: white; padding: 20px 30px; border-radius: 10px; margin-bottom: 30px; box-shadow: 0 2px 4px rgba(0,0,0,0.05); display: flex; justify-content: space-between; align-items: center; }
        .header h1 { font-size: 24px; color: #2d3748; }
        .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-bottom: 30px; }
        .stat-card { background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
        .stat-card .number { font-size: 28px; font-weight: bold; color: #667eea; }
        .stat-card .label { color: #718096; font-size: 14px; margin-top: 5px; }
        .table-container { background: white; border-radius: 10px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
        .table-container h3 { margin-bottom: 15px; color: #2d3748; }
        table { width: 100%; border-collapse: collapse; }
        th { text-align: left; padding: 12px; background: #f7fafc; color: #4a5568; font-weight: 600; font-size: 14px; }
        td { padding: 12px; border-bottom: 1px solid #e2e8f0; font-size: 14px; }
        .status-badge { padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 600; }
        .status-draft { background: #fefcbf; color: #975a16; }
        .status-generated { background: #bee3f8; color: #2a69ac; }
        .status-issued { background: #c6f6d5; color: #22543d; }
        .btn-logout { background: #e53e3e; color: white; border: none; padding: 8px 20px; border-radius: 5px; cursor: pointer; }
        .btn-logout:hover { background: #c53030; }
    </style>
</head>
<body>
    <div class="sidebar">
        <h2>🎓 SKUAST-K Portal</h2>
        <div class="user-info">
            <div class="name"><?php echo htmlspecialchars($_SESSION['user_name']); ?></div>
            <div class="email"><?php echo htmlspecialchars($_SESSION['user_email']); ?></div>
            <div class="email" style="margin-top:5px;color:#a0aec0;">Role: <?php echo $user_role; ?></div>
        </div>
        <a href="dashboard.php" class="active"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
        <a href="add_certificate.php"><i class="fas fa-plus-circle"></i> Add Certificate</a>
        <a href="bulk_upload.php"><i class="fas fa-upload"></i> Bulk Upload</a>
        <a href="map_photo.php"><i class="fas fa-image"></i> Map Photo</a>
        <a href="generate_certificate.php"><i class="fas fa-file-pdf"></i> Generate Certificate</a>
        <a href="view_certificates.php"><i class="fas fa-list"></i> View All</a>
        <a href="logout.php" style="margin-top: 30px; border-top: 1px solid #4a5568; padding-top: 20px;"><i class="fas fa-sign-out-alt"></i> Logout</a>
    </div>
    
    <div class="main-content">
        <div class="header">
            <h1>Dashboard</h1>
            <form method="POST" action="logout.php">
                <button type="submit" class="btn-logout"><i class="fas fa-sign-out-alt"></i> Logout</button>
            </form>
        </div>
        
        <div class="stats">
            <div class="stat-card">
                <div class="number"><?php echo $total_certificates; ?></div>
                <div class="label">Total Certificates</div>
            </div>
            <div class="stat-card">
                <div class="number"><?php echo $generated_certificates; ?></div>
                <div class="label">Generated Certificates</div>
            </div>
            <div class="stat-card">
                <div class="number"><?php echo $issued_certificates; ?></div>
                <div class="label">Issued Certificates</div>
            </div>
        </div>
        
        <div class="table-container">
            <h3>Recent Certificates</h3>
            <table>
                <thead>
                    <tr>
                        <th>Reg No</th>
                        <th>Name</th>
                        <th>Degree</th>
                        <th>GPA</th>
                        <th>Status</th>
                        <th>Generated By</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($recent_certificates as $cert): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($cert['registration_no']); ?></td>
                        <td><?php echo htmlspecialchars($cert['name']); ?></td>
                        <td><?php echo htmlspecialchars($cert['degree']); ?></td>
                        <td><?php echo $cert['grade_point_average']; ?></td>
                        <td>
                            <span class="status-badge status-<?php echo $cert['certificate_status']; ?>">
                                <?php echo ucfirst($cert['certificate_status']); ?>
                            </span>
                        </td>
                        <td><?php echo htmlspecialchars($cert['generated_by_name'] ?? 'N/A'); ?></td>
                        <td><?php echo date('d M Y', strtotime($cert['created_at'])); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>