$(document).ready(function () {

    $("body").on("change", '.expense', getItemDt);
    $("body").on("change", '.expense', getItemStk);
    $("body").on("change", '.expense', getOrdersList);
    $("body").on("change", '.expense', getVendorList);

    getOrdersList();
    
    function getOrdersList() {
        var item = $(".expense").val(),
            vouchdt = $(".vouchdt").val();
        
        $.post('ShowOrderList.php', { item: item, vouchdt: vouchdt }, function (data) {
            $(".tbodyy").html(data);
        });
    }
  
  
    function getVendorList() {
        var item = $(".expense").val();

        $.post('ShowVendorList.php', { item: item }, function (data) {
            $(".vendor").html(data);
        });
    }

    getItemDt();

    function getItemDt() {
        var item = $(".expense").val();

        $.post('getItemDt.php', { item: item }, function (data) {
            $(".txtunit").text(data);
            $(".unit").val(data);
        });
    }
    

    $(".showstock").hide();  // Hide div if boat number doesn't exist
    function getItemStk() {
        var item = $(".expense").val(),
            vouchdt = $(".vouchdt").val();
            
        if (item > 0) {

            $.post('getItemStk.php', { item: item, vouchdt: vouchdt }, function (data) {

                // Parse the JSON response
                var response = JSON.parse(data);

                $(".stkqty").text(response.stkqty);
                $(".sunit").text(response.unit);

                // Show or hide the boat number div based on whether the boat number exists
                if (response.stkqtyExists) {
                    $(".showstock").show();  // Show div if boat number exists
                } else {
                    $(".showstock").hide();  // Hide div if boat number doesn't exist
                }

            });
        }
    }

});