<?php
include("../../classes/manager.class.php");
$m = new Manager();

if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
    $id = $_REQUEST['id'];
    $qry = IngSetup::GetIngSetupById($id);
    if ($qry) {
        echo "<tr id='tr" . $id . "'>";
        
        // Serial Number (SN) Column
        echo "<td class='sn'></td>";

        // Delete Action Column
        echo "<td>";
        echo "<i id='trashing" . $id . "' style='cursor: pointer; font-size: 18px; margin-top: 7px;' class='mdi-action-delete mdi mdi-delete'></i>";
        echo "</td>";

        // Item Name Column
        echo "<td>";
        echo "<input class='form-control' style='width:100%!important;' type='text' readonly value='" . $qry->name . "'>";
        echo "<input type='hidden' name='item[]' id='item' value='" . $qry->id . "'>";
        echo "</td>";

        // Qty Column
        echo "<td>";
        echo "<input style='width:100%!important;' required maxlength='10' onkeypress='return isNumberKey(event)' class='form-control' type='text' name='per_unit[]' id='per_unit'>";
        echo "</td>";

        echo "</tr>";
    }
}
?>

<script>
    // JavaScript to handle row deletion
    $('#trashing<?=$id?>').on('click', function () {
        $(this).closest('tr').remove(); // Closest finds the nearest <tr> ancestor and removes it
        updateSN(); // Update the serial numbers after deletion
    });

    // Function to update the serial numbers
    function updateSN() {
        $('#addNewIngredient tr').each(function(index) {
            $(this).find('.sn').text(index + 1);
        });
    }
</script>
