<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$trandb = "shiptran";
if ($_REQUEST["vouchno"]) {
    $nocart = $_REQUEST["nocart"];
    $vouchno = $_REQUEST["vouchno"];
    $par = $_REQUEST["par"] ?? 0;

    // Output par value for JS to read
    $mctr = 0;
    $ctr = 0;
    $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$vouchno' ORDER BY srno") or die(mysqli_error($conn));

    if (mysqli_num_rows($qry) > 0) {
        $tcart = $nocart - mysqli_num_rows($qry);
        while ($row = mysqli_fetch_array($qry)) {
            $mctr++;
            echo "<tr>";
            echo "<td><i class='fa fa-close remove'></i></td>";

            echo "<td><input type='text' name='srno_$ctr' class='srno form-control text-right formins'  value='$mctr' readonly tabindex='-1'></td>";
            echo "<td><input type='number' step='any' name='len_$ctr' class='len form-control text-right formins' value='$row[len]' ></td>";
            echo "<td><input type='number' step='any' name='brea_$ctr' class='brea form-control text-right formins' value='$row[brea]' ></td>";
            echo "<td><input type='number' step='any' name='heig_$ctr' class='heig form-control text-right formins' value='$row[heig]' ></td>";
            echo "<td><input type='number' step='any' name='cartcount_$ctr' class='cartcount form-control text-right formins' value='$row[cartcount]' ></td>";
            echo "<td><input type='number' step='any' name='cbm_$ctr' class='cbm form-control text-right formins' value='$row[cbm]' readonly   tabindex='-1'></td>";
            echo "</tr>";
            $ctr++;
        }
    } else {
        $mctr = 0;
        for ($i = 0; $i < 1; $i++) {

            $mctr++;
            echo "<tr>";
            echo "<td><i class='fa fa-close remove'></i></td>";

            echo "<td><input type='text' name='srno_$ctr' class='srno form-control text-right formins'  value='$mctr' readonly tabindex='-1'></td>";
            echo "<td><input type='number' step='any' name='len_$ctr' class='len form-control text-right formins' value='0' ></td>";
            echo "<td><input type='number' step='any' name='brea_$ctr' class='brea form-control text-right formins' value='0' ></td>";
            echo "<td><input type='number' step='any' name='heig_$ctr' class='heig form-control text-right formins' value='0' ></td>";
            echo "<td><input type='number' step='any' name='cartcount_$ctr' class='cartcount form-control text-right formins' value='1' ></td>";
            echo "<td><input type='number' step='any' name='cbm_$ctr' class='cbm form-control text-right formins' value='0' readonly tabindex='-1'></td>";
            echo "</tr>";
            $ctr++;
        }
    }
    

    echo "<input type='hidden' name='items' id='items' value='$ctr'>";
    echo "<input type='hidden' id='par' value='$par'>";
    ?>

    <script type="text/javascript">
        $(document).ready(function () {
            var par = $("#par").val();
            if (par == "1") {
                // Make all input fields with class 'form' readonly, except cbm and srno (already readonly)
                $(".formins").prop("readonly", true);
            }
        });
    </script>

    <?php

} else {
    echo "ERROR";
}
?>
 