<script>
	// Finalized AJAX Search Logic for Dent/Paint (Following Your Pattern Exactly)

	function bindDentSearch(searchInput, resultContainer) {
		searchInput.addEventListener("input", function() {
			let query = searchInput.value.trim();
			if (query.length > 0) {
				fetch(`./php/fetchDentPaint.php?query=${encodeURIComponent(query)}`)
					.then(response => response.json())
					.then(data => {
						resultContainer.innerHTML = "";
						resultContainer.style.display = "block";

						if (data.length === 0) {
							resultContainer.innerHTML = "<div>No matching records found</div>";
							return;
						}

						data.forEach(dent => {
							let div = document.createElement("div");
							div.textContent = dent.dentType;
							div.dataset.dentID = dent.dentID;
							div.dataset.dentCharge = dent.dentCharge;

							div.addEventListener("click", function() {
								searchInput.value = dent.dentType;
								resultContainer.style.display = "none";

								const row = searchInput.closest(".row");
								const quantityField = row.querySelector(".dentQuantity");
								const totalPriceField = row.querySelector(".dentTotal");
								const netAmountField = row.querySelector(".dentNetAmount");
								const discountField = row.querySelector(".dentDiscount");

								quantityField.value = 1;
								totalPriceField.value = parseFloat(dent.dentCharge).toFixed(2);

								function updateNetAmount() {
									let quantity = parseFloat(quantityField.value) || 1;
									let unitPrice = parseFloat(dent.dentCharge) || 0;
									let discount = parseFloat(discountField.value) || 0;

									let totalBeforeDiscount = quantity * unitPrice;
									let netAmount = totalBeforeDiscount - (totalBeforeDiscount * discount / 100);

									netAmountField.value = netAmount.toFixed(2);
									updateTotalDentAjaxNetAmount();
								}

								quantityField.addEventListener("input", updateNetAmount);
								discountField.addEventListener("input", updateNetAmount);

								updateNetAmount();
							});

							resultContainer.appendChild(div);
						});
					})
					.catch(error => console.error("Error fetching dent/paint details:", error));
			} else {
				resultContainer.style.display = "none";
			}
		});
	}

	// Also add global click handler here (once per page)
	document.addEventListener("click", function(e) {
		document.querySelectorAll(".dentSearchResults").forEach(resultContainer => {
			const input = resultContainer.previousElementSibling;
			if (!resultContainer.contains(e.target) && !input.contains(e.target)) {
				resultContainer.style.display = "none";
			}
		});
	});


	document.addEventListener("DOMContentLoaded", function() {
		document.querySelectorAll(".mainDentPaintRow").forEach(row => {
			const searchInput = row.querySelector(".dentTypeSearch");
			const resultContainer = row.querySelector(".dentSearchResults");
			if (searchInput && resultContainer) {
				bindDentSearch(searchInput, resultContainer);
			}
		});
	});

	function updateTotalDentAjaxNetAmount() {
		let total = 0;
		document.querySelectorAll(".dentNetAmount").forEach(input => {
			total += parseFloat(input.value) || 0;
		});
		document.getElementById("totalDentAjax").value = total.toFixed(2);

		if (typeof updateFinalDentBillTotal === "function") {
			updateFinalDentBillTotal();
		}
	}
</script>