<?php
include "../../root.class.php";
$db = new db_safeguard();

$team_id = isset($_GET['team_id']) ? (int) $_GET['team_id'] : 0;

if ($team_id === 0) {
    exit;
}

$res = $db->query(
    "jobcards",
    "SELECT * 
     FROM jobcards 
     WHERE status = 0 AND team_assigned_id = $team_id
     ORDER BY jc_no DESC"
);

// echo "SELECT * 
//      FROM jobcards 
//      WHERE status = 0 AND team_assigned_id = $team_id
//      ORDER BY jc_no DESC";


if ($res->num_rows > 0) {

    $data_res = $res->fetch_assoc();

    // echo "SELECT * FROM jobcard_timeline WHERE jobcard_id = $data_res[record_id]";

    $timeline_res = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id = {$data_res['record_id']} ORDER BY record_id DESC LIMIT 1");
    $timeline = $timeline_res->fetch_assoc();

    if ($timeline['status'] == 1) {
        echo "<em>No active jobcards</em>";
        exit;
    } else {
        ?>
        <div class="jobcard_item">
            <strong>Jobcard #<?= (int) $data_res['jc_no']; ?></strong><br>
            <?= htmlspecialchars($data_res['address']); ?><br>
            <small><?= $jc['action_date']; ?></small>

            <button onclick="openDetails(<?= (int) $data_res['jc_no']; ?>)">
                VIEW DETAILS
            </button>
        </div>
        <?php
    }
} else {
    echo "<em>No active jobcards</em>";
    exit;
}

// if ($res->num_rows === 0) {
//     echo "<em>No active jobcards</em>";
//     exit;
// }

// while ($jc = $res->fetch_assoc()) {
//     ?>
<!-- //     <div class="jobcard_item">
//         <strong>Jobcard #<= (int) $jc['jc_no']; ?></strong><br>
//         <= htmlspecialchars($jc['address']); ?><br>
//         <small><= $jc['action_date']; ?></small>

//         <button onclick="openDetails(<= (int) $jc['jc_no']; ?>)">
//             VIEW DETAILS
//         </button>
//     </div> -->
<!-- <?php
//  }
?> -->