<?php
$page_title = "অ্যাডমিট কার্ড";
include 'db_connect.php';
include 'templates/header.php';

// Centralized configurations
include_once __DIR__ . '/config/institution_info.php';
include_once __DIR__ . '/config/class_list.php';

// Get parameters
$exam_id = isset($_GET['exam_id']) ? (int)$_GET['exam_id'] : 0;
$class_id = isset($_GET['class_id']) ? (int)$_GET['class_id'] : 0;

if (!$exam_id || !$class_id) {
    $_SESSION['message'] = ['type' => 'warning', 'text' => 'অ্যাডমিট কার্ড দেখার জন্য পরীক্ষা এবং শ্রেণি নির্বাচন করুন।'];
    header('Location: generate_admit_card.php');
    exit();
}

// Fetch exam details
$exam_stmt = $conn->prepare("SELECT e.exam_name, e.session_year, ec.name_bn as category_name FROM exams e LEFT JOIN exam_categories ec ON e.category_id = ec.id WHERE e.id = ?");
$exam_stmt->bind_param("i", $exam_id);
$exam_stmt->execute();
$exam_result = $exam_stmt->get_result();
$exam_details = $exam_result->fetch_assoc();

if (!$exam_details) {
    $_SESSION['message'] = ['type' => 'danger', 'text' => 'পরীক্ষার তথ্য পাওয়া যায়নি।'];
    header('Location: generate_admit_card.php');
    exit();
}

// Fetch students for the class
$students_stmt = $conn->prepare("SELECT id, student_id, name_bn, guardian_name_bn, guardian_phone FROM students WHERE class_id = ? AND status = 'active' ORDER BY student_id");
$students_stmt->bind_param("i", $class_id);
$students_stmt->execute();
$students_result = $students_stmt->get_result();

$class_name = $classes[$class_id] ?? 'Unknown Class';
?>

<div class="card shadow-sm no-print">
    <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
        <h4 class="mb-0">অ্যাডমিট কার্ড - <?php echo htmlspecialchars($exam_details['exam_name']); ?></h4>
        <div>
            <button onclick="window.print()" class="btn btn-light btn-sm me-2">
                <i class="fas fa-print"></i> প্রিন্ট করুন
            </button>
            <a href="generate_admit_card.php" class="btn btn-outline-light btn-sm">
                <i class="fas fa-arrow-left"></i> ফিরে যান
            </a>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-6">
                <strong>পরীক্ষার নাম:</strong> <?php echo htmlspecialchars($exam_details['exam_name']); ?><br>
                <strong>সেশন:</strong> <?php echo htmlspecialchars($exam_details['session_year']); ?><br>
                <strong>শ্রেণি:</strong> <?php echo htmlspecialchars($class_name); ?>
            </div>
            <div class="col-md-6 text-end">
                <strong>মোট ছাত্র:</strong> <?php echo $students_result->num_rows; ?> জন<br>
                <strong>পরীক্ষার ধরণ:</strong> <?php echo htmlspecialchars($exam_details['category_name'] ?? 'সাধারণ'); ?>
            </div>
        </div>
    </div>
</div>

