frappe.pages['ebay_pricing'].on_page_load = function(wrapper) { var page = frappe.ui.make_app_page({ parent: wrapper, title: __('eBay Pricing'), single_column: true }); let $container = $(`

Search for a model or run batch pricing

`).appendTo(page.main); $container.find('#ebay-search-btn').on('click', function() { let query = $container.find('#ebay-search-input').val().trim(); if (!query) { frappe.msgprint(__('Enter a model to search')); return; } search_ebay(query); }); $container.find('#ebay-search-input').on('keypress', function(e) { if (e.which === 13) { $container.find('#ebay-search-btn').trigger('click'); } }); $container.find('#ebay-batch-btn').on('click', function() { let size = $container.find('#ebay-batch-size').val(); run_batch(size); }); function search_ebay(query) { frappe.call({ method: 'westech_r2.api.ebay_pricing.search_model', args: { query: query }, freeze: true, freeze_message: __('Searching eBay sold listings...'), callback: function(r) { if (r.message && r.message.results) { render_results(r.message); } else { let msg = (r.message && r.message.message) || __('No results found'); frappe.msgprint(msg); } } }); } function run_batch(size) { frappe.call({ method: 'westech_r2.api.ebay_pricing.run_batch', args: { batch_size: size }, freeze: true, freeze_message: __('Running batch pricing...'), callback: function(r) { if (r.message) { frappe.msgprint(__('Batch complete: {0} priced, {1} failed, {2} skipped', [r.message.priced, r.message.failed, r.message.skipped])); load_recent_pricing(); } } }); } function render_results(data) { let $area = $container.find('#ebay-results-area').empty(); if (!data.results || !data.results.length) { $area.html(`
No results
`); return; } let html = ``; data.results.forEach(item => { html += ``; }); html += `
Title Price Condition Sold Shipping
${frappe.utils.escape_html(item.title || '')} $${(item.price || 0).toFixed(2)} ${frappe.utils.escape_html(item.condition || '')} ${item.sold || ''} ${item.shipping || ''}
`; if (data.pricing) { html += `

Pricing Summary

Low: $${data.pricing.price_low}
High: $${data.pricing.price_high}
Average: $${data.pricing.price_average}
Median: $${data.pricing.price_auction}
Source: ${data.pricing.source}
Samples: ${data.pricing.sample_count}
`; } $area.html(html); } function load_recent_pricing() { frappe.call({ method: 'westech_r2.api.ebay_pricing.get_recent_pricing', args: { limit: 50 }, callback: function(r) { if (r.message) { render_pricing_grid(r.message); } } }); } function render_pricing_grid(items) { let $area = $container.find('#ebay-results-area'); if (!items || !items.length) { $area.html(`
No pricing data yet
`); return; } let html = `

Recent Pricing Results

`; items.forEach(row => { let status_class = 'status-needs'; if (row.pricing_status === 'Priced') status_class = 'status-fresh'; else if (row.pricing_status === 'Manual Override') status_class = 'status-fresh'; else if (row.pricing_status === 'Expired') status_class = 'status-expired'; else if (row.pricing_status === 'Error') status_class = 'status-error'; let age = row.days_since_pricing || 0; let age_badge = age < 90 ? 'status-fresh' : (age < 120 ? 'status-aging' : 'status-expired'); html += ``; }); html += `
Manufacturer Model Status Age Low High Avg Samples Source Last Priced
${frappe.utils.escape_html(row.manufacturer || '')} ${frappe.utils.escape_html(row.model || '')} ${row.pricing_status} ${age} days $${row.price_low || ''} $${row.price_high || ''} $${row.price_average || ''} ${row.sample_count || ''} ${row.source || ''} ${frappe.datetime.str_to_user(row.scraped_at) || ''}
`; $area.html(html); } load_recent_pricing(); };