<?php
include('db_connection.php');

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$serial = $_GET['serial'] ?? '';

$transcript = null;
$status = "INVALID";

/* =========================================================
   VALIDATE SERIAL
========================================================= */
if (!empty($serial)) {

    $stmt = $conn->prepare("
        SELECT 
            t.serial_no,
            t.student_id,
            t.term,
            t.year,
            t.gpa,
            t.class_position,
            t.promotion_status,
            t.verify_url,
            s.name AS student_name
        FROM transcripts t
        JOIN students s ON s.student_id = t.student_id
        WHERE t.serial_no = ?
        LIMIT 1
    ");

    $stmt->bind_param("s", $serial);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows === 1) {
        $transcript = $result->fetch_assoc();
        $status = "VALID";
    }

    $stmt->close();
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Transcript Verification</title>

<style>
body {
    font-family: Arial;
    background: #f1f5f9;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.card {
    background: white;
    padding: 30px;
    border-radius: 12px;
    width: 420px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    text-align: center;
}

/* STATUS BADGES */
.valid {
    color: #16a34a;
    font-size: 26px;
    font-weight: bold;
}

.invalid {
    color: #dc2626;
    font-size: 26px;
    font-weight: bold;
}

.box {
    margin-top: 20px;
    text-align: left;
    border-top: 1px solid #ddd;
    padding-top: 15px;
}

.row {
    margin-bottom: 8px;
}

.label {
    font-weight: bold;
    color: #334155;
}

.value {
    color: #0f172a;
}
</style>
</head>

<body>

<div class="card">

<?php if ($status === "VALID"): ?>

    <div class="valid">✔ VALID TRANSCRIPT</div>

    <div class="box">

        <div class="row">
            <span class="label">Student:</span>
            <span class="value"><?= htmlspecialchars($transcript['student_name']) ?></span>
        </div>

        <div class="row">
            <span class="label">Serial:</span>
            <span class="value"><?= htmlspecialchars($transcript['serial_no']) ?></span>
        </div>

        <div class="row">
            <span class="label">Term:</span>
            <span class="value"><?= htmlspecialchars($transcript['term']) ?></span>
        </div>

        <div class="row">
            <span class="label">Year:</span>
            <span class="value"><?= htmlspecialchars($transcript['year']) ?></span>
        </div>

        <div class="row">
            <span class="label">GPA:</span>
            <span class="value"><?= htmlspecialchars($transcript['gpa']) ?></span>
        </div>

        <div class="row">
            <span class="label">Position:</span>
            <span class="value"><?= htmlspecialchars($transcript['class_position']) ?></span>
        </div>

        <div class="row">
            <span class="label">Status:</span>
            <span class="value"><?= htmlspecialchars($transcript['promotion_status']) ?></span>
        </div>

    </div>

<?php else: ?>

    <div class="invalid">✖ INVALID TRANSCRIPT</div>
    <p style="margin-top:10px; color:#64748b;">
        This transcript is not found or has been tampered with.
    </p>

<?php endif; ?>

</div>

</body>
</html>