$(document).ready(function() {
    $('#finalSubmit').attr('disabled', true);
    var selectedItems = []; 
    var quantities = []; // Array to store quantities
    var singleCosts = []; // Array to store single costs
    var itemCosts = []; // Array to store item costs

/**********Make Payment****/
        $('#barOrder').click(function() {
            var selectedItemsHtml = $('#selectedItems').html(); // Get the existing selected items HTML

           
            var newSelectionHtml = '';
            var selectionCount = $('#selectedItems p').length + 1; // Get the count of existing selections
            $('.viewItem1').each(function(index) {
                var selectedOption = $(this).find('option:selected');
                if (selectedOption.val() !== "") {
                    var itemID = $(this).val();
                    var itemName = selectedOption.text();
                    var quantity = $('#orderCount' + (index + 1)).val() || 0;
                    var singleCost = parseFloat($('#wrapCost' + (index + 1)).val()) || 0;
                    var itemCost = singleCost * quantity;
                selectedItems.push(itemID); 
                quantities.push(quantity); 
                singleCosts.push(singleCost); 
                itemCosts.push(itemCost); 
                    newSelectionHtml += '<p>Item ' + selectionCount + ': <br/> <span style="font-size:3px" class="itemID">' + itemID + '</span>' + removeDashPart(itemName) + ' - <b>qty:</b> <span class="quantity">' + quantity + '</span> &nbsp;&nbsp; #<span class="singleCost">' + singleCost + '</span> - tt: &#8358;<span class="itemCost">' + itemCost + '</span> <button class="delete-btnBar" data-index="' + selectionCount + '"><i class="fas fa-trash text-danger"></i></button></p>';
                }
            });
            selectedItemsHtml += newSelectionHtml;
            // Update the selected items display
            $('#selectedItems').html(selectedItemsHtml);

if (selectedItems.length > 0) {$('#finalSubmit').prop('disabled', false);
} else {$('#finalSubmit').prop('disabled', true); }

        });
/******************************END*******************************/

        
function updateSelectedData() {
selectedItems = [];quantities = [];singleCosts = [];itemCosts = [];
$('#selectedItems p').each(function() {
        var itemID = $(this).find('.itemID').text();
        var quantity = $(this).find('.quantity').text();
        var singleCost = $(this).find('.singleCost').text();
        var itemCost = $(this).find('.itemCost').text();
        selectedItems.push(itemID);
        quantities.push(quantity);
        singleCosts.push(singleCost);
        itemCosts.push(itemCost);
    });
if (selectedItems.length > 0) {$('#finalSubmit').prop('disabled', false);
    } else {$('#finalSubmit').prop('disabled', true);}
}


 document.getElementById('finalSubmit').addEventListener('click', function(e) {
    e.preventDefault();
  var mergedArrays = mergeDuplicates(selectedItems, quantities, singleCosts, itemCosts);
    // console.log(mergedArrays.itemid);
    // console.log(mergedArrays.quantity);
    // console.log(mergedArrays.singleCost);
    // console.log(mergedArrays.bulkcost);
   if($('#tableNo').val() ==''){ alert('Table Number not Selected');}else{ 
    $.ajax({
        url: 'get_selected_order',
        type: 'POST',
        dataType: 'html',
        data: {mergedArrays:mergedArrays,type:1,serveWine:'wine',tableNumb:$('#tableNo').val(),selCusName:$('#CustomerName').val()},
        success: function(response) {$('#show1').show();$('#finalSubmit').attr('disabled', true);},
        error: function(xhr, status, error) {
            // Handle error response
            console.error(error);
            alert("Error occurred. Please try again later.");
        }
    });
}//end if
});










        // Delete selected item when delete button is clicked
        $(document).on('click', '.delete-btnBar', function() {
            var index = $(this).data('index');
            $('#selectedItems p:nth-child(' + index + ')').remove();
            updateSelectedData();
        });
    });