<script>
    document.addEventListener("DOMContentLoaded", function() {
        let rowCount = 0;

        function calculateLabourNetAmount(row) {
            let amount = parseFloat(row.querySelector(".customLabourAmount").value.trim()) || 0;
            let quantity = parseFloat(row.querySelector(".customLabourQuantity").value.trim()) || 1;
            let discount = parseFloat(row.querySelector(".customLabourDiscount").value.trim()) || 0;
            discount = Math.min(100, Math.max(0, discount));

            let totalAmount = amount * quantity;
            let netAmount = totalAmount - (totalAmount * discount / 100);
            row.querySelector(".customLabourNetAmount").value = netAmount.toFixed(2);
            updateTotalCustomLabourNetAmount();
            updateFinalDentBillTotal();
        }

        function updateTotalCustomLabourNetAmount() {
            let totalCustomLabour = 0;
            document.querySelectorAll(".customLabourNetAmount").forEach(input => {
                totalCustomLabour += parseFloat(input.value) || 0;
            });
            document.getElementById("totalCustomLabour").value = totalCustomLabour.toFixed(2);
            updateFinalDentBillTotal();
        }

        function addEventListenersToLabourRow(row) {
            row.querySelector(".customLabourAmount").addEventListener("input", function() {
                calculateLabourNetAmount(row);
            });
            row.querySelector(".customLabourQuantity").addEventListener("input", function() {
                calculateLabourNetAmount(row);
            });
            row.querySelector(".customLabourDiscount").addEventListener("input", function() {
                calculateLabourNetAmount(row);
            });
            row.querySelector(".deleteLabourRow").addEventListener("click", function() {
                if (document.querySelectorAll(".labourRow").length > 1) {
                    row.remove();
                    rowCount--;
                    updateTotalCustomLabourNetAmount();
                } else {
                    alert("At least one row is required!");
                }
            });
        }

        document.addEventListener("DOMContentLoaded", function() {
            if (!window.labourHandlerAttached) {
                window.labourHandlerAttached = true;

                document.getElementById("addLabourRow").addEventListener("click", function() {
                    if (rowCount < 100) { // Adjust this limit as needed
                        let container = document.getElementById("labourContainer");
                        let originalRow = container.querySelector(".labourRow");

                        if (originalRow) {
                            let newRow = originalRow.cloneNode(true);
                            newRow.querySelectorAll("input").forEach(input => {
                                input.value = input.type === "text" ? "" : "0";
                            });

                            container.appendChild(newRow);
                            addEventListenersToLabourRow(newRow);

                            rowCount++; // Increment the row count when a new row is added
                        }
                    } else {
                        alert("You can add a maximum of 100 rows."); // Adjust message accordingly
                    }
                });
            }
        });


        function updateFinalDentBillTotal() {
            let totalCustomLabour = parseFloat(document.getElementById("totalCustomLabour").value) || 0;
            let totalLabourAjax = parseFloat(document.getElementById("totalLabourAjax").value) || 0;
            let finalLabourBillAmount = totalCustomLabour + totalLabourAjax;
            document.querySelector("[name='labourBillAmount']").value = finalLabourBillAmount.toFixed(2);
        }

        let initialRow = document.querySelector(".labourRow");
        if (initialRow) addEventListenersToLabourRow(initialRow);
    });
</script>