🔍 Vendor Product Search

Search Products!

🔍
Loading product count...
Thumanns M&V Westside Driscoll
Searching products...

Welcome to Vendor Product Search!

Start typing to search across all vendor catalogs

💡 Search Tips:

  • Item Codes: Try "12345" or "ABC123"
  • Descriptions: Try "chicken", "beef", "cheese"
  • Multi-word: Try "120 american" to find products with both words
  • Categories: Try "meat", "dairy", "produce"
  • Browse All: Select a vendor and type "*" to see all their products
  • Vendors: Results show which vendor carries each item
if (query.length === 0) { showEmptyState(); return; } // Special case: * wildcard with vendor filter shows all products for that vendor if (query === '*' && dcFilter.value) { resultCount.textContent = 'Loading all products for ' + dcFilter.value + '...'; debounceTimer = setTimeout(() => { currentPage = 1; // Reset pagination for new searches performSearch('*', 1); }, 300); return; } if (query.length < 2 && query !== '*') { resultCount.textContent = 'Type at least 2 characters to search (or * to show all for selected vendor)'; return; } resultCount.textContent = 'Searching...'; // Longer delay to ensure full input is captured debounceTimer = setTimeout(() => { // Double-check the current value in case it changed const currentQuery = searchBox.value.trim(); if (currentQuery.length >= 2) { currentPage = 1; // Reset pagination for new searches performSearch(currentQuery, 1); } }, 600); }); // Also handle Enter key for immediate search searchBox.addEventListener('keydown', function(e) { if (e.key === 'Enter') { clearTimeout(debounceTimer); const query = this.value.trim(); if (query.length >= 2 || query === '*') { performSearch(query); } } }); // Handle filter and sort changes dcFilter.addEventListener('change', function() { const query = searchBox.value.trim(); if (query.length >= 2 || query === '*') { performSearch(query); } }); sortBy.addEventListener('change', function() { const query = searchBox.value.trim(); if (query.length >= 2 || query === '*') { performSearch(query); } }); sortOrder.addEventListener('change', function() { const query = searchBox.value.trim(); if (query.length >= 2 || query === '*') { performSearch(query); } }); // Pagination event handlers prevPageBtn.addEventListener('click', function() { if (currentPage > 1) { const query = searchBox.value.trim(); performSearch(query, currentPage - 1); } }); nextPageBtn.addEventListener('click', function() { if (currentPage < totalPages) { const query = searchBox.value.trim(); performSearch(query, currentPage + 1); } }); // Enhanced search function with better error handling async function performSearch(query, page = 1) { // Prevent multiple simultaneous searches if (isSearching) { console.log('Search already in progress, skipping'); return; } console.log('Performing search for:', query, 'page:', page); isSearching = true; showLoading(); try { // Parse advanced query const parsedQuery = parseAdvancedQuery(query); console.log('Ajax search - Query:', query); console.log('Ajax search - Parsed query:', parsedQuery); // Build URL with all parameters const params = new URLSearchParams({ q: query, sort: sortBy.value, order: sortOrder.value, advanced: JSON.stringify(parsedQuery) }); if (dcFilter.value) { params.append('dc', dcFilter.value); } // Add pagination for wildcard searches if (query === '*') { params.append('page', page); currentPage = page; } const url = 'ajax_search.php?' + params.toString(); console.log('Fetching:', url); const response = await fetch(url, { method: 'GET', headers: { 'Cache-Control': 'no-cache' } }); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } const data = await response.text(); console.log('Search response received, length:', data.length); hideLoading(); if (data.includes('No results found') || data.trim() === 'No results found.') { showNoResults(query); } else if (data.includes('

Welcome to Vendor Product Search!

Start typing to search across all vendor catalogs

💡 Search Tips:

`; } function showResults(data, query) { // Check for pagination info const paginationMatch = data.match(/
/); if (paginationMatch) { // Wildcard search with pagination currentPage = parseInt(paginationMatch[1]); totalPages = parseInt(paginationMatch[2]); const totalResults = parseInt(paginationMatch[3]); resultCount.textContent = `Browsing ${dcFilter.value} products (${totalResults} total)`; // Show pagination controls pagination.style.display = 'block'; pageInfo.textContent = `Page ${currentPage} of ${totalPages}`; prevPageBtn.disabled = currentPage <= 1; nextPageBtn.disabled = currentPage >= totalPages; // Remove pagination info from data data = data.replace(/
/s, ''); } else { // Regular search - no pagination const rowCount = (data.match(//g) || []).length - 1; // -1 for header row let countText = `Found ${rowCount} result${rowCount !== 1 ? 's' : ''} for "${query}"`; if (dcFilter.value) { countText += ` in ${dcFilter.value}`; } resultCount.textContent = countText; // Hide pagination controls pagination.style.display = 'none'; } // Enhanced table formatting with proper vendor styling let enhancedData = data.replace(//, '
'); // Format prices enhancedData = enhancedData.replace(/'); // Style vendor cells properly (target the vendor column specifically) // Handle both HTML encoded and plain text versions - show only first word for cleaner interface enhancedData = enhancedData.replace(/'); enhancedData = enhancedData.replace(/'); enhancedData = enhancedData.replace(/'); enhancedData = enhancedData.replace(/'); enhancedData = enhancedData.replace(/'); console.log('Enhanced data sample:', enhancedData.substring(0, 500)); results.innerHTML = enhancedData; } function showNoResults(query) { resultCount.textContent = `No results found for "${query}"`; results.innerHTML = `

🔍 No Results Found

We couldn't find any products matching "${query}"

Try these suggestions:

  • Check your spelling
  • Try shorter or more general terms
  • Search for categories like "meat", "dairy", or "produce"
  • Try partial item codes or descriptions
`; } function showError(error) { resultCount.textContent = 'Search error occurred'; results.innerHTML = `

⚠️ Search Error

There was a problem searching the database.

Error: ${error.message}

`; } // Load dynamic product count and focus search box on page load window.addEventListener('load', () => { loadProductCount(); searchBox.focus(); }); // Function to load current product count from database function loadProductCount() { fetch('get_product_count.php') .then(response => response.json()) .then(data => { const count = data.formatted || '17,796'; const total = data.total || 17796; // Update header const headerElement = document.getElementById('productCountHeader'); if (headerElement) { headerElement.textContent = `Search ${count} Products!`; } // Update result count resultCount.textContent = `Ready to search ${count}+ products`; // Update page title if desired document.title = `Vendor Product Search - ${count} Products - Buddy Boy Provisions`; }) .catch(error => { console.error('Error loading product count:', error); // Fallback to current count const headerElement = document.getElementById('productCountHeader'); if (headerElement) { headerElement.textContent = 'Search 17,796 Products!'; } resultCount.textContent = 'Ready to search 17,796+ products'; }); } // Keyboard shortcuts document.addEventListener('keydown', (e) => { // Focus search box with Ctrl+F or / if ((e.ctrlKey && e.key === 'f') || e.key === '/') { e.preventDefault(); searchBox.focus(); searchBox.select(); } // Clear search with Escape if (e.key === 'Escape' && document.activeElement === searchBox) { searchBox.value = ''; showEmptyState(); } });
\$(\d+\.?\d*)<\/td>/g, '$$$1Thumanns<\/td>/g, 'ThumannsM&V Provisions<\/td>/g, 'M&VM&V Provisions<\/td>/g, 'M&VWestside Foods<\/td>/g, 'WestsideDriscoll Foods<\/td>/g, 'Driscoll