diff --git a/westech_r2/page/ebay-pricing/ebay-pricing.js b/westech_r2/page/ebay-pricing/ebay-pricing.js index 9ec7a25..0fce3f1 100644 --- a/westech_r2/page/ebay-pricing/ebay-pricing.js +++ b/westech_r2/page/ebay-pricing/ebay-pricing.js @@ -4,5 +4,210 @@ frappe.pages['ebay-pricing'].on_page_load = function(wrapper) { title: __('eBay Pricing'), single_column: true }); - $('

eBay Pricing

Search and batch price systems from eBay sold listings.

').appendTo(page.main); + + 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 += `
TitlePriceConditionSoldShipping
${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 = 'badge-needs'; + if (row.pricing_status === 'Priced') status_class = 'badge-fresh'; + else if (row.pricing_status === 'Manual Override') status_class = 'badge-fresh'; + else if (row.pricing_status === 'Expired') status_class = 'badge-expired'; + else if (row.pricing_status === 'Error') status_class = 'badge-error'; + + let age = row.days_since_pricing || 0; + let age_badge = age < 90 ? 'badge-fresh' : (age < 120 ? 'badge-aging' : 'badge-expired'); + + html += ` + + + + + + + + + + + `; + }); + + html += `
ManufacturerModelStatusAgeLowHighAvgSamplesSourceLast 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(); };