<?php if ($students_result->num_rows > 0): ?>
    <div class="admit-cards-container">
        <?php 
        $students = [];
        while ($student = $students_result->fetch_assoc()) {
            $students[] = $student;
        }
        
        // Group students into pairs for 2 cards per page
        for ($i = 0; $i < count($students); $i += 2): 
        ?>
            <div class="admit-card-page">
                <!-- First Card -->
                <div class="admit-card">
                    <!-- Header -->
                    <div class="admit-header text-center">
                        <h3 class="institution-name"><?php echo htmlspecialchars($institution_info['name']); ?></h3>
                        <p class="institution-address"><?php echo htmlspecialchars($institution_info['address']); ?></p>
                        <h4 class="admit-title">প্রবেশপত্র</h4>
                    </div>

                    <!-- Student Information -->
                    <div class="student-info">
                        <table class="info-table">
                            <tr>
                                <td><strong>ছাত্রের নাম:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i]['name_bn']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>স্টুডেন্ট আইডি:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i]['student_id']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>শ্রেণি:</strong></td>
                                <td><?php echo htmlspecialchars($class_name); ?></td>
                            </tr>
                            <tr>
                                <td><strong>পরীক্ষার নাম:</strong></td>
                                <td><?php echo htmlspecialchars($exam_details['exam_name']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>অভিভাবকের নাম:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i]['guardian_name_bn']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>মোবাইল নং:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i]['guardian_phone']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>সেশন:</strong></td>
                                <td><?php echo htmlspecialchars($exam_details['session_year']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>সিট নং:</strong></td>
                                <td></td>
                            </tr>
                        </table>
                    </div>

                    <!-- Signature Section -->
                    <div class="signature-section">
                        <div class="row">
                            <div class="col-4 text-center">
                                <div class="signature-line"></div>
                                <p>জামেয়া প্রধানের স্বাক্ষর</p>
                            </div>
                            <div class="col-4 text-center">
                                <div class="signature-line"></div>
                                <p>শিক্ষা পরিচালকের স্বাক্ষর</p>
                            </div>
                            <div class="col-4 text-center">
                                <div class="signature-line"></div>
                                <p>প্রধান শিক্ষকের স্বাক্ষর</p>
                            </div>
                        </div>
                    </div>

                    <!-- Footer Date Only -->
                    <div class="admit-footer">
                        <p class="issue-date">জারির তারিখ: 27/06/2025</p>
                    </div>
                </div>

                <!-- Second Card (if exists) -->
                <?php if (isset($students[$i + 1])): ?>
                <div class="admit-card">
                    <!-- Header -->
                    <div class="admit-header text-center">
                        <h3 class="institution-name"><?php echo htmlspecialchars($institution_info['name']); ?></h3>
                        <p class="institution-address"><?php echo htmlspecialchars($institution_info['address']); ?></p>
                        <h4 class="admit-title">প্রবেশপত্র</h4>
                    </div>

                    <!-- Student Information -->
                    <div class="student-info">
                        <table class="info-table">
                            <tr>
                                <td><strong>ছাত্রের নাম:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i + 1]['name_bn']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>স্টুডেন্ট আইডি:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i + 1]['student_id']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>শ্রেণি:</strong></td>
                                <td><?php echo htmlspecialchars($class_name); ?></td>
                            </tr>
                            <tr>
                                <td><strong>পরীক্ষার নাম:</strong></td>
                                <td><?php echo htmlspecialchars($exam_details['exam_name']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>অভিভাবকের নাম:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i + 1]['guardian_name_bn']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>মোবাইল নং:</strong></td>
                                <td><?php echo htmlspecialchars($students[$i + 1]['guardian_phone']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>সেশন:</strong></td>
                                <td><?php echo htmlspecialchars($exam_details['session_year']); ?></td>
                            </tr>
                            <tr>
                            <td><strong>সিট নং:</strong></td>
                                <td></td>
                            </tr>
                        </table>
                    </div>

                    <!-- Signature Section -->
                    <div class="signature-section">
                        <div class="row">
                            <div class="col-4 text-center">
                                <div class="signature-line"></div>
                                <p>জামেয়া প্রধানের স্বাক্ষর</p>
                            </div>
                            <div class="col-4 text-center">
                                <div class="signature-line"></div>
                                <p>শিক্ষা পরিচালকের স্বাক্ষর</p>
                            </div>
                            <div class="col-4 text-center">
                                <div class="signature-line"></div>
                                <p>প্রধান শিক্ষকের স্বাক্ষর</p>
                            </div>
                        </div>
                    </div>

                    <!-- Footer Date Only -->
                    <div class="admit-footer">
                        <p class="issue-date">জারির তারিখ: 27/06/2025</p>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        <?php endfor; ?>
    </div>
<?php else: ?>
    <div class="alert alert-warning text-center">
        এই শ্রেণিতে কোনো সক্রিয় ছাত্র পাওয়া যায়নি।
    </div>
<?php endif; ?>

<style>
/* Print Styles */
@media print {
    .no-print { display: none !important; }
    
    @page {
        size: A4 portrait;
        margin: 0.2in;
    }
    
    body {
        margin: 0;
        padding: 0;
        background: white;
        font-size: 11px;
    }
    
    .admit-card-page {
        page-break-after: always;
        margin: 0;
        padding: 0;
        height: 100vh;
    }
    
    .admit-card {
        border: 2px solid #000;
        padding: 8px 10px 38px 10px;
        margin: 5px 0;
        height: 46vh;
        position: relative;
        background: white;
        page-break-inside: avoid;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
    }
    
    .student-info {
        margin-bottom: 8px;
    }
    
    .signature-section {
        margin-top: auto;
        margin-bottom: 0;
    }
    
    .signature-line {
        height: 16px;
    }
    
    .signature-section p {
        font-size: 8px;
    }
    
    .admit-footer {
        position: absolute;
        bottom: 6px;
        right: 12px;
        text-align: right;
    }
    
    .issue-date {
        font-size: 9px;
        margin-bottom: 0;
        font-weight: bold;
    }
}

/* Screen Styles */
.admit-cards-container {
    margin: 20px 0;
}

.admit-card-page {
    margin-bottom: 30px;
}

.admit-card {
    border: 2px solid #006a4e;
    border-radius: 10px;
    padding: 20px;
    background: white;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    height: 450px;
    position: relative;
    margin-bottom: 20px;
}

.admit-header {
    border-bottom: 3px solid #006a4e;
    padding-bottom: 15px;
    margin-bottom: 20px;
}

.institution-name {
    color: #006a4e;
    font-weight: bold;
    margin-bottom: 5px;
    font-size: 20px;
}

.institution-address {
    color: #666;
    margin-bottom: 10px;
    font-size: 14px;
}

.admit-title {
    color: #f42a41;
    font-weight: bold;
    border: 2px solid #f42a41;
    display: inline-block;
    padding: 10px 25px;
    border-radius: 5px;
    font-size: 18px;
}

.student-info {
    margin-bottom: 20px;
}

.info-table {
    width: 100%;
}

.info-table td {
    padding: 6px 0;
    border-bottom: 1px solid #eee;
    font-size: 12px;
}

.info-table td:first-child {
    width: 40%;
    font-weight: bold;
}

.instructions {
    margin-bottom: 20px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 5px;
    max-height: 100px;
    overflow: hidden;
}

.instructions h6 {
    color: #006a4e;
    margin-bottom: 10px;
    font-size: 14px;
}

.instructions ol {
    margin-bottom: 0;
    font-size: 12px;
}

.instructions li {
    margin-bottom: 6px;
    line-height: 1.3;
}

.signature-section {
    margin-bottom: 20px;
}

.signature-line {
    border-bottom: 2px solid #000;
    width: 80%;
    margin: 0 auto 8px;
    height: 30px;
}

.signature-section p {
    margin: 0;
    font-size: 12px;
    font-weight: bold;
}

.admit-footer {
    position: absolute;
    bottom: 15px;
    right: 20px;
    text-align: right;
}

.issue-date {
    margin-bottom: 0;
    font-size: 12px;
    font-weight: bold;
}
</style>

<?php 
$exam_stmt->close();
$students_stmt->close();
$conn->close();
include 'templates/footer.php'; 
?> 

When I try to print this admit card, the signature part of the first admit card on the page does not show, but the signature part of the second admit card does. So solve the problem of not showing the signature of the first admit card.