<?php
include "../db.php";
$id = intval($_GET['id'] ?? 0);
$row = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM student_bill WHERE sb_id = $id"));
if (!$row) { echo "Bill not found."; exit; }

/* --- OPTIONAL: reuse your existing helper queries
         to turn IDs into names exactly as in the table --- */

function getVal($q) {
    global $con; $r = mysqli_fetch_assoc(mysqli_query($con,$q));
    return $r ? array_values($r)[0] : '';
}

$student = getVal("SELECT uname FROM users WHERE uid = '{$row['sb_for']}'");
$course  = getVal("SELECT course_name FROM clg_course WHERE sl_no =
                   (SELECT product FROM product WHERE pid = '{$row['sb_course']}')");
$dept    = getVal("SELECT cat_name FROM clg_course_cat WHERE sl_no = '{$row['sb_dept']}'");
$college = getVal("SELECT uname FROM users WHERE uid = '{$row['sb_college']}'");
?>

<table class="table table-sm table-bordered">
    <tr>
        <th>Invoice</th>
        <td><?= $row['sb_invoice'] ?></td>
    </tr>
    <tr>
        <th>Student</th>
        <td><?= $student ?></td>
    </tr>
    <tr>
        <th>Course</th>
        <td><?= $course ?> (<?= $dept ?>)</td>
    </tr>
    <tr>
        <th>College</th>
        <td><?= $college ?></td>
    </tr>
    <tr>
        <th>Amount</th>
        <td>₹<?= number_format($row['sb_amount'], 2) ?></td>
    </tr>
    <tr>
        <th>Date</th>
        <td><?= $row['sb_date'] ?></td>
    </tr>
    <?php if(!empty($row["sb_remark"])){?>
    <tr>
        <th>Remark</th>
        <td><?= $row['sb_remark'] ?></td>
    </tr>
    <tr>
        <th>Description</th>
        <td><?= $row['sb_description'] ?></td>
    </tr>
    <tr>
        <th>Purpuse</th>
        <td><?= $row['sb_purpus'] ?></td>
    </tr>
    <?php }?>
</table>
<?php if(!empty($row["sb_file"])){?>
<img src="../upload/payout/<?= $row['sb_file']?>" alt="">
<?php }?>