/** * Ayula Store POS System * Main JavaScript file for transaction page functionality */ $(document).ready(function() { // Initialize globals initializeGlobals(); // Setup event handlers setupEventHandlers(); // Initialize UI components initializeUI(); // Check and update the "Clear All" button state on page load updateClearCartButtonInitialState(); }); /** * Initialize global variables and state */ function initializeGlobals() { // Variables to track selected products window.selectedProducts = []; window.multiSelectToolbar = $('.multi-select-toolbar'); // Store product stock information window.productStock = {}; $('.productset').each(function() { const productId = $(this).data('product-id'); const stockText = $(this).find('.productsetimg h6').text(); const stockValue = parseInt(stockText.replace('Stok: ', '')) || 0; window.productStock[productId] = stockValue; }); } /** * Check and update the "Clear All" button state on page load */ function updateClearCartButtonInitialState() { const cartItemsCount = $('.product-lists').length; console.log('Initial cart items count:', cartItemsCount); // Get the clear cart button const clearCartBtn = $('#clear-cart-btn'); if (cartItemsCount > 0) { // Enable the clear cart button clearCartBtn.attr('href', 'javascript:void(0);') .removeClass('disabled') .css({ 'opacity': '1', 'cursor': 'pointer' }); console.log('Clear cart button initially enabled'); } else { // Disable the clear cart button clearCartBtn.attr('href', '#') .addClass('disabled') .css({ 'opacity': '0.5', 'cursor': 'not-allowed' }); console.log('Clear cart button initially disabled'); } } /** * Setup all event handlers for the page */ function setupEventHandlers() { // Product card selection setupProductSelection(); // Toolbar actions setupToolbarActions(); } /** * Initialize UI components */ function initializeUI() { // Hide all check marks initially $('.check-product i').hide(); // Trigger toggle button click to activate it when page loads setTimeout(function() { $("#toggle_btn").trigger('click'); }, 100); // Focus search field when search icon is clicked $('.responsive-search').on('click', function() { setTimeout(function() { $('input[name="search"]').focus(); }, 100); }); // Focus the page header search field on click $('.product-search-form input[name="search"]').on('click', function() { $(this).focus(); }); // Enable live search functionality setupLiveSearch(); // Enable AJAX category navigation setupCategoryNavigation(); // Initialize cash payment functionality setupCashPayment(); } /** * Setup product selection functionality */ function setupProductSelection() { // Click on product card to toggle selection $('.productset').on('click', function(e) { // Only handle clicks on the card itself, not buttons or links inside if ($(e.target).closest('button, a, form').length === 0) { const productId = $(this).data('product-id'); const checkbox = $(this).find('.product-checkbox'); const isChecked = checkbox.prop('checked'); console.log('Clicked product ID:', productId); // Toggle checkbox checkbox.prop('checked', !isChecked); if (!isChecked) { // Add to selected list and highlight window.selectedProducts.push(productId); $(this).addClass('selected'); $(this).find('.check-product i').show(); } else { // Remove from selected list and unhighlight window.selectedProducts = window.selectedProducts.filter(id => id !== productId); $(this).removeClass('selected'); $(this).find('.check-product i').hide(); } console.log('Selected products:', window.selectedProducts); updateToolbar(); } }); } /** * Setup toolbar action buttons */ function setupToolbarActions() { // Cancel selection button $('#cancel-selection').on('click', function() { // Uncheck all checkboxes and hide check marks $('.product-checkbox').prop('checked', false); $('.productset').removeClass('selected'); $('.check-product i').hide(); window.selectedProducts = []; updateToolbar(); }); // Add selected products to cart $('#add-selected-to-cart').on('click', function() { if (window.selectedProducts.length > 0) { console.log('Adding products to cart:', window.selectedProducts); // Create a form to submit selected products const form = $('