<?php
require_once 'db_connect.php';
$result_data = null; $search_error = "";
if (isset($_POST['regi_no']) && isset($_POST['year'])) {
    $regi = trim($_POST['regi_no']); $year = trim($_POST['year']);
    $stmt = $pdo->prepare("SELECT * FROM results WHERE regi_no = ? AND passing_year = ? AND status = 'publish' LIMIT 1");
    $stmt->execute([$regi, $year]);
    $result_data = $stmt->fetch();
    if ($result_data) {
        $details = json_decode($result_data['details_json'], true);
        $photo_path = (!empty($details['student_photo'])) ? $details['student_photo'] : 'uploads/students/default.png';
    } else { $search_error = "Sorry! No result found for this Registration No. and Year."; }
}
?>
<?php include 'header.php'; ?>

    <style>
        .search-container { max-width: 550px; margin: 60px auto; }
        .search-card { border: none; border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.08); overflow: hidden; }
        .search-card-header { background: var(--green); color: #fff; padding: 18px; text-align: center; }
        .transcript-box { max-width: 900px; margin: 40px auto; background: #fff; padding: 45px; border-radius: 12px; border: 1px solid #e0e0e0; box-shadow: 0 10px 30px rgba(0,0,0,0.05); }
        .transcript-header { border-bottom: 3px solid var(--green); padding-bottom: 25px; margin-bottom: 30px; }
        .transcript-header-img { height: 80px; margin-right: 20px; }
        .student-photo { width: 110px; height: 135px; border: 2px solid #ddd; object-fit: cover; border-radius: 6px; padding: 3px; }
        .info-table th { background-color: #f8f9fa; width: 30%; color: #444; font-weight: 700; border-color: #e9ecef; }
        @media (max-width: 768px) { .transcript-box { padding: 20px; margin: 20px 10px; } .transcript-header { flex-direction: column; text-align: center; align-items: center !important; } .transcript-header-img { margin-right: 0; margin-bottom: 15px; height: 70px; } .student-photo { margin-top: 20px; } }
        @media print { .no-print { display: none !important; } body { background: #fff; padding: 0; margin: 0; } .transcript-box { box-shadow: none; border: 1px solid #000; margin: 0 auto; width: 100%; padding: 20px; } .badge { color: #000 !important; border: 1px solid #000; } }
    </style>

    <section class="py-5">
        <div class="container py-4">
            <?php if (!$result_data): ?>
            <div class="search-container no-print">
                <div class="card search-card">
                    <div class="search-card-header"><h5 class="mb-0 fw-bold"><i class="fas fa-search me-2"></i> Search Your Result</h5></div>
                    <div class="card-body p-4 p-md-5 bg-white">
                        <form method="POST">
                            <div class="mb-4"><label class="form-label fw-bold small">Enter Registration No.</label><input type="text" name="regi_no" class="form-control form-control-lg" required placeholder="Registration No" value="<?= htmlspecialchars(@$_POST['regi_no']) ?>"></div>
                            <div class="mb-4"><label class="form-label fw-bold small">Select Passing Year</label><select name="year" class="form-select form-select-lg" required><option value="">Select Year</option><?php for($y=2026; $y>=2000; $y--) { echo "<option value='$y'>$y</option>"; } ?></select></div>
                            <button type="submit" class="btn btn-success w-100 fw-bold py-3 shadow-sm rounded-3">VIEW RESULTS <i class="fas fa-arrow-right ms-2"></i></button>
                        </form>
                        <?php if ($search_error): ?><div class="alert alert-danger mt-4 text-center fw-bold small rounded-pill"><?= $search_error ?></div><?php endif; ?>
                    </div>
                </div>
            </div>
            <?php else: ?>
            <div class="transcript-box">
                <div class="d-flex justify-content-between align-items-start transcript-header">
                    <div class="d-flex align-items-center flex-column flex-md-row text-center text-md-start">
                        <img src="https://bdftl.com/assets/images/firstaidlogo.png" alt="Logo" class="transcript-header-img">
                        <div>
                            <h2 class="fw-bold mb-1 text-success" style="font-size: 1.6rem;">First Aid and Technology Institute</h2>
                            <p class="small text-dark mb-1 fw-semibold" style="line-height: 1.5; max-width: 450px;">Approved by the Ministry of Health and Family Welfare and Bangladesh Nursing Science and Midwifery Council.</p>
                            <p class="mb-0 fw-bold text-primary mt-2">Academic Transcript (Online Copy)</p>
                        </div>
                    </div>
                    <img src="<?= $photo_path ?>" class="student-photo shadow-sm" alt="Student">
                </div>

                <h5 class="fw-bold mb-4 text-uppercase border-bottom pb-2 text-center" style="color: var(--green);">Student Information</h5>
                <div class="table-responsive border-0">
                    <table class="table table-bordered info-table align-middle">
                        <tr><th>Student Name</th><td colspan="3" class="text-uppercase fw-bold"><?= htmlspecialchars($details['Name'] ?? 'N/A') ?></td></tr>
                        <tr><th>Registration No</th><td class="fw-bold text-success"><?= htmlspecialchars($result_data['regi_no']) ?></td><th>Roll No</th><td><?= htmlspecialchars($details['Roll'] ?? 'N/A') ?></td></tr>
                        <tr><th>Father's Name</th><td><?= htmlspecialchars($details['Father\'s Name'] ?? 'N/A') ?></td><th>Mother's Name</th><td><?= htmlspecialchars($details['Mothers Name'] ?? 'N/A') ?></td></tr>
                        <tr><th>Course Name</th><td colspan="3" class="text-primary fw-bold"><?= htmlspecialchars($result_data['class_name']) ?></td></tr>
                        <tr><th>Institute</th><td colspan="3"><?= htmlspecialchars($details['Institute'] ?? 'Firstaid & Technology Institute') ?></td></tr>
                        <tr><th>Passing Year</th><td><?= htmlspecialchars($result_data['passing_year']) ?></td><th>GPA</th><td class="fw-bold text-success fs-5"><?= htmlspecialchars($result_data['gpa']) ?></td></tr>
                        <tr><th>Result Status</th><td colspan="3"><span class="badge <?= ($result_data['result_label'] == 'Passed') ? 'bg-success' : 'bg-danger' ?> px-4 py-2 fs-6"><?= htmlspecialchars($result_data['result_label']) ?></span></td></tr>
                    </table>
                </div>

                <div class="row mt-5 pt-4 text-center d-none d-print-flex">
                    <div class="col-4"><div style="border-top: 1px solid #000; padding-top: 5px;">Controller of Examination</div></div>
                    <div class="col-4"></div>
                    <div class="col-4"><div style="border-top: 1px solid #000; padding-top: 5px;">Principal</div></div>
                </div>

                <div class="mt-5 text-center no-print border-top pt-4">
                    <button onclick="window.print()" class="btn btn-dark px-4 py-2 me-2 shadow-sm"><i class="fas fa-print me-1"></i> Print Transcript</button>
                    <a href="view_result" class="btn btn-outline-success px-4 py-2 shadow-sm"><i class="fas fa-search me-1"></i> New Search</a>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </section>

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