<?php
session_start();
if(!isset($_SESSION['user']) || empty($_SESSION['user'])) {
    header("location: ../login.php"); exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<script>
function toggleInputs() {
    const selectElement = document.getElementById("rangeSelect");
    const selectedValue = selectElement.value;

    const customInputs = document.getElementById("customInputs");

    // Hide all input containers initially
    customInputs.style.display = "none";

    // Show the relevant input container based on selection
    if (selectedValue === "custom") {
        customInputs.style.display = "block";
    }
}
</script>
<head>
<title>Transaction Detail</title>
<style type="text/css" media="screen">
table tr td {
    text-align: left;
    padding: 0 15px;
}
 .right-text { text-align: right; }
 <style type="text/css" media="screen">
 table tr td {
     text-align: left;
     padding: 0 15px;
 }

 body {
     font-family: Arial, sans-serif;
     background-color: #f4f6f8;
     padding: 20px;
 }

 table {
     width: 100%;
     border-collapse: collapse;
     background-color: #fff;
     border-radius: 8px;
     overflow: hidden; /* Ensures rounded corners work */
     box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
 }

 thead {
     background-color: #4CAF50;
     color: white;
 }

 th, td {
     padding: 12px 15px;
     text-align: left;
     height: 20px;
 }

 table td:nth-child(1) { width: 100px; }
 table td:nth-child(2) { width: 240px; }


 tbody tr:nth-child(even) {
     background-color: #f9f9f9;
 }

 tbody tr:hover {
     background-color: #f1f1f1;
     transition: background-color 0.3s ease;
 }

 th {
     font-weight: bold;
 }

 /* Responsive table for small screens */
 @media (max-width: 600px) {
     table, thead, tbody, th, td, tr {
         display: block;
         width: 100%;
     }
     thead tr {
         display: none; /* Hide table headers */
     }
     td {
         padding: 10px;
         text-align: right;
         position: relative;
     }
     td::before {
         content: attr(data-label);
         position: absolute;
         left: 10px;
         text-align: left;
         font-weight: bold;
         color: #555;
     }
 }

</style>
</head>
<body>

<?php

$num = $_GET['num'];

echo '<form method="GET">';

echo '<label for="from">Transaction Number:</label>';
echo '<input type="text" name="num" id="num" placeholder="' . $num . '">';
echo '<input type="submit" value="Submit">';
echo '</form>';

$jsonFile = '/mnt/nand/apps/ids/db.json';
$jsonContent = file_get_contents($jsonFile);
$data = json_decode($jsonContent, true);
if (!$data) {
    echo "ID Storage database not configured";
} else {

   if ($data['dbtype'] != 'mysql') {
      echo "ID Storage database type must by MySQL for reporting";
   } else {

        $conn = mysqli_connect($data['server'], $data['user'], $data['passw'], $data['database']);
        if(!$conn) {
            echo 'Connection error: ' . mysqli_connect_error();
        } else {
            //echo "range " . $range;

			$imgdir = '../../../images/ids/';
			$svrimgdir = '/usr/share/apache2/default-site/htdocs/images/ids/';

            $units = "";

            echo "<h2>Transaction Number " . $num . "</h2>";

            //$where = "datetm >= '2025-11-01' AND datetm <= '2025-11-09 23:59:59'";
            $sql = 'SELECT * FROM trans ' . $where . ' WHERE tkt_num = ' . $num . ';';
            $result = $conn->query($sql);
            if($result->num_rows > 0) {
                if($row = $result->fetch_assoc()) {
                    $tkt_num = $row['tkt_num'];
                    $datetime = $row['datetm'];
                    $fpdatetime = $row['fpdatetm'];
                    $veh = $row['truck'];
                    $prod = $row['prod'];
                    $cust = $row['cust'];
                    $job = $row['job'];
                    $gross = $row['gross'];
                    $tare = $row['tare'];
                    $net = $row['net'];
                    $units = $row['units'];

                    $dt_object = DateTime::createFromFormat('Y-m-d H:i:s', $datetime);
		    		$dt_object2 = DateTime::createFromFormat('Y-m-d H:i:s', $fpdatetime);

                    echo '<table><tr><th>Item</th><th>Value</th><th></th></tr>';
                    echo '<tr><td>Date/Time</td><td>' . $dt_object->format('F d, Y h:i:s A') . '</td><td></td></tr>';
                    if($fpdatetime) {
                        echo '<tr><td>First Pass Date/Time</td><td>' . $dt_object2->format('F d, Y h:i:s A') . '</td><td></td></tr>';
                    }
                    echo '<tr><td>Vehicle</td><td>' . $veh . '</td><td></td></tr>';
                    echo '<tr><td>Product</td><td>' . $prod . '</td><td></td></tr>';
                    echo '<tr><td>Customer</td><td>' . $cust . '</td><td></td></tr>';
                    echo '<tr><td>Job</td><td>' . $job . '</td><td></td></tr>';
                    echo '<tr><td>Gross Weight</td><td class="right-text">' . $gross . ' ' . $units .'</td><td></td></tr>';
                    echo '<tr><td>Tare Weight</td><td class="right-text">' . $tare . ' ' . $units .'</td><td></td></tr>';
                    echo '<tr><td>Net Weight</td><td class="right-text">' . $net . ' ' . $units .'</td><td></td></tr>';

                    // $imgfile = sprintf("v%06d_1.jpg", $tkt_num);
					$imgnumstr = sprintf("%06d", $tkt_num);
					$imgfile = $dt_object->format('Y-m-d') . '/v' . $imgnumstr . '_1.jpg';
					$img = $imgdir . $imgfile;
                    //if(file_exists($img)) {
					if(file_exists($svrimgdir . $imgfile)) {
                        echo '<tr><td colspan="3"><br>Transaction image<br>';
                        echo '<a href="' . $img . '"><img src="' . $img .'" width="512" height="384" ></a></td></tr>';
                    }
                    //$imgfile = sprintf("v%06da_1.jpg", $tkt_num);
					$imgfile = $dt_object->format('Y-m-d') . '/v' . $imgnumstr . 'a_1.jpg';
					$img = $imgdir . $imgfile;
                    //if(file_exists($img)) {
					if(file_exists($svrimgdir . $imgfile)) {
                        echo '<tr><td colspan="3"><br>First pass image<br>';
                        echo '<a href="' . $img . '"><img src="' . $img .'" width="512" height="384" ></a></td></tr>';
                    }
                    echo '</table>';
                    echo '<br>';

                }
            }
        }
    }
}

?>
</body>
</html>
