// Customer Modal Functions
function openModal(id) { 
    document.getElementById(id).classList.remove('hidden'); 
    document.getElementById(id).classList.add('flex'); 
}

function closeModal(id) { 
    document.getElementById(id).classList.add('hidden'); 
    document.getElementById(id).classList.remove('flex'); 
}

function resetCustomerForm() {
    document.getElementById('custModalTitle').textContent = 'Add Customer';
    document.getElementById('custAction').value = 'add';
    document.getElementById('custId').value = '';
    document.getElementById('cName').value = '';
    document.getElementById('cPhone').value = '';
    document.getElementById('cEmail').value = '';
    document.getElementById('cAddress').value = '';
    document.getElementById('cCity').value = '';
    document.getElementById('cState').value = '';
    document.getElementById('cPincode').value = '';
    document.getElementById('cGstin').value = '';
    document.getElementById('cStatus').value = 'active';
}

function openAddCustomer() {
    resetCustomerForm();
    openModal('custModal');
}

function editCustomer(c) {
    document.getElementById('custModalTitle').textContent = 'Edit Customer';
    document.getElementById('custAction').value = 'edit';
    document.getElementById('custId').value = c.id;
    document.getElementById('cName').value = c.name;
    document.getElementById('cPhone').value = c.phone || '';
    document.getElementById('cEmail').value = c.email || '';
    document.getElementById('cAddress').value = c.address || '';
    document.getElementById('cCity').value = c.city || '';
    document.getElementById('cState').value = c.state || '';
    document.getElementById('cPincode').value = c.pincode || '';
    document.getElementById('cGstin').value = c.gstin || '';
    document.getElementById('cStatus').value = c.status;
    openModal('custModal');
}

// Initialize event listeners when DOM is ready
document.addEventListener('DOMContentLoaded', function() {
    // Add Customer button
    const addCustomerBtn = document.getElementById('addCustomerBtn');
    if (addCustomerBtn) {
        addCustomerBtn.addEventListener('click', openAddCustomer);
    }
    
    // Close modal buttons
    const custModalCloseBtn = document.getElementById('custModalCloseBtn');
    if (custModalCloseBtn) {
        custModalCloseBtn.addEventListener('click', function() {
            closeModal('custModal');
        });
    }
    
    const custModalCancelBtn = document.getElementById('custModalCancelBtn');
    if (custModalCancelBtn) {
        custModalCancelBtn.addEventListener('click', function() {
            closeModal('custModal');
        });
    }
    
    // Edit buttons
    const editButtons = document.querySelectorAll('.editCustomerBtn');
    editButtons.forEach(btn => {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            const customerData = JSON.parse(this.dataset.customer);
            editCustomer(customerData);
        });
    });
});
