<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);

// ✅ Guard prevents "session already started" warnings
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

include "../../../root.class.php";
$db = new db_safeguard();

if (empty($_POST['jobcard_no'])) {
    echo '<p style="color:white; text-align:center;">NO JOBCARD NO FOUND</p>';
    exit;
}

$jobcard_no = $_POST['jobcard_no'];

$res = $db->query(
    "notes",
    "SELECT * FROM notes 
     WHERE jobcard_id = '{$jobcard_no}' 
     AND reason = 'INSTALL IMAGE' 
     ORDER BY record_id DESC"
);

if ($res->num_rows === 0) {
    echo '<p style="color:white; text-align:center;">NO IMAGES FOUND</p>';
    exit;
}

while ($row = $res->fetch_assoc()) {

    // ✅ Skip rows with no image filename
    if (empty($row['image'])) continue;

    $record_id = (int) $row['record_id'];
    $image     = htmlspecialchars($row['image']);
    $note      = htmlspecialchars($row['note']);
    $date_time = htmlspecialchars($row['date_time']);
    $img_path  = '/app/jobcards/pump/install_images/' . $image;

    echo "
        <div class='install_image_card' style='display:flex; flex-direction:column; align-items:center; 
                    background:rgba(0,0,0,0.1); border-radius:8px; padding:2vw;'>

            <img 
                src='{$img_path}' 
                style='width:100%; border-radius:8px; border:2px solid white; cursor:pointer;'
                onclick=\"openLightbox('{$img_path}')\"
            /><br>

            <label style='color:black; font-size:2em;'>DESCRIPTION</label>
            <textarea 
                style='color:black; margin-top:8px; font-size:3em; text-align:center; width:100%;' 
                class='inputs' cols='70' rows='3' 
                readonly>{$note}</textarea>

            <p style='color:black; font-size:3em; margin-top:8px;'>{$date_time}</p>

            <button 
                class='submit_btn' 
                style='max-width: none;'
                onclick=\"deleteInstallImage({$record_id}, this)\">
                🗑 Delete Image
            </button>

        </div>
    ";
}
?>