// Main JavaScript file for Kamani Fish Farms document.addEventListener('DOMContentLoaded', function() { // Mobile navigation toggle const navToggle = document.querySelector('.nav-toggle'); const mainNav = document.getElementById('main-nav'); if (navToggle) { navToggle.addEventListener('click', function() { mainNav.classList.toggle('active'); }); } // Format currency for all elements with class 'currency' function formatCurrency(amount) { if (isNaN(amount)) return "₹0.00"; return new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR', minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(amount); } document.querySelectorAll('.currency').forEach(function(element) { const amount = parseFloat(element.textContent); if (!isNaN(amount)) { element.textContent = formatCurrency(amount); } }); // Confirm delete actions document.querySelectorAll('.delete-confirm').forEach(function(link) { link.addEventListener('click', function(e) { if (!confirm('Are you sure you want to delete this item?')) { e.preventDefault(); } }); }); // Make tables responsive document.querySelectorAll('table.data-table').forEach(function(table) { if (!table.parentElement.classList.contains('table-responsive')) { const wrapper = document.createElement('div'); wrapper.className = 'table-responsive'; table.parentNode.insertBefore(wrapper, table); wrapper.appendChild(table); } }); // Dynamic form calculations (for sales form) const salesForm = document.getElementById('sales-form'); if (salesForm) { const weightTilapia = document.getElementById('weight_tilapia'); const weightSmallFish = document.getElementById('weight_small_fish'); const weightBigFish = document.getElementById('weight_big_fish'); const includeDelivery = document.getElementById('include_delivery'); const finalAmount = document.getElementById('final_amount'); const finalAmountOverride = document.getElementById('final_amount_override'); const totalAmountDisplay = document.getElementById('total-amount'); function calculateFinalAmount() { // This is a simplified calculation. The actual calculation // is handled by the application logic with buyer rates. let total = (parseFloat(weightTilapia.value) || 0) + (parseFloat(weightSmallFish.value) || 0) + (parseFloat(weightBigFish.value) || 0); if (includeDelivery.checked) { total += 100; // Assuming a flat delivery fee of 100 } finalAmount.value = total.toFixed(2); if (totalAmountDisplay) { totalAmountDisplay.textContent = total.toFixed(2); } } [weightTilapia, weightSmallFish, weightBigFish, includeDelivery].forEach(function(element) { if (element) { element.addEventListener('input', calculateFinalAmount); } }); if (finalAmountOverride) { finalAmountOverride.addEventListener('input', function() { if (this.value) { const overrideValue = parseFloat(this.value); finalAmount.value = overrideValue.toFixed(2); if (totalAmountDisplay) { totalAmountDisplay.textContent = overrideValue.toFixed(2); } } else { calculateFinalAmount(); } }); } } // Handle date fields - set default to today if empty document.querySelectorAll('input[type="date"]').forEach(function(dateInput) { if (!dateInput.value) { const today = new Date(); const year = today.getFullYear(); const month = String(today.getMonth() + 1).padStart(2, '0'); const day = String(today.getDate()).padStart(2, '0'); dateInput.value = `${year}-${month}-${day}`; } }); // Improve touch experience for form elements on mobile if (window.innerWidth <= 768) { document.querySelectorAll('select, input[type="number"]').forEach(function(element) { element.addEventListener('touchstart', function(e) { e.target.focus(); }); }); } // Auto-hide flash messages after 5 seconds const flashMessages = document.querySelectorAll('.flash-message'); if (flashMessages.length > 0) { setTimeout(function() { flashMessages.forEach(function(message) { message.style.opacity = '0'; message.style.transition = 'opacity 0.5s ease'; setTimeout(function() { message.style.display = 'none'; }, 500); }); }, 5000); } });