<?php
include "../../root.class.php";
$db = new db_safeguard();

$jc_no = isset($_GET['jc_no']) ? (int)$_GET['jc_no'] : 0;

if ($jc_no === 0) {
    exit("Invalid jobcard.");
}

$timeline_res = $db->query(
    "jobcard_timeline",
    "SELECT * FROM jobcard_timeline 
     WHERE jobcard_id = $jc_no 
     ORDER BY record_id DESC"
);
?>

<h2>JOB CARD TIMELINE</h2>

<table>
    <tr>
        <th>USER</th>
        <th>TYPE</th>
        <th>SERIAL NO</th>
        <th>DATE TIME</th>
        <th>METERS</th>
    </tr>

<?php
while ($timeline = $timeline_res->fetch_assoc()) {

    $user_id = (int)$timeline['user_id'];
    $user_res = $db->query(
        "users",
        "SELECT username FROM users WHERE record_id = $user_id"
    );
    $user = $user_res ? $user_res->fetch_assoc() : null;
    ?>
    <tr>
        <td><?= $user ? $user['username'] : '—' ?></td>
        <td><?= $timeline['type'] ?></td>
        <td><?= $timeline['serial_number'] ?></td>
        <td><?= $timeline['date_time'] ?></td>
        <td><?= (float)$timeline['meters'] ?></td>
    </tr>
<?php } ?>
</table>

<style>
table {
    width: 100%;
    border-collapse: collapse;
}
th, td {
    border: 2px solid black;
    padding: 8px;
    background: white;
}
th {
    background: #f0f0f0;
}
</style>
