<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!checkDBConnection()) exit;
Auth()->requireRole(['super_admin', 'admin']);

$db = DB();

// Get passed students
$passedStudents = $db->fetchAll("
    SELECT s.*, 
           c.course_name,
           u.institution_name as college_name
    FROM students s
    LEFT JOIN courses c ON s.course_id = c.id
    LEFT JOIN users u ON s.institution_id = u.id
    WHERE s.result_declared = 1 AND s.percentage >= 40 AND s.remark != 'Fail'
    ORDER BY s.result_declared_at DESC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Certificate Generation - <?= APP_SHORT_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/admin-style.css" rel="stylesheet">
</head>
<body>
    <?php include __DIR__ . '/../includes/admin_header.php'; ?>
    <div class="container-fluid">
        <div class="row">
            <?php include __DIR__ . '/../includes/admin_sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3"><i class="fas fa-certificate me-2 text-primary"></i>Certificate Generation</h2>
                </div>

                <div class="alert alert-info border-0 shadow-sm rounded-4 mb-4 d-flex align-items-center">
                    <i class="fas fa-info-circle fa-2x me-3"></i>
                    <div>
                        <h6 class="fw-bold mb-1">Eligible Students</h6>
                        <p class="mb-0 small">Only students who have passed their final exams (Result Declared & Percentage >= 40%) are listed here for certificate generation.</p>
                    </div>
                </div>

                <div class="card shadow-sm border-0" style="border-radius:15px;">
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-hover align-middle mb-0">
                                <thead class="bg-light">
                                    <tr>
                                        <th class="ps-4">Student Details</th>
                                        <th>Roll Number</th>
                                        <th>Passed Course</th>
                                        <th>Marks & Grade</th>
                                        <th>College/Institution</th>
                                        <th class="text-end pe-4">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if(empty($passedStudents)): ?>
                                        <tr><td colspan="6" class="text-center py-5 text-muted">No eligible students found for certificate generation.</td></tr>
                                    <?php endif; ?>
                                    <?php foreach ($passedStudents as $s): ?>
                                    <tr>
                                        <td class="ps-4">
                                            <div class="fw-bold"><?= htmlspecialchars($s['full_name'] ?? '') ?></div>
                                            <div class="small text-muted">DOB: <?= htmlspecialchars($s['date_of_birth'] ?? '-') ?></div>
                                        </td>
                                        <td><span class="badge bg-secondary"><?= htmlspecialchars($s['roll_number'] ?? 'N/A') ?></span></td>
                                        <td>
                                            <div class="fw-bold text-primary"><?= htmlspecialchars($s['course_name'] ?? 'Unknown Course') ?></div>
                                        </td>
                                        <td>
                                            <div class="fw-bold"><?= $s['marks_obtained'] ?> / <?= $s['total_marks'] ?></div>
                                            <span class="badge bg-success"><?= number_format($s['percentage'], 2) ?>% - <?= getDivisionLabel($s['division']) ?></span>
                                        </td>
                                        <td>
                                            <div class="small fw-bold text-dark"><?= htmlspecialchars($s['college_name'] ?? 'Direct Admission') ?></div>
                                        </td>
                                        <td class="text-end pe-4">
                                            <a href="certificate-view.php?id=<?= $s['id'] ?>&csrf_token=<?= Auth()->generateCSRFToken() ?>" target="_blank" class="btn btn-sm btn-primary shadow-sm rounded-pill px-3">
                                                <i class="fas fa-print me-1"></i> Generate
                                            </a>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>