| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077 |
- jQuery.noConflict(); // Release $ to other libraries
- // console.log(typeof jQuery);
- // $ = jQuery;
- // --- Config ---
- const UPLOAD_API_URL = '/attr/products/upload-excel/'; // TODO: set to your upload endpoint
- const ACCEPT_TYPES = '*'; // e.g., 'image/*,.csv,.xlsx'
- const thresholdInput = document.getElementById('thresholdRange');
- const thresholdValueDisplay = document.getElementById('thresholdValue');
- var attributesFullData = [];
- var PRODUCT_BASE = [
- // { id: 1, item_id: 'SKU001', product_name: "Levi's Jeans", product_long_description: 'Classic blue denim jeans with straight fit.', product_short_description: 'Blue denim jeans.', product_type: 'Clothing', image_path: 'media/products/jeans.jpg', image: 'http://127.0.0.1:8000/media/products/jeans.png' },
- // { id: 2, item_id: 'SKU002', product_name: 'Adidas Running Shoes', product_long_description: 'Lightweight running shoes with breathable mesh and cushioned sole.', product_short_description: "Men's running shoes.", product_type: 'Footwear', image_path: 'media/products/shoes.png', image: 'http://127.0.0.1:8000/media/products/shoes.png' },
- // { id: 3, item_id: 'SKU003', product_name: 'Nike Sports T-Shirt', product_long_description: 'Moisture-wicking sports tee ideal for training and outdoor activities.', product_short_description: 'Performance t-shirt.', product_type: 'Clothing', image_path: 'media/products/tshirt.png', image: 'http://127.0.0.1:8000/media/products/tshirt.png' },
- // { id: 4, item_id: 'SKU004', product_name: 'Puma Hoodie', product_long_description: 'Soft fleece hoodie with kangaroo pocket and adjustable drawstring.', product_short_description: 'Casual hoodie.', product_type: 'Clothing', image_path: 'media/products/hoodie.png', image: 'http://127.0.0.1:8000/media/products/hoodie.png' },
- // { id: 5, item_id: 'SKU005', product_name: 'Ray-Ban Sunglasses', product_long_description: 'Classic aviator sunglasses with UV protection lenses.', product_short_description: 'Aviator sunglasses.', product_type: 'Accessories', image_path: 'media/products/sunglasses.png', image: 'http://127.0.0.1:8000/media/products/sunglasses.png' }
- ];
- // --- Data ---
- const mediaUrl = "./../";
- document.addEventListener('DOMContentLoaded', () => {
- jQuery('#full-page-loader').show();
- fetch('/attr/products', {
- method: 'GET', // or 'POST' if your API expects POST
- headers: {
- 'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]')?.value || ''
- }
- })
- .then(response => response.json())
- .then(data => {
- // console.log("data",data);
- // --- Wire up ---
- PRODUCT_BASE = data;
- PRODUCT_BASE = PRODUCT_BASE.map((d)=>{return {...d,mandatoryAttributes:["color","size"]}});
- // console.log("PRODUCT_BASE",PRODUCT_BASE);
- if(PRODUCT_BASE.length > 0){
- $('#paginationBar').style.display = 'block';
- }
- renderProducts();
- getAtributeList();
- document.getElementById('btnSubmit').addEventListener('click', submitAttributes);
- // document.getElementById('btnReset').addEventListener('click', resetAll);
- // document.getElementById('btnSelectAll').addEventListener('click', () => {
- // if (selectedIds.size === PRODUCT_BASE.length) { selectedIds.clear(); } else { selectedIds = new Set(PRODUCT_BASE.map(p => p.id)); }
- // // renderProducts();
- // });
- // Replace your existing Select All listener with this:
- document.getElementById('btnSelectAll').addEventListener('click', () => {
- // Use the container for the active layout
- const container = (layoutMode === 'cards')
- ? document.getElementById('cardsContainer')
- : document.getElementById('tableContainer');
- // Collect all visible checkboxes
- const boxes = Array.from(container.querySelectorAll('input[type="checkbox"]'));
- // If every visible checkbox is already checked, we'll deselect; otherwise select all
- const allChecked = boxes.length > 0 && boxes.every(cb => cb.checked);
- boxes.forEach(cb => {
- const target = !allChecked; // true to select, false to deselect
- if (cb.checked !== target) {
- cb.checked = target;
- // Trigger your existing "change" handler so selectedIds & row .selected class update
- cb.dispatchEvent(new Event('change', { bubbles: true }));
- }
- });
- // Update the selection pill text (doesn't re-render the list)
- updateSelectionInfo();
- });
- document.getElementById('btnCards').addEventListener('click', () => setLayout('cards'));
- document.getElementById('btnTable').addEventListener('click', () => setLayout('table'));
- jQuery('#full-page-loader').hide();
- // if (data.success) {
- // }
- });
- });
- var API_RESPONSE_AI = {
- // results: [
- // { product_id: 'SKU001', mandatory: { 'Clothing Neck Style': 'V-Neck', 'Clothing Top Style': 'Pullover', 'Condition': 'New', 'T-Shirt Type': 'Classic T-Shirt' }, additional: { 'Material': 'Turkish Pima Cotton', 'Size': 'Large', 'Color': 'Blue', 'Brand': 'Sierra', 'Fabric Type': 'Soft & Breathable', 'Fabric Composition': '95% Turkish Pima cotton', 'Care Instructions': 'Machine Washable', 'Sizes Available': 'S-XL' } },
- // { product_id: 'SKU002', mandatory: { 'Shoe Type': 'Running', 'Closure': 'Lace-Up', 'Condition': 'New', 'Gender': 'Men' }, additional: { 'Upper Material': 'Engineered Mesh', 'Midsole': 'EVA Foam', 'Outsole': 'Rubber', 'Color': 'Black/White', 'Brand': 'Adidas', 'Size': 'UK 9', 'Care Instructions': 'Surface Clean' } },
- // { product_id: 'SKU003', mandatory: { 'Clothing Neck Style': 'Crew Neck', 'Sleeve Length': 'Short Sleeve', 'Condition': 'New', 'T-Shirt Type': 'Performance' }, additional: { 'Material': 'Polyester Blend', 'Color': 'Red', 'Brand': 'Nike', 'Size': 'Medium', 'Fabric Technology': 'Dri-FIT', 'Care Instructions': 'Machine Wash Cold' } },
- // { product_id: 'SKU004', mandatory: { 'Clothing Top Style': 'Hoodie', 'Closure': 'Pullover', 'Condition': 'New', 'Fit': 'Relaxed' }, additional: { 'Material': 'Cotton Fleece', 'Color': 'Charcoal', 'Brand': 'Puma', 'Size': 'Large', 'Care Instructions': 'Machine Wash Warm' } },
- // { product_id: 'SKU005', mandatory: { 'Accessory Type': 'Sunglasses', 'Frame Style': 'Aviator', 'Condition': 'New', 'Lens Protection': 'UV 400' }, additional: { 'Frame Material': 'Metal', 'Lens Color': 'Green', 'Brand': 'Ray-Ban', 'Size': 'Standard', 'Case Included': 'Yes', 'Care Instructions': 'Clean with microfiber' } }
- // ],
- // total_products: 5,
- // successful: 5,
- // failed: 0
- };
- // --- State ---
- let selectedIds = new Set();
- // NEW: Array of objects { item_id: string, mandatory_attrs: { [attribute_name]: string[] } }
- let selectedProductsWithAttributes = [];
- let selectedAttributes = new Array();
- const lastSeen = new Map(); // per-product memory for NEW highlighting (product_id -> maps)
- let layoutMode = 'table'; // 'cards' | 'table'
- // --- Helpers ---
- const $ = (sel) => document.querySelector(sel);
- const el = (tag, cls) => { const e = document.createElement(tag); if (cls) e.className = cls; return e; }
- function updateSelectionInfo() {
- const pill = $('#selectionInfo');
- const total = PRODUCT_BASE.length;
- // const count = selectedIds.size;
- const count = selectedProductsWithAttributes.length;
- pill.textContent = count === 0 ? 'No products selected' : `${count} of ${total} selected`;
- }
- function setChecked(id, checked) { if (checked) selectedIds.add(id); else selectedIds.delete(id); updateSelectionInfo(); }
- // function setCheckedAttributes(id,attribute, checked) { if (checked) selectedAttributes.add({id: [attribute]}); else selectedIds.delete({id:[attribute]}); updateSelectionInfo(); }
- function formatString(str) {
- return str
- // Replace underscores with spaces
- .replace(/_/g, ' ')
- // Insert a space before any uppercase letter (except at the start)
- .replace(/([a-z])([A-Z])/g, '$1 $2')
- // Capitalize the first letter
- .replace(/^./, char => char.toUpperCase());
- }
- // --- Chips rendering ---
- function renderChips(container, obj, memoryMap) {
- container.innerHTML = '';
- let count = 0;
- Object.entries(obj || {}).forEach(([k, v]) => {
- const chip = el('span', 'chip');
- const kEl = el('span', 'k'); kEl.textContent = formatString(k) + ':';
- // console.log("v",v);
- let resVal = "";
- let sourceVal = "";
- if(v instanceof Array){
- resVal = String(v.map(v => formatString(v.value)).join(", "));
- sourceVal = String(formatString(v[0]?.source));
- const vEl = el('span', 'v'); vEl.textContent = ' ' + resVal +' (' + sourceVal + ')';
- chip.appendChild(kEl); chip.appendChild(vEl);
- }
- // console.log("k",k);
- if(v instanceof Array){
- const was = memoryMap.get(k);
- if (was === undefined || was !== v) chip.classList.add('new');
- container.appendChild(chip);
- memoryMap.set(k, v);
-
- count++;
- }
- });
- return count;
- }
- function findApiResultForProduct(p, index, api) { return api.results?.find(r => r.product_id === p.item_id) || api.results?.[index] || null; }
- // --- Cards layout ---
- function createProductCard(p) {
- const row = el('div', 'product');
- // Check selection using the new helper
- if (isProductSelected(p.item_id)) row.classList.add('selected');
- // if (selectedIds.has(p.item_id)) row.classList.add('selected');
- // const left = el('div', 'thumb');
- // const img = new Image(); img.src = p.image_path || p.image || '';
- // // console.log("image path",p.image_path);
- // img.alt = `${p.product_name} image`;
- // // console.log("img",img);
- // // img.onerror = () => { img.remove(); const fb = el('div', 'fallback'); fb.textContent = (p.product_name || 'Product').split(' ').map(w => w[0]).slice(0,2).join('').toUpperCase(); left.appendChild(fb); };
- // img.onerror = () => { img.src = mediaUrl+"media/images/no-product.png" };
- // left.appendChild(img);
- // Assume 'el' is a function that creates an element (e.g., document.createElement)
- // Assume 'p' (product data) and 'mediaUrl' are available in scope.
- // 1. Create the main container for the hover effect
- const container = el('div', 'mini-thumb-container');
- // 2. Create the visible thumbnail wrapper (your original 'left' element)
- // This will serve as the base for the small image
- const left = el('div', 'thumb');
- // Get the image source (same as before)
- const imageSrc = p.image_path || p.image || '';
- // 3. Create the thumbnail image element (same as before)
- const thumbImg = new Image();
- thumbImg.src = imageSrc;
- thumbImg.alt = `${p.product_name} image`;
- thumbImg.onerror = () => { thumbImg.src = mediaUrl+"media/images/no-product.png" };
- // Use thumbImg instead of img to avoid naming conflict with other code if possible
- // We will call it 'img' here to match your original code:
- const img = thumbImg;
- left.appendChild(img);
- container.appendChild(left); // Add the visible thumbnail to the main container
- // 4. Create the full-size image element for hover (new part)
- const fullImgWrapper = el('div', 'full-image-hover'); // Class for CSS control
- const fullImg = new Image();
- fullImg.src = imageSrc; // Use the same source, or p.full_image_path if available
- fullImg.alt = `${p.product_name} full view`;
- fullImg.onerror = () => { fullImg.src = mediaUrl + "media/images/no-product.png" }; // Same fallback
- fullImgWrapper.appendChild(fullImg);
- container.appendChild(fullImgWrapper); // Add full image wrapper to the main container
- // The variable to use when appending this element to the DOM is 'container'.
- // return container; // If this block is inside a function
- const mid = el('div', 'meta');
- const name = el('div', 'name'); name.textContent = p.product_name || '—';
- const desc = el('div', 'desc'); desc.innerHTML = p.product_short_description || '';
- const badges = el('div', 'badges');
- const sku = el('span', 'pill'); sku.textContent = `SKU: ${p.item_id || '—'}`; badges.appendChild(sku);
- const type = el('span', 'pill'); type.textContent = p.product_type || '—'; badges.appendChild(type);
- const long = el('div', 'desc'); long.innerHTML = p.product_long_description || ''; long.style.marginTop = '4px';
- mid.appendChild(name); mid.appendChild(desc); mid.appendChild(badges); mid.appendChild(long);
- // Helper function to create the chip UI for attributes
- function createAttributeChips(p, attr, initialSelected, isMandatory, updateCallback) {
- const wrapper = el('div', 'attribute-chip-group');
- wrapper.dataset.attrName = attr.attribute_name;
- wrapper.innerHTML = `<p class="attribute-header">${attr.attribute_name} (${isMandatory ? 'Mandatory' : 'Optional'}):</p>`;
- const chipContainer = el('div', 'chips-container');
- attr.possible_values.forEach(value => {
- const chip = el('label', 'attribute-chip');
-
- // Checkbox input is hidden, but drives the selection state
- const checkbox = document.createElement('input');
- checkbox.type = 'checkbox';
- checkbox.value = value;
- checkbox.name = `${p.item_id}-${attr.attribute_name}`;
-
- // Set initial state
- checkbox.checked = initialSelected.includes(value);
-
- // The visual part of the chip
- const span = el('span');
- span.textContent = value;
-
- chip.appendChild(checkbox);
- chip.appendChild(span);
- chipContainer.appendChild(chip);
- });
-
- // Use event delegation on the container for performance
- chipContainer.addEventListener('change', updateCallback);
- wrapper.appendChild(chipContainer);
- return wrapper;
- }
- // --- Main Select Checkbox (Product Selection) ---
- const right = el('label', 'select');
- const cb = document.createElement('input'); cb.type = 'checkbox';
- cb.checked = isProductSelected(p.item_id);
- const lbl = el('span'); lbl.textContent = 'Select Product';
- right.appendChild(cb); right.appendChild(lbl);
-
- // --- Dynamic Attribute Selects ---
- const attrContainer = el('div', 'attribute-selectors');
- if(p.product_type_details.length > 0){
- // Find all mandatory and non-mandatory attributes for this product
- const mandatoryAttributes = p.product_type_details?.filter(a => a.is_mandatory === 'Yes') || [];
- const optionalAttributes = p.product_type_details?.filter(a => a.is_mandatory !== 'Yes') || [];
- // Helper to update the main state object with all current selections
- const updateProductState = () => {
- const isSelected = cb.checked;
- const currentSelections = {};
- if (isSelected) {
- // Iterate over all attribute groups (Mandatory and Optional)
- attrContainer.querySelectorAll('.attribute-chip-group').forEach(group => {
- const attrName = group.dataset.attrName;
-
- // Collect selected chip values
- const selectedOptions = Array.from(group.querySelectorAll('input[type="checkbox"]:checked'))
- .map(checkbox => checkbox.value);
- if (selectedOptions.length > 0) {
- currentSelections[attrName] = selectedOptions;
- }
- });
- }
-
- toggleProductSelection(p.item_id, isSelected, currentSelections);
- row.classList.toggle('selected', isSelected);
- };
- // Attach listener to main checkbox
- cb.addEventListener('change', () => {
- attrContainer.classList.toggle('disabled', !cb.checked);
- updateProductState();
- });
-
- // --- Render Mandatory Attributes ---
- // if (mandatoryAttributes.length > 0) {
- // const manTitle = el('p', "pSelectRight mandatory-title");
- // manTitle.innerHTML = "Mandatory Attributes:";
- // attrContainer.appendChild(manTitle);
- // mandatoryAttributes.forEach(attr => {
- // const initialSelected = getSelectedAttributes(p.item_id)[attr.attribute_name] || attr.possible_values;
- // const chipGroup = createAttributeChips(p, attr, initialSelected, true, updateProductState);
- // attrContainer.appendChild(chipGroup);
- // });
- // }
- // --- Render Optional Attributes ---
- if (optionalAttributes.length > 0) {
- const br = el('br');
- const optTitle = el('p', "pSelectRight optional-title");
- optTitle.innerHTML = "Additional Attributes:";
- attrContainer.appendChild(br);
- attrContainer.appendChild(optTitle);
- optionalAttributes.forEach(attr => {
- const initialSelected = getSelectedAttributes(p.item_id)[attr.attribute_name] || attr.possible_values;
- const chipGroup = createAttributeChips(p, attr, initialSelected, false, updateProductState);
- attrContainer.appendChild(chipGroup);
- });
- }
- // Initialize attribute selectors' enabled state and state data
- attrContainer.classList.toggle('disabled', !cb.checked);
- // Initial state setup if the product was already selected (e.g., after a re-render)
- if (cb.checked) {
- // This is important to set the initial state correctly on load
- // We defer this until all selects are mounted, or ensure the initial state is correct.
- // For simplicity, we assume the data from PRODUCT_BASE already includes selected attributes if a selection exists
- // (which it won't in this case, so they default to all/empty)
- }
- }
- const inline = el('div', 'attr-inline');
- inline.dataset.pid = p.item_id; // use item_id for mapping
-
- row.appendChild(container); row.appendChild(mid);
- if(p.product_type_details.length > 0){
- console.log("IN ");
- // row.appendChild(attrContainer); // Append the new attribute selectors container
- }
- row.appendChild(right);
- // if (p.mandatoryAttributes && p.mandatoryAttributes.length > 0) {
- // const hr = el('hr');
- // row.appendChild(hr);
- // row.appendChild(attri);
- // row.appendChild(secondRight);
- // }
- row.appendChild(inline);
-
- return row;
- }
- // Cards layout
- function renderProductsCards(items = getCurrentSlice()) {
- const cards = document.getElementById('cardsContainer');
- cards.innerHTML = '';
- if(items.length > 0){
- items.forEach(p => cards.appendChild(createProductCard(p)));
- }else{
- cards.innerHTML = "<p>No Products Found.</p>"
- }
- }
- // --- Table layout ---
- // function createMiniThumb(p) {
- // const mt = el('div', 'mini-thumb');
- // const img = new Image(); img.src = p.image_path || p.image || ''; img.alt = `${p.product_name} image`;
- // // console.log("image path",p.image_path);
- // // console.log("img",img);
- // img.onerror = () => { img.src = mediaUrl+"media/images/no-product.png" };
- // // img.onerror = () => { img.remove(); const fb = el('div', 'fallback'); fb.textContent = (p.product_name || 'Product').split(' ').map(w => w[0]).slice(0,2).join('').toUpperCase(); mt.appendChild(fb); };
- // mt.appendChild(img);
- // return mt;
- // }
- function createMiniThumb(p) {
- // 1. Create a container for the hover effect
- const container = document.createElement('div');
- container.className = 'mini-thumb-container';
- // 2. Create the visible thumbnail (Mini Thumb)
- const mt = document.createElement('div');
- mt.className = 'mini-thumb';
- // Get the image source
- const imageSrc = p.image_path || p.image || '';
- // 3. Create the thumbnail image element
- const thumbImg = new Image();
- thumbImg.src = imageSrc;
- thumbImg.alt = `${p.product_name} thumbnail`;
- thumbImg.onerror = () => { thumbImg.src = mediaUrl + "media/images/no-product.png" };
-
- mt.appendChild(thumbImg);
- container.appendChild(mt); // Add thumbnail to container
- // 4. Create the full-size image element for hover
- const fullImgWrapper = document.createElement('div');
- fullImgWrapper.className = 'full-image-hover'; // Class for CSS control
-
- const fullImg = new Image();
- // Assuming the same source is used for the full image, but you can change this
- // to p.full_image_path if your product object has a separate full-size URL.
- fullImg.src = imageSrc;
- fullImg.alt = `${p.product_name} full view`;
- fullImg.onerror = () => { fullImg.src = mediaUrl + "media/images/no-product.png" }; // Same fallback
-
- fullImgWrapper.appendChild(fullImg);
- container.appendChild(fullImgWrapper); // Add full image wrapper to container
- // Return the main container instead of just the mini-thumb
- return container;
- }
- // function createMiniThumb(p) {
- // const container = document.createElement('div');
- // container.className = 'mini-thumb-container';
- // const mt = document.createElement('div');
- // mt.className = 'mini-thumb';
- // const imageSrc = p.image_path || p.image || '';
- // const thumbImg = new Image();
- // thumbImg.src = imageSrc;
- // thumbImg.alt = `${p.product_name} thumbnail`;
- // thumbImg.onerror = () => { thumbImg.src = mediaUrl + "media/images/no-product.png" };
- // mt.appendChild(thumbImg);
- // container.appendChild(mt);
- // const fullImgWrapper = document.createElement('div');
- // fullImgWrapper.className = 'full-image-hover';
- // const fullImg = new Image();
- // fullImg.src = imageSrc;
- // fullImg.alt = `${p.product_name} full view`;
- // fullImg.onerror = () => { fullImg.src = mediaUrl + "media/images/no-product.png" };
- // fullImgWrapper.appendChild(fullImg);
- // document.body.appendChild(fullImgWrapper); // Append to body, not container
- // // Hover logic
- // mt.addEventListener('mouseenter', () => {
- // fullImgWrapper.style.display = 'block';
- // });
- // mt.addEventListener('mousemove', (e) => {
- // fullImgWrapper.style.top = (e.clientY + 20) + 'px'; // 20px offset
- // fullImgWrapper.style.left = (e.clientX + 20) + 'px';
- // });
- // mt.addEventListener('mouseleave', () => {
- // fullImgWrapper.style.display = 'none';
- // });
- // return container;
- // }
- // Create one global hover container
- // const hoverContainer = document.createElement('div');
- // hoverContainer.className = 'full-image-hover';
- // const hoverImg = new Image();
- // hoverContainer.appendChild(hoverImg);
- // document.body.appendChild(hoverContainer);
- // let hoverActive = false;
- // function createMiniThumb(p) {
- // const container = document.createElement('div');
- // container.className = 'mini-thumb-container';
- // const mt = document.createElement('div');
- // mt.className = 'mini-thumb';
- // const imageSrc = p.image_path || p.image || '';
- // const thumbImg = new Image();
- // thumbImg.src = imageSrc;
- // thumbImg.alt = `${p.product_name} thumbnail`;
- // thumbImg.onerror = () => { thumbImg.src = mediaUrl + "media/images/no-product.png" };
- // mt.appendChild(thumbImg);
- // container.appendChild(mt);
- // // Hover logic
- // mt.addEventListener('mouseenter', () => {
- // hoverImg.src = imageSrc;
- // hoverImg.alt = `${p.product_name} full view`;
- // hoverContainer.style.display = 'block';
- // });
- // mt.addEventListener('mousemove', (e) => {
- // hoverContainer.style.top = (e.clientY + 20) + 'px';
- // hoverContainer.style.left = (e.clientX + 20) + 'px';
- // });
- // mt.addEventListener('mouseleave', () => {
- // setTimeout(() => {
- // if (!hoverActive) hoverContainer.style.display = 'none';
- // }, 100);
- // });
- // hoverContainer.addEventListener('mouseenter', () => {
- // hoverActive = true;
- // });
- // hoverContainer.addEventListener('mouseleave', () => {
- // hoverActive = false;
- // hoverContainer.style.display = 'none';
- // });
- // return container;
- // }
- // Table layout
- // function renderProductsTable(items = getCurrentSlice()) {
- // const wrap = document.getElementById('tableContainer');
- // wrap.innerHTML = '';
- // const table = document.createElement('table');
- // const thead = document.createElement('thead'); const trh = document.createElement('tr');
- // ['Select', 'Image', 'Product', 'SKU', 'Type', 'Short Description'].forEach(h => {
- // const th = document.createElement('th'); th.textContent = h; trh.appendChild(th);
- // });
- // thead.appendChild(trh); table.appendChild(thead);
- // const tbody = document.createElement('tbody');
- // if(items.length > 0 ){
- // items.forEach(p => {
- // const tr = document.createElement('tr'); tr.id = `row-${p.id}`;
- // const tdSel = document.createElement('td'); tdSel.className = 'select-cell';
- // const cb = document.createElement('input'); cb.type = 'checkbox'; cb.checked = selectedIds.has(p.item_id);
- // cb.addEventListener('change', () => { setChecked(p.item_id, cb.checked); tr.classList.toggle('selected', cb.checked); });
- // tdSel.appendChild(cb); tr.appendChild(tdSel);
- // const tdImg = document.createElement('td'); tdImg.className = 'thumb-cell'; tdImg.appendChild(createMiniThumb(p)); tr.appendChild(tdImg);
- // const tdName = document.createElement('td'); tdName.textContent = p.product_name || '—'; tr.appendChild(tdName);
- // const tdSku = document.createElement('td'); tdSku.textContent = p.item_id || '—'; tr.appendChild(tdSku);
- // const tdType = document.createElement('td'); const b = document.createElement('span'); b.className = 'badge'; b.textContent = p.product_type || '—'; tdType.appendChild(b); tr.appendChild(tdType);
- // const tdDesc = document.createElement('td'); tdDesc.textContent = p.product_short_description || ''; tr.appendChild(tdDesc);
- // tr.addEventListener('click', (e) => { if (e.target.tagName.toLowerCase() !== 'input') { cb.checked = !cb.checked; cb.dispatchEvent(new Event('change')); } });
- // tbody.appendChild(tr);
- // });
- // }else{
- // const tr = el('tr');
- // // tr.id = `row-${p.id}`;
- // const tdName = el('td');
- // tdName.colSpan = 6;
- // tdName.innerHTML = "No Products Found."
- // tr.appendChild(tdName);
- // // tr.colspan = 6;
- // // tr.innerHTML
- // tbody.appendChild(tr);
- // }
- // table.appendChild(tbody);
- // wrap.appendChild(table);
- // }
- // NOTE: Ensure getProductStateUpdater and generateAttributeUI functions are defined globally or accessible here.
- /**
- * Returns a closure function that updates the global selectedProductsWithAttributes state
- * based on the current selections (chips) found in the DOM for a specific product.
- * This is used for both card and table views.
- * * @param {Object} p - The product object.
- * @param {HTMLElement} cb - The main product selection checkbox element.
- * @param {HTMLElement} tr - The main row/card element (used for toggling 'selected' class).
- * @returns {function} A function to be used as the attribute change handler.
- */
- const getProductStateUpdater = (p, cb, tr) => () => {
- const isSelected = cb.checked;
- const currentSelections = {};
-
- // Find the attribute container using its unique ID, which is the same structure
- // used in both card and table detail views (e.g., 'attr-container-124353498' or just the main card element).
- // For card view, the container is often the attrContainer element itself.
- // For table view, we use the explicit ID.
- const attrContainer = document.getElementById(`attr-container-${p.item_id}`) || tr.querySelector('.attribute-selectors');
- if (isSelected && attrContainer) {
- // Iterate over all attribute groups (Mandatory and Optional) within the container
- attrContainer.querySelectorAll('.attribute-chip-group').forEach(group => {
- const attrName = group.dataset.attrName;
-
- // Collect selected chip values
- const selectedOptions = Array.from(group.querySelectorAll('input[type="checkbox"]:checked'))
- .map(checkbox => checkbox.value);
- // Only add to the selection if at least one option is selected
- if (selectedOptions.length > 0) {
- currentSelections[attrName] = selectedOptions;
- }
- });
- }
-
- // Update the global state array (selectedProductsWithAttributes)
- toggleProductSelection(p.item_id, isSelected, currentSelections);
-
- // Update the visual status of the row/card
- tr.classList.toggle('selected', isSelected);
- };
- /**
- * Generates the full attribute selection UI (chips) for a given product.
- * NOTE: Assumes el(), createAttributeChips(), and getSelectedAttributes() are defined globally.
- * @param {Object} p - The product object from PRODUCT_BASE.
- * @param {function} updateProductState - The callback to run on chip changes.
- * @param {HTMLElement} attrContainer - The container to append the UI to.
- */
- function generateAttributeUI(p, updateProductState, attrContainer) {
- // Clear the container first, just in case
- attrContainer.innerHTML = '';
- const mandatoryAttributes = p.product_type_details?.filter(a => a.is_mandatory === 'Yes') || [];
- const optionalAttributes = p.product_type_details?.filter(a => a.is_mandatory !== 'Yes') || [];
- // --- Render Mandatory Attributes ---
- if (mandatoryAttributes.length > 0) {
- // Use a general title for the section header
- const manTitle = el('p', "pSelectRight mandatory-title");
- manTitle.innerHTML = "Mandatory Attributes:";
- attrContainer.appendChild(manTitle);
- mandatoryAttributes.forEach(attr => {
- const initialSelected = getSelectedAttributes(p.item_id)[attr.attribute_name] || attr.possible_values;
- // The createAttributeChips function must be globally available
- const chipGroup = createAttributeChips(p, attr, initialSelected, true, updateProductState);
- attrContainer.appendChild(chipGroup);
- });
- }
- // --- Render Optional Attributes ---
- if (optionalAttributes.length > 0) {
- // Add visual separation using the optional-title class
- const optTitle = el('p', "pSelectRight optional-title");
- optTitle.innerHTML = "Additional Attributes:";
-
- // Append the title for separation
- attrContainer.appendChild(optTitle);
- optionalAttributes.forEach(attr => {
- const initialSelected = getSelectedAttributes(p.item_id)[attr.attribute_name] || attr.possible_values;
- const chipGroup = createAttributeChips(p, attr, initialSelected, false, updateProductState);
- attrContainer.appendChild(chipGroup);
- });
- }
- }
- /**
- * Creates the HTML structure for a single attribute group using chip/checkbox labels.
- * Assumes the helper function 'el' is available.
- * * @param {Object} p - The product object.
- * @param {Object} attr - The specific attribute detail object.
- * @param {string[]} initialSelected - Array of values that should be pre-checked.
- * @param {boolean} isMandatory - True if the attribute is mandatory.
- * @param {function} updateCallback - The function to call when a chip selection changes.
- * @returns {HTMLElement} The attribute chip group container (div).
- */
- function createAttributeChips(p, attr, initialSelected, isMandatory, updateCallback) {
- const wrapper = el('div', 'attribute-chip-group');
- wrapper.dataset.attrName = attr.attribute_name;
-
- // Determine the header text based on structure preference (e.g., just the name)
- const statusText = isMandatory ? ' (Mandatory)' : ' (Optional)';
- wrapper.innerHTML = `<p class="attribute-header">${attr.attribute_name}${statusText}:</p>`;
- const chipContainer = el('div', 'chips-container');
- attr.possible_values.forEach(value => {
- const chip = el('label', 'attribute-chip');
-
- // Checkbox input is hidden, but drives the selection state
- const checkbox = document.createElement('input');
- checkbox.type = 'checkbox';
- checkbox.value = value;
- // Ensure the name is unique per product/attribute group
- checkbox.name = `${p.item_id}-${attr.attribute_name}`;
-
- // Set initial state
- checkbox.checked = initialSelected.includes(value);
-
- // The visual part of the chip
- const span = el('span');
- span.textContent = value;
-
- chip.appendChild(checkbox);
- chip.appendChild(span);
- chipContainer.appendChild(chip);
- });
-
- // Attach listener to the container using event delegation
- chipContainer.addEventListener('change', updateCallback);
- wrapper.appendChild(chipContainer);
- return wrapper;
- }
- // function renderProductsTable(items = getCurrentSlice()) {
- // const wrap = document.getElementById('tableContainer');
- // wrap.innerHTML = '';
- // const table = document.createElement('table');
- // table.classList.add('table', 'table-striped', 'table-bordered','table-responsive');
-
- // const thead = document.createElement('thead');
- // const trh = document.createElement('tr');
-
- // // Table Headers
- // ['Select', 'Image', 'Product', 'SKU', 'Type', 'Short Description'].forEach(h => {
- // const th = document.createElement('th'); th.textContent = h; trh.appendChild(th);
- // });
- // thead.appendChild(trh); table.appendChild(thead);
-
- // const tbody = document.createElement('tbody');
-
- // if (items.length > 0) {
- // items.forEach(p => {
- // const tr = document.createElement('tr');
- // tr.id = `row-${p.id}`;
- // if (isProductSelected(p.item_id)) tr.classList.add('selected');
- // // --- Define Checkbox (cb) and State Updater ---
- // const cb = document.createElement('input');
- // cb.type = 'checkbox';
- // cb.checked = isProductSelected(p.item_id);
- // // console.log("checkkkkk")
-
- // // The state updater function is bound to this specific row/checkbox
- // const updateProductState = getProductStateUpdater(p, cb, tr);
- // // --- Select Cell ---
- // const tdSel = document.createElement('td');
- // tdSel.className = 'select-cell';
- // tdSel.appendChild(cb);
- // tr.appendChild(tdSel);
- // // --- Other Cells ---
- // const tdImg = document.createElement('td'); tdImg.className = 'thumb-cell'; tdImg.appendChild(createMiniThumb(p)); tr.appendChild(tdImg);
- // const tdName = document.createElement('td'); tdName.textContent = p.product_name || '—'; tr.appendChild(tdName);
- // const tdSku = document.createElement('td'); tdSku.textContent = p.item_id || '—'; tr.appendChild(tdSku);
- // const tdType = document.createElement('td'); const b = document.createElement('span'); b.className = 'badge'; b.textContent = p.product_type || '—'; tdType.appendChild(b); tr.appendChild(tdType);
- // const tdDesc = document.createElement('td'); tdDesc.innerHTML = p.product_short_description || ''; tr.appendChild(tdDesc);
- // // ---------------------------------------------
- // // --- ATTRIBUTE SELECTION IMPLEMENTATION ---
- // // ---------------------------------------------
- // // 1. DETAIL ROW STRUCTURE
- // const detailRow = document.createElement('tr');
- // detailRow.classList.add('attribute-detail-row'); // Custom class for styling
- // detailRow.style.display = 'none'; // Initially hidden
- // detailRow.id = `detail-row-${p.id}`;
-
- // const detailCell = document.createElement('td');
- // detailCell.colSpan = 6; // Must span all columns
-
- // const attrContainer = document.createElement('div');
- // attrContainer.id = `attr-container-${p.item_id}`; // Unique ID for targeting by updateProductState
- // attrContainer.classList.add('attribute-selectors', 'table-selectors');
- // // 2. GENERATE CHIPS UI
- // generateAttributeUI(p, updateProductState, attrContainer);
- // // Initially disable the chips if the product is not selected
- // attrContainer.classList.toggle('disabled', !cb.checked);
- // detailCell.appendChild(attrContainer);
- // detailRow.appendChild(detailCell);
- // if(p.product_type_details.length > 0){
- // // 3. TOGGLE BUTTON (in the main row)
- // const tdAttr = document.createElement('td');
- // const toggleButton = document.createElement('button');
- // toggleButton.textContent = 'Configure';
- // toggleButton.classList.add('btn', 'btn-sm', 'btn-info', 'attribute-toggle-btn');
- // tdAttr.appendChild(toggleButton);
- // // tr.appendChild(tdAttr);
-
- // // 4. EVENT LISTENERS
-
- // // a) Toggle Button Logic
- // toggleButton.addEventListener('click', (e) => {
- // e.stopPropagation(); // Stop row click event
- // const isHidden = detailRow.style.display === 'none';
- // detailRow.style.display = isHidden ? '' : 'none'; // Toggle visibility
- // toggleButton.textContent = isHidden ? 'Hide Attributes' : 'Configure';
- // toggleButton.classList.toggle('btn-info', !isHidden);
- // toggleButton.classList.toggle('btn-secondary', isHidden);
- // });
-
- // // b) Main Checkbox Change Logic
- // cb.addEventListener('change', () => {
- // // console.log("cheeeeeeeeee");
- // updateProductState(); // Update state on check/uncheck
- // attrContainer.classList.toggle('disabled', !cb.checked); // Enable/Disable chips
- // });
- // // c) Row Click Listener (Updated to ignore button clicks)
- // tr.addEventListener('click', (e) => {
- // const tag = e.target.tagName.toLowerCase();
- // // console.log("clikk")
- // if (tag !== 'input' && tag !== 'button') {
- // cb.checked = !cb.checked;
- // cb.dispatchEvent(new Event('change'));
- // }
- // });
- // }else{
- // const tdAttr = document.createElement('td');
- // tr.appendChild(tdAttr);
- // // b) Main Checkbox Change Logic
- // cb.addEventListener('change', () => {
- // // console.log("cheeeeeeeeee");
- // updateProductState(); // Update state on check/uncheck
- // attrContainer.classList.toggle('disabled', !cb.checked); // Enable/Disable chips
- // });
- // tr.addEventListener('click', (e) => {
- // const tag = e.target.tagName.toLowerCase();
- // // console.log("clikk")
- // if (tag !== 'input' && tag !== 'button') {
- // cb.checked = !cb.checked;
- // cb.dispatchEvent(new Event('change'));
- // }
- // });
- // }
-
- // // 5. Append Rows to TBODY
- // tbody.appendChild(tr);
- // tbody.appendChild(detailRow); // Append the detail row right after the main row
- // });
- // } else {
- // const tr = el('tr');
- // const tdName = el('td');
- // tdName.colSpan = 6;
- // tdName.innerHTML = "No Products Found.";
- // tr.appendChild(tdName);
- // tbody.appendChild(tr);
- // }
- // table.appendChild(tbody);
- // wrap.appendChild(table);
- // }
- function renderProductsTable(items = getCurrentSlice()) {
- const wrap = document.getElementById('tableContainer');
- wrap.innerHTML = '';
- const table = document.createElement('table');
- table.classList.add('table', 'table-striped', 'table-bordered', 'table-responsive');
- table.id = 'productsTable'; // ✅ Add a unique table ID
- const thead = document.createElement('thead');
- const trh = document.createElement('tr');
- // --- "Select All" Header with Checkbox ---
- const thSelect = document.createElement('th');
- const selectAllCheckbox = document.createElement('input');
- selectAllCheckbox.type = 'checkbox';
- selectAllCheckbox.id = 'selectAllCheckbox';
- thSelect.appendChild(selectAllCheckbox);
- trh.appendChild(thSelect);
- // Other headers
- ['Image', 'Product', 'SKU', 'Type', 'Short Description'].forEach(h => {
- const th = document.createElement('th');
- th.textContent = h;
- trh.appendChild(th);
- });
- thead.appendChild(trh);
- table.appendChild(thead);
- const tbody = document.createElement('tbody');
- if (items.length > 0) {
- items.forEach(p => {
- const tr = document.createElement('tr');
- tr.id = `row-${p.id}`;
- if (isProductSelected(p.item_id)) tr.classList.add('selected');
- // Checkbox for each row
- const cb = document.createElement('input');
- cb.type = 'checkbox';
- cb.classList.add('checkbox-productlist'); // ✅ correct way to add class
- cb.checked = isProductSelected(p.item_id);
- const updateProductState = getProductStateUpdater(p, cb, tr);
- const tdSel = document.createElement('td');
- tdSel.className = 'select-cell';
- tdSel.appendChild(cb);
- tr.appendChild(tdSel);
- const tdImg = document.createElement('td');
- tdImg.className = 'thumb-cell';
- tdImg.appendChild(createMiniThumb(p));
- tr.appendChild(tdImg);
- const tdName = document.createElement('td');
- tdName.textContent = p.product_name || '—';
- tr.appendChild(tdName);
- const tdSku = document.createElement('td');
- tdSku.textContent = p.item_id || '—';
- tr.appendChild(tdSku);
- const tdType = document.createElement('td');
- const b = document.createElement('span');
- b.className = 'badge';
- b.textContent = p.product_type || '—';
- tdType.appendChild(b);
- tr.appendChild(tdType);
- const tdDesc = document.createElement('td');
- tdDesc.innerHTML = p.product_short_description || '';
- tr.appendChild(tdDesc);
- // Handle attribute rows (kept your same logic)
- const detailRow = document.createElement('tr');
- detailRow.classList.add('attribute-detail-row');
- detailRow.style.display = 'none';
- detailRow.id = `detail-row-${p.id}`;
- const detailCell = document.createElement('td');
- detailCell.colSpan = 6;
- const attrContainer = document.createElement('div');
- attrContainer.id = `attr-container-${p.item_id}`;
- attrContainer.classList.add('attribute-selectors', 'table-selectors');
- generateAttributeUI(p, updateProductState, attrContainer);
- attrContainer.classList.toggle('disabled', !cb.checked);
- detailCell.appendChild(attrContainer);
- detailRow.appendChild(detailCell);
- // Checkbox behavior
- cb.addEventListener('change', () => {
- updateProductState();
- attrContainer.classList.toggle('disabled', !cb.checked);
- });
- tr.addEventListener('click', (e) => {
- const tag = e.target.tagName.toLowerCase();
- if (tag !== 'input' && tag !== 'button') {
- cb.checked = !cb.checked;
- cb.dispatchEvent(new Event('change'));
- }
- });
- tbody.appendChild(tr);
- tbody.appendChild(detailRow);
- });
- } else {
- const tr = document.createElement('tr');
- const td = document.createElement('td');
- td.colSpan = 6;
- td.textContent = 'No Products Found.';
- tr.appendChild(td);
- tbody.appendChild(tr);
- }
- table.appendChild(tbody);
- wrap.appendChild(table);
- // --- Select All Checkbox Logic ---
- selectAllCheckbox.addEventListener('change', () => {
- // ✅ Only get product checkboxes inside *this* table
- const allCheckboxes = table.querySelectorAll('tbody .checkbox-productlist');
- console.log("allCheckboxes", allCheckboxes);
- allCheckboxes.forEach(cb => {
- cb.checked = selectAllCheckbox.checked;
- cb.dispatchEvent(new Event('change'));
- });
- });
- // --- Keep "Select All" synced with individual selections ---
- tbody.addEventListener('change', (e) => {
- if (e.target && e.target.classList.contains('checkbox-productlist')) {
- // ✅ Again, limit scope to *this* table
- const allCheckboxes = table.querySelectorAll('tbody .checkbox-productlist');
- const allChecked = Array.from(allCheckboxes).every(cb => cb.checked);
- const someChecked = Array.from(allCheckboxes).some(cb => cb.checked);
- selectAllCheckbox.checked = allChecked;
- selectAllCheckbox.indeterminate = !allChecked && someChecked;
- }
- });
- }
- // function renderInlineForCards() {
- // const api = API_RESPONSE_AI;
- // // Clear all inline sections first
- // document.querySelectorAll('.attr-inline').forEach(div => div.innerHTML = '');
- // PRODUCT_BASE.forEach((p, idx) => {
- // const inline = document.querySelector(`.attr-inline[data-pid="${p.item_id}"]`);
- // if (!inline) return;
-
- // // --- CHANGE HERE: Use the new helper function ---
- // if (!isProductSelected(p.item_id)) return; // only show for selected
-
- // const res = findApiResultForProduct(p, idx, api);
- // const pid = p.item_id;
- // if (!lastSeen.has(pid)) lastSeen.set(pid, { mandatory: new Map(), additional: new Map(), ocr_results: new Map(), visual_results: new Map() });
- // const mem = lastSeen.get(pid);
- // // Build sections
- // const manTitle = el('div', 'section-title'); manTitle.innerHTML = '<strong>Mandatory</strong>';
- // const manChips = el('div', 'chips');
- // const addTitle = el('div', 'section-title'); addTitle.innerHTML = '<strong>Additional</strong>';
- // const addChips = el('div', 'chips');
- // const addOcr = el('div', 'section-title'); addOcr.innerHTML = '<strong>Ocr</strong>';
- // const ocrChips = el('div', 'chips');
- // const addVisual = el('div', 'section-title'); addVisual.innerHTML = '<strong>Visual</strong>';
- // const visualChips = el('div', 'chips');
-
- // const mandCount = renderChips(manChips, res?.mandatory || {}, mem.mandatory);
- // const addCount = renderChips(addChips, res?.additional || {}, mem.additional);
- // const ocrCount = renderChips(ocrChips, res?.ocr_results?.extracted_attributes || {}, mem?.ocr_results);
- // const visualCount = renderChips(visualChips, res?.visual_results?.visual_attributes || {}, mem?.visual_results);
- // const counts = el('div'); counts.style.display = 'flex'; counts.style.gap = '8px'; counts.style.margin = '8px 0 0';
- // const c1 = el('span', 'pill'); c1.textContent = `Mandatory: ${mandCount}`;
- // const c2 = el('span', 'pill'); c2.textContent = `Additional: ${addCount}`;
- // const c3 = el('span', 'pill'); c3.textContent = `OCR: ${ocrCount}`;
- // const c4 = el('span', 'pill'); c4.textContent = `Visuals: ${visualCount}`;
- // counts.appendChild(c1); counts.appendChild(c2); counts.appendChild(c3); counts.appendChild(c4);
- // inline.appendChild(manTitle); inline.appendChild(manChips);
- // inline.appendChild(addTitle); inline.appendChild(addChips);
- // inline.appendChild(addOcr); inline.appendChild(ocrChips);
- // inline.appendChild(addVisual); inline.appendChild(visualChips);
- // inline.appendChild(counts);
- // });
- // // Update summary
- // $('#statTotal').textContent = api.total_products ?? 0;
- // $('#statOk').textContent = api.successful ?? 0;
- // $('#statKo').textContent = api.failed ?? 0;
- // $('#api-summary').style.display = 'block';
- // }
- // -----------------------------------------------------------
- // function renderInlineForTable() {
- // const api = API_RESPONSE_AI;
- // const table = $('#tableContainer');
- // if (!table) return;
- // // Remove existing detail rows
- // table.querySelectorAll('tr.detail-row').forEach(r => r.remove());
- // PRODUCT_BASE.forEach((p, idx) => {
- // // --- CHANGE HERE: Use the new helper function ---
- // if (!isProductSelected(p.item_id)) return;
-
- // const res = findApiResultForProduct(p, idx, api);
- // const pid = p.item_id;
- // if (!lastSeen.has(pid)) lastSeen.set(pid, { mandatory: new Map(), additional: new Map(), ocr_results: new Map(), visual_results: new Map() });
- // const mem = lastSeen.get(pid);
- // const tbody = table.querySelector('tbody');
- // // NOTE: The table rendering uses p.id for the row ID: `row-${p.id}`.
- // // Assuming p.id is still valid for finding the base row, as your original code used it.
- // const baseRow = tbody.querySelector(`#row-${p.id}`);
- // if (!baseRow) return;
- // const detail = el('tr', 'detail-row');
- // const td = el('td'); td.colSpan = 6; // number of columns
- // const content = el('div', 'detail-content');
- // const manTitle = el('div', 'section-title'); manTitle.innerHTML = '<strong>Mandatory</strong>';
- // const manChips = el('div', 'chips');
- // const addTitle = el('div', 'section-title'); addTitle.innerHTML = '<strong>Additional</strong>';
- // const addChips = el('div', 'chips');
- // const addOcr = el('div', 'section-title'); addOcr.innerHTML = '<strong>Ocr</strong>';
- // const ocrChips = el('div', 'chips');
- // const addVisuals = el('div', 'section-title'); addVisuals.innerHTML = '<strong>Visuals</strong>';
- // const visualsChips = el('div', 'chips');
- // const mandCount = renderChips(manChips, res?.mandatory || {}, mem.mandatory);
- // const addCount = renderChips(addChips, res?.additional || {}, mem.additional);
- // const ocrCount = renderChips(ocrChips, res?.ocr_results?.extracted_attributes || {}, mem.ocr_results);
- // const visualCount = renderChips(visualsChips, res?.visual_results?.visual_attributes || {}, mem.visual_results);
- // const counts = el('div'); counts.style.display = 'flex'; counts.style.gap = '8px'; counts.style.margin = '8px 0 0';
- // const c1 = el('span', 'pill'); c1.textContent = `Mandatory: ${mandCount}`;
- // const c2 = el('span', 'pill'); c2.textContent = `Additional: ${addCount}`;
- // const c3 = el('span', 'pill'); c3.textContent = `Ocr: ${ocrCount}`;
- // const c4 = el('span', 'pill'); c4.textContent = `Visuals: ${visualCount}`;
- // counts.appendChild(c1); counts.appendChild(c2); counts.appendChild(c3); counts.appendChild(c4);
- // content.appendChild(manTitle); content.appendChild(manChips);
- // content.appendChild(addTitle); content.appendChild(addChips);
- // content.appendChild(addOcr); content.appendChild(ocrChips);
- // content.appendChild(addVisuals); content.appendChild(visualsChips);
- // content.appendChild(counts);
- // td.appendChild(content); detail.appendChild(td);
- // // insert after base row
- // baseRow.insertAdjacentElement('afterend', detail);
- // });
- // // Update summary
- // $('#statTotal').textContent = api.total_products ?? 0;
- // $('#statOk').textContent = api.successful ?? 0;
- // $('#statKo').textContent = api.failed ?? 0;
- // $('#api-summary').style.display = 'block';
- // }
- function renderInlineForCards() {
- const api = API_RESPONSE_AI;
- document.querySelectorAll('.attr-inline').forEach(div => div.innerHTML = '');
- PRODUCT_BASE.forEach((p, idx) => {
- const inline = document.querySelector(`.attr-inline[data-pid="${p.item_id}"]`);
- if (!inline) return;
-
- if (!isProductSelected(p.item_id)) return;
-
- const res = findApiResultForProduct(p, idx, api);
-
- // Clear existing content
- inline.innerHTML = '';
- // 1. MANDATORY SECTION: RENDER AS COMPARISON CARDS
- const mandatorySection = renderMandatoryComparisonCards(
- res?.mandatory || {},
- 'Mandatory Attributes Comparison'
- );
- inline.appendChild(mandatorySection);
- const mandCount = Object.keys(res?.mandatory || {}).length;
-
- const combinedTitle = el('div', 'section-title');
- combinedTitle.innerHTML = '<h2>Additional & AI-Driven Attributes</h2>';
- inline.appendChild(combinedTitle);
- const combinedAttributesContainer = el('div', 'combined-attributes-container');
- // 2. ADDITIONAL SECTION: RENDER AS SIMPLE TABLE
- const additionalSection = renderAttributesAsTable(
- res?.additional || {},
- 'Additional Attributes'
- );
- // inline.appendChild(additionalSection);
- const addCount = Object.keys(res?.additional || {}).length;
-
- // 3. OCR SECTION: RENDER AS SIMPLE TABLE
- const ocrSection = renderAttributesAsTable(
- res?.ocr_results?.extracted_attributes || {},
- 'OCR Results'
- );
- // inline.appendChild(ocrSection);
- const ocrCount = Object.keys(res?.ocr_results?.extracted_attributes || {}).length;
-
- // 4. VISUAL SECTION: RENDER AS SIMPLE TABLE
- const visualSection = renderAttributesAsTable(
- res?.visual_results?.visual_attributes || {},
- 'Visual Attributes'
- );
- // inline.appendChild(visualSection);
- const visualCount = Object.keys(res?.visual_results?.visual_attributes || {}).length;
- combinedAttributesContainer.appendChild(additionalSection);
- combinedAttributesContainer.appendChild(ocrSection);
- combinedAttributesContainer.appendChild(visualSection);
- inline.appendChild(combinedAttributesContainer);
- // --- Summary Counts (Pills) ---
- const counts = el('div'); counts.style.display = 'flex'; counts.style.gap = '8px'; counts.style.margin = '8px 0 0';
- const c1 = el('span', 'pill'); c1.textContent = `Mandatory: ${mandCount}`;
- const c2 = el('span', 'pill'); c2.textContent = `Additional: ${addCount}`;
- const c3 = el('span', 'pill'); c3.textContent = `OCR: ${ocrCount}`;
- const c4 = el('span', 'pill'); c4.textContent = `Visuals: ${visualCount}`;
- counts.appendChild(c1); counts.appendChild(c2); counts.appendChild(c3); counts.appendChild(c4);
- inline.appendChild(counts);
- });
- // Update summary
- $('#statTotal').textContent = api.total_products ?? 0;
- $('#statOk').textContent = api.successful ?? 0;
- $('#statKo').textContent = api.failed ?? 0;
- $('#api-summary').style.display = 'block';
- }
- // function renderInlineForCards() {
- // const api = API_RESPONSE_AI;
- // // Clear all inline sections first
- // document.querySelectorAll('.attr-inline').forEach(div => div.innerHTML = '');
- // PRODUCT_BASE.forEach((p, idx) => {
- // const inline = document.querySelector(`.attr-inline[data-pid="${p.item_id}"]`);
- // if (!inline) return;
-
- // if (!isProductSelected(p.item_id)) return;
-
- // const res = findApiResultForProduct(p, idx, api);
- // const pid = p.item_id;
- // // Memory map (mem) is no longer needed since we removed chip rendering
- // // I'll keep the variable declarations for count consistency, but remove the memory map usage.
- // // --- 1. MANDATORY SECTION: RENDER AS COMPARISON CARDS ---
- // const mandatorySection = renderMandatoryComparisonCards(
- // res?.mandatory || {},
- // 'Mandatory Attributes Comparison'
- // );
- // inline.appendChild(mandatorySection);
-
- // const mandCount = Object.keys(res?.mandatory || {}).length;
-
- // // --- 2. ADDITIONAL SECTION: RENDER AS SIMPLE CARDS ---
- // const additionalSection = renderAttributesAsTable(
- // res?.additional || {},
- // 'Additional Attributes'
- // );
- // inline.appendChild(additionalSection);
- // const addCount = Object.keys(res?.additional || {}).length;
-
- // // --- 3. OCR SECTION: RENDER AS SIMPLE CARDS ---
- // const ocrSection = renderAttributesAsTable(
- // res?.ocr_results?.extracted_attributes || {},
- // 'OCR Results'
- // );
- // inline.appendChild(ocrSection);
- // const ocrCount = Object.keys(res?.ocr_results?.extracted_attributes || {}).length;
-
- // // --- 4. VISUAL SECTION: RENDER AS SIMPLE CARDS ---
- // const visualSection = renderAttributesAsTable(
- // res?.visual_results?.visual_attributes || {},
- // 'Visual Results'
- // );
- // inline.appendChild(visualSection);
- // const visualCount = Object.keys(res?.visual_results?.visual_attributes || {}).length;
-
- // // --- Summary Counts (Pills) ---
- // const counts = el('div'); counts.style.display = 'flex'; counts.style.gap = '8px'; counts.style.margin = '8px 0 0';
- // const c1 = el('span', 'pill'); c1.textContent = `Mandatory: ${mandCount}`;
- // const c2 = el('span', 'pill'); c2.textContent = `Additional: ${addCount}`;
- // const c3 = el('span', 'pill'); c3.textContent = `OCR: ${ocrCount}`;
- // const c4 = el('span', 'pill'); c4.textContent = `Visuals: ${visualCount}`;
- // counts.appendChild(c1); counts.appendChild(c2); counts.appendChild(c3); counts.appendChild(c4);
- // inline.appendChild(counts);
- // });
- // // Update summary
- // $('#statTotal').textContent = api.total_products ?? 0;
- // $('#statOk').textContent = api.successful ?? 0;
- // $('#statKo').textContent = api.failed ?? 0;
- // $('#api-summary').style.display = 'block';
- // }
- // ----------------------------------------------------------------
- // NOTE: You MUST include renderMandatoryComparisonCards (from previous response)
- // and the necessary CSS for both card styles!
- // ----------------------------------------------------------------
- // ------------------------------------------------------------------
- // --- 1. MANDATORY COMPARISON HELPER (Existing vs. AI, with Highlighting) ---
- // ------------------------------------------------------------------
- // function renderInlineForCards() {
- // const api = API_RESPONSE_AI;
- // // Clear all inline sections first
- // document.querySelectorAll('.attr-inline').forEach(div => div.innerHTML = '');
- // PRODUCT_BASE.forEach((p, idx) => {
- // const inline = document.querySelector(`.attr-inline[data-pid="${p.item_id}"]`);
- // if (!inline) return;
-
- // // --- CHANGE HERE: Use the new helper function ---
- // if (!isProductSelected(p.item_id)) return; // only show for selected
-
- // const res = findApiResultForProduct(p, idx, api);
- // const pid = p.item_id;
- // // Keep memory logic for old chip renderer, even though card renderer doesn't need it
- // if (!lastSeen.has(pid)) lastSeen.set(pid, { mandatory: new Map(), additional: new Map(), ocr_results: new Map(), visual_results: new Map() });
- // const mem = lastSeen.get(pid);
- // // ------------------------------------------------
- // // 1. MANDATORY SECTION: RENDER AS COMPARISON CARDS
- // // ------------------------------------------------
- // const mandatorySection = renderMandatoryComparisonCards(
- // res?.mandatory || {},
- // 'Mandatory Attributes Comparison'
- // );
- // inline.appendChild(mandatorySection);
-
- // // Count the attributes for the summary pill
- // const mandCount = Object.keys(res?.mandatory || {}).length;
-
- // // ------------------------------------------------
- // // 2. ADDITIONAL/OCR/VISUALS: RENDER AS CHIPS (Original Logic)
- // // ------------------------------------------------
-
- // // ADDITIONAL
- // const addTitle = el('div', 'section-title'); addTitle.innerHTML = '<strong>Additional</strong>';
- // const addChips = el('div', 'chips');
- // const addCount = renderChips(addChips, res?.additional || {}, mem.additional);
- // inline.appendChild(addTitle); inline.appendChild(addChips);
-
- // // OCR
- // const addOcr = el('div', 'section-title'); addOcr.innerHTML = '<strong>Ocr</strong>';
- // const ocrChips = el('div', 'chips');
- // const ocrCount = renderChips(ocrChips, res?.ocr_results?.extracted_attributes || {}, mem?.ocr_results);
- // inline.appendChild(addOcr); inline.appendChild(ocrChips);
-
- // // VISUALS
- // const addVisual = el('div', 'section-title'); addVisual.innerHTML = '<strong>Visual</strong>';
- // const visualChips = el('div', 'chips');
- // const visualCount = renderChips(visualChips, res?.visual_results?.visual_attributes || {}, mem?.visual_results);
- // inline.appendChild(addVisual); inline.appendChild(visualChips);
-
- // // --- Summary Counts (Pills) ---
- // const counts = el('div'); counts.style.display = 'flex'; counts.style.gap = '8px'; counts.style.margin = '8px 0 0';
- // const c1 = el('span', 'pill'); c1.textContent = `Mandatory: ${mandCount}`;
- // const c2 = el('span', 'pill'); c2.textContent = `Additional: ${addCount}`;
- // const c3 = el('span', 'pill'); c3.textContent = `OCR: ${ocrCount}`;
- // const c4 = el('span', 'pill'); c4.textContent = `Visuals: ${visualCount}`;
- // counts.appendChild(c1); counts.appendChild(c2); counts.appendChild(c3); counts.appendChild(c4);
- // inline.appendChild(counts);
- // });
- // // Update summary
- // $('#statTotal').textContent = api.total_products ?? 0;
- // $('#statOk').textContent = api.successful ?? 0;
- // $('#statKo').textContent = api.failed ?? 0;
- // $('#api-summary').style.display = 'block';
- // }
- /**
- * Renders a table for Mandatory attributes, comparing AI-extracted value ('value')
- * against the existing value ('original_value'). Includes a scroll wrapper.
- * @param {Object} attributes - The mandatory attribute data.
- * @param {string} title - The title for the table section.
- * @returns {HTMLElement} A div containing the comparison table.
- */
- // function renderMandatoryComparisonTable(attributes, title) {
- // const section = el('div', 'attribute-section');
-
- // let attributeEntries = [];
- // Object.keys(attributes).forEach(key => {
- // const valuesArray = Array.isArray(attributes[key]) ? attributes[key] : [attributes[key]];
- // valuesArray.forEach(v => {
- // const aiValue = v.value || 'N/A';
- // const originalValue = v.original_value || 'N/A';
- // const source = v.source || 'N/A';
-
- // // Comparison is case-insensitive and ignores leading/trailing whitespace
- // const isMatch = (String(aiValue).trim().toLowerCase() === String(originalValue).trim().toLowerCase());
- // attributeEntries.push({
- // name: key,
- // aiValue: aiValue,
- // originalValue: originalValue,
- // source: source,
- // isMatch: isMatch
- // });
- // });
- // });
- // const titleEl = el('div', 'section-title');
- // titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- // section.appendChild(titleEl);
-
- // if (attributeEntries.length === 0) {
- // const msg = el('p', 'no-attributes-message');
- // msg.textContent = `No ${title.toLowerCase()} found.`;
- // section.appendChild(msg);
- // return section;
- // }
- // // --- SCROLL WRAPPER ADDITION ---
- // const scrollWrapper = el('div', 'attribute-scroll-wrapper');
- // const table = el('table', 'attribute-detail-table comparison-table');
-
- // const thead = el('thead');
- // const headerRow = el('tr');
-
- // ['Attribute Name', 'Source', 'Manually Identified Value', 'AI Generated Value'].forEach(text => {
- // const th = el('th');
- // th.textContent = text;
- // headerRow.appendChild(th);
- // });
- // thead.appendChild(headerRow);
- // table.appendChild(thead);
- // const tbody = el('tbody');
- // attributeEntries.forEach(attr => {
- // // Highlight the entire row in red if the values do not match
- // const row = el('tr', attr.isMatch ? 'match' : 'mismatch-row');
-
- // // 1. Attribute Name
- // const nameTd = el('td', 'attribute-name');
- // nameTd.textContent = attr.name.replace(/_/g, ' ');
- // row.appendChild(nameTd);
- // // 2. Source
- // const sourceTd = el('td', 'attribute-source');
- // sourceTd.textContent = formatString(attr.source);
- // row.appendChild(sourceTd);
- // // 3. Existing Value
- // const originalTd = el('td', 'original-value');
- // originalTd.textContent = formatString(attr.originalValue);
- // row.appendChild(originalTd);
- // // 4. AI Extracted Value (Highlight if mismatch)
- // const aiTd = el('td', `ai-value ${attr.aiValue ? '' : 'mismatch-value'}`);
- // aiTd.textContent = attr.aiValue;
- // row.appendChild(aiTd);
-
- // // 5. Match Status
- // // const matchTd = el('td', 'match-status');
- // // const matchPill = el('span', `pill status-pill status-${attr.isMatch ? 'match' : 'mismatch'}`);
- // // matchPill.textContent = attr.isMatch ? '✅ MATCH' : '❌ MISMATCH';
- // // matchTd.appendChild(matchPill);
- // // row.appendChild(matchTd);
- // tbody.appendChild(row);
- // });
- // table.appendChild(tbody);
- // scrollWrapper.appendChild(table); // Append table to wrapper
- // section.appendChild(scrollWrapper); // Append wrapper to section
- // return section;
- // }
- // ------------------------------------------------------------------
- // --- 2. GENERAL ATTRIBUTE HELPER (Name, Value, Source, with Scroll) ---
- // ------------------------------------------------------------------
- /**
- * Renders a table for Mandatory attributes, comparing AI-extracted value ('value')
- * against the existing value ('original_value'). Includes a scroll wrapper and mismatch highlighting.
- * @param {Object} attributes - The mandatory attribute data.
- * @param {string} title - The title for the table section.
- * @returns {HTMLElement} A div containing the comparison table.
- */
- function renderMandatoryComparisonTable(attributes, title, productType) {
- const section = el('div', 'attribute-section');
- let attributeEntries = [];
- // --- Build attributeEntries ---
- Object.keys(attributes).forEach(key => {
- const valuesArray = Array.isArray(attributes[key]) ? attributes[key] : [attributes[key]];
- valuesArray.forEach(v => {
- const aiValue = v.value || 'N/A';
- const originalValue = v.original_value || 'N/A';
- const source = v.source || 'N/A';
- const reason = v.reason || 'N/A';
- // Find possible values for this attribute from full data
- const attrConfig = attributesFullData.find(item => item.attribute_name === key);
-
- let possibleValues = [];
- if (attrConfig && attrConfig.possible_values) {
- possibleValues = attrConfig.possible_values.split(',').map(s => s.trim());
- }
- // Determine match flag
- const isFoundInPossible = possibleValues.includes(aiValue);
- const matchFlag = isFoundInPossible ? true : false;
- attributeEntries.push({
- name: key,
- aiValue: aiValue,
- possibleValues: possibleValues,
- originalValue: originalValue,
- source: source,
- isMatch: matchFlag,
- reason: reason
- });
- });
- });
- // --- Section title ---
- const titleEl = el('div', 'section-title');
- titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- section.appendChild(titleEl);
- if (attributeEntries.length === 0) {
- const msg = el('p', 'no-attributes-message');
- msg.textContent = `No ${title.toLowerCase()} found.`;
- section.appendChild(msg);
- return section;
- }
- // --- Split attributes in half ---
- const midIndex = Math.ceil(attributeEntries.length / 2);
- const leftAttributes = attributeEntries.slice(0, midIndex);
- const rightAttributes = attributeEntries.slice(midIndex);
- // --- Create a container for two tables ---
- const tableContainer = el('div', 'two-column-table-container');
- // Helper function to build a table
- function buildTable(attrArray) {
- const scrollWrapper = el('div', 'attribute-scroll-wrapper');
- const table = el('table', 'attribute-detail-table comparison-table');
- const thead = el('thead');
- const headerRow = el('tr');
- ['Attribute Name', 'AI Generated Value', 'Action'].forEach(text => {
- const th = el('th');
- th.textContent = text;
- headerRow.appendChild(th);
- });
- thead.appendChild(headerRow);
- table.appendChild(thead);
- const tbody = el('tbody');
- attrArray.forEach(attr => {
- const row = el('tr', attr.isMatch ? 'match-row' : 'mismatch-row');
- // Attribute name
- const nameTd = el('td', 'attribute-name');
- nameTd.textContent = formatString(attr.name).replace(/_/g, ' ');
- row.appendChild(nameTd);
- // AI Value display
- // AI Value cell with info icon
- const aiValueTd = el('td', `ai-value ${attr.isMatch ? '' : 'mismatch-value'}`);
- // Create a wrapper for text and icon
- const aiValueWrapper = document.createElement('div');
- aiValueWrapper.style.display = 'flex';
- aiValueWrapper.style.alignItems = 'center';
- aiValueWrapper.style.gap = '6px';
- // Text node
- const valueText = document.createElement('span');
- valueText.textContent = formatString(attr.aiValue);
- aiValueWrapper.appendChild(valueText);
- // Info icon (Bootstrap Icons)
- if (attr.reason && attr.reason.trim() !== '') {
- const infoIcon = document.createElement('i');
- infoIcon.className = 'bi bi-info-circle';
- infoIcon.style.cursor = 'pointer';
- infoIcon.setAttribute('title', attr.reason); // Tooltip on hover
- infoIcon.style.color = '#0d6efd'; // Bootstrap blue
- aiValueWrapper.appendChild(infoIcon);
- }
- aiValueTd.appendChild(aiValueWrapper);
- row.appendChild(aiValueTd);
- // const aiValueTd = el('td', `ai-value ${attr.isMatch ? '' : 'mismatch-value'}`);
- // aiValueTd.textContent = formatString(attr.aiValue);
- // row.appendChild(aiValueTd);
- // Dropdown
- // const aiTd = el('td', 'attribute-source');
- // const select = document.createElement('select');
- // select.classList.add('select2-dropdown');
- // select.setAttribute('data-attribute', attr.name);
-
- const aiTd = el('td', 'attribute-source');
- // Create a multi-select dropdown
- const select = document.createElement('select');
- select.classList.add('select2-dropdown');
- // select.id = 'manually-attributes'; // You may want to make this unique per row
- select.name = 'manuallyUpdatedAttributes[]';
- // select.setAttribute('aria-labelledby', 'select a attribute for which multiple data required');
- select.setAttribute('multiple', 'multiple');
- // select.style.width = '100%';
- select.setAttribute('data-attribute', attr.name);
- // Populate options
- attr.possibleValues.forEach(val => {
- const option = document.createElement('option');
- option.value = val;
- option.textContent = val;
- if (val === attr.aiValue) option.selected = true;
- select.appendChild(option);
- });
- if (!attr.isMatch && attr.aiValue !== 'N/A') {
- const newOpt = document.createElement('option');
- newOpt.value = attr.aiValue;
- newOpt.textContent = attr.aiValue; // + " (new)";
- newOpt.selected = true;
- select.appendChild(newOpt);
- }
- aiTd.appendChild(select);
- row.appendChild(aiTd);
- tbody.appendChild(row);
- // Initialize Select2
- jQuery(select).select2({
- tags: true,
- width: 'resolve'
- });
- });
- table.appendChild(tbody);
- scrollWrapper.appendChild(table);
- return scrollWrapper;
- }
- // --- Build and append both tables ---
- const leftTable = buildTable(leftAttributes);
- const rightTable = buildTable(rightAttributes);
- tableContainer.appendChild(leftTable);
- tableContainer.appendChild(rightTable);
- section.appendChild(tableContainer);
- return section;
- }
- // function renderMandatoryComparisonTable(attributes, title, productType) {
- // const section = el('div', 'attribute-section');
- // let attributeEntries = [];
- // Object.keys(attributes).forEach(key => {
- // const valuesArray = Array.isArray(attributes[key]) ? attributes[key] : [attributes[key]];
- // valuesArray.forEach(v => {
- // const aiValue = v.value || 'N/A';
- // const originalValue = v.original_value || 'N/A';
- // const source = v.source || 'N/A';
- // // Find possible values for this attribute from `a`
- // const attrConfig = attributesFullData.find(item =>
- // item.attribute_name === key
- // );
-
- // let possibleValues = [];
- // if (attrConfig && attrConfig.possible_values) {
- // possibleValues = attrConfig.possible_values.split(',').map(s => s.trim());
- // }
- // // Determine if AI value exists in possible values
- // const isFoundInPossible = possibleValues.includes(aiValue);
- // const matchFlag = isFoundInPossible ? true : false;
- // attributeEntries.push({
- // name: key,
- // aiValue: aiValue,
- // possibleValues: possibleValues,
- // originalValue: originalValue,
- // source: source,
- // isMatch: matchFlag
- // });
- // });
- // });
- // const titleEl = el('div', 'section-title');
- // titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- // section.appendChild(titleEl);
- // if (attributeEntries.length === 0) {
- // const msg = el('p', 'no-attributes-message');
- // msg.textContent = `No ${title.toLowerCase()} found.`;
- // section.appendChild(msg);
- // return section;
- // }
- // const scrollWrapper = el('div', 'attribute-scroll-wrapper');
- // const table = el('table', 'attribute-detail-table comparison-table');
- // const thead = el('thead');
- // const headerRow = el('tr');
- // ['Attribute Name', 'AI Generated Value', 'Action'].forEach(text => {
- // const th = el('th');
- // th.textContent = text;
- // headerRow.appendChild(th);
- // });
- // thead.appendChild(headerRow);
- // table.appendChild(thead);
- // const tbody = el('tbody');
- // attributeEntries.forEach(attr => {
- // const row = el('tr', attr.isMatch ? 'match-row' : 'mismatch-row');
- // // 1. Attribute Name
- // const nameTd = el('td', 'attribute-name');
- // nameTd.textContent = formatString(attr.name).replace(/_/g, ' ');
- // row.appendChild(nameTd);
- // // 3. Source Column
- // const sourceTd = el('td', `ai-value ${attr.isMatch ? '' : 'mismatch-value'}`);
- // sourceTd.textContent = formatString(attr.aiValue);
- // row.appendChild(sourceTd);
- // // 2. AI Value Dropdown (Select2)
- // const aiTd = el('td', 'attribute-source');
- // const select = document.createElement('select');
- // select.classList.add('select2-dropdown');
- // select.setAttribute('data-attribute', attr.name);
- // // Populate possible values
- // attr.possibleValues.forEach(val => {
- // const option = document.createElement('option');
- // option.value = val;
- // option.textContent = val;
- // if (val === attr.aiValue) option.selected = true;
- // select.appendChild(option);
- // });
- // // If not found in possible values, add it as a new option
- // if (!attr.isMatch && attr.aiValue !== 'N/A') {
- // const newOpt = document.createElement('option');
- // newOpt.value = attr.aiValue;
- // newOpt.textContent = attr.aiValue + " (new)";
- // newOpt.selected = true;
- // select.appendChild(newOpt);
- // }
- // aiTd.appendChild(select);
- // row.appendChild(aiTd);
- // tbody.appendChild(row);
- // // Initialize Select2 after element is added
- // jQuery(select).select2({
- // tags: true, // allows new values
- // width: 'resolve'
- // });
- // });
- // table.appendChild(tbody);
- // scrollWrapper.appendChild(table);
- // section.appendChild(scrollWrapper);
- // return section;
- // }
- // function renderMandatoryComparisonTable(attributes, title) {
- // const section = el('div', 'attribute-section');
-
- // let attributeEntries = [];
- // Object.keys(attributes).forEach(key => {
- // const valuesArray = Array.isArray(attributes[key]) ? attributes[key] : [attributes[key]];
- // valuesArray.forEach(v => {
- // const aiValue = v.value || 'N/A';
- // const originalValue = v.original_value || 'N/A';
- // const source = v.source || 'N/A';
-
- // // Comparison is case-insensitive and ignores leading/trailing whitespace
- // const isMatch = (String(aiValue).trim().toLowerCase() === String(originalValue).trim().toLowerCase());
- // attributeEntries.push({
- // name: key,
- // aiValue: aiValue,
- // originalValue: originalValue,
- // source: source,
- // isMatch: isMatch
- // });
- // });
- // });
- // const titleEl = el('div', 'section-title');
- // titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- // section.appendChild(titleEl);
-
- // if (attributeEntries.length === 0) {
- // const msg = el('p', 'no-attributes-message');
- // msg.textContent = `No ${title.toLowerCase()} found.`;
- // section.appendChild(msg);
- // return section;
- // }
- // // --- SCROLL WRAPPER ADDITION ---
- // const scrollWrapper = el('div', 'attribute-scroll-wrapper');
- // const table = el('table', 'attribute-detail-table comparison-table');
-
- // const thead = el('thead');
- // const headerRow = el('tr');
-
- // // Updated Headers for Comparison Table
- // ['Attribute Name', 'AI Generated Value', 'Action'].forEach(text => {
- // const th = el('th');
- // th.textContent = text;
- // headerRow.appendChild(th);
- // });
- // thead.appendChild(headerRow);
- // table.appendChild(thead);
- // const tbody = el('tbody');
- // attributeEntries.forEach(attr => {
- // // Highlight the entire row in red if the values do not match
- // const row = el('tr', attr.isMatch ? 'match-row' : 'mismatch-row');
-
- // // 1. Attribute Name
- // const nameTd = el('td', 'attribute-name');
- // nameTd.textContent = formatString(attr.name).replace(/_/g, ' ');
- // row.appendChild(nameTd);
- // // 3. AI Extracted Value
- // const aiTd = el('td', `ai-value ${attr.isMatch ? '' : 'mismatch-value'}`);
- // aiTd.textContent = attr.aiValue;
- // row.appendChild(aiTd);
-
- // // 4. Source
- // const sourceTd = el('td', 'attribute-source');
- // sourceTd.textContent = formatString(attr.source);
- // row.appendChild(sourceTd);
- // tbody.appendChild(row);
- // });
- // table.appendChild(tbody);
- // scrollWrapper.appendChild(table); // Append table to wrapper
- // section.appendChild(scrollWrapper); // Append wrapper to section
- // return section;
- // }
- /**
- * Renders a table for Additional, OCR, or Visual attributes (Name, Value, Source).
- * @param {Object} attributes - The attribute data.
- * @param {string} title - The title for the table section.
- * @returns {HTMLElement} A div containing the table.
- */
- function renderAttributesAsTable(attributes, title, mandatoryData = null) {
- const section = el('div', 'attribute-section');
- let attributeEntries = [];
- // --- STEP 1: Create a Set of all Mandatory Original Values (Normalized) ---
- // A Set is used for fast lookups. Values are normalized (trimmed, lowercase)
- const mandatoryOriginalValuesSet = new Set();
- if (mandatoryData) {
- Object.values(mandatoryData).forEach(attrArray => {
- const originalValue = attrArray[0]?.original_value;
- if (originalValue) {
- mandatoryOriginalValuesSet.add(String(originalValue).trim().toLowerCase());
- }
- });
- }
- // Helper to extract attribute entries consistently
- const processAttribute = (key, values) => {
- const valuesArray = Array.isArray(values) ? values : [values];
- valuesArray.forEach(v => {
- attributeEntries.push({
- name: key,
- value: v.value,
- source: v.source || 'N/A'
- });
- });
- };
- // Iterate through attributes (OCR/Visual/Additional) and flatten them
- Object.keys(attributes).forEach(key => {
- const attribute = attributes[key];
-
- if (Array.isArray(attribute)) {
- processAttribute(key, attribute);
- } else if (typeof attribute === 'object' && attribute !== null) {
- // Handle simple { "key": { "value": "X", "source": "Y" } } structure
- if (attribute.value !== undefined) {
- attributeEntries.push({
- name: key,
- value: attribute.value,
- source: attribute.source || 'N/A'
- });
- } else {
- // Handle nested objects
- Object.keys(attribute).forEach(subKey => {
- const subAttribute = attribute[subKey];
- if (Array.isArray(subAttribute)) {
- processAttribute(`${key} (${subKey.replace(/_/g, ' ')})`, subAttribute);
- }
- });
- }
- }
- });
- const titleEl = el('div', 'section-title');
- titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- section.appendChild(titleEl);
-
- if (attributeEntries.length === 0) {
- const msg = el('p', 'no-attributes-message');
- msg.textContent = `No ${title.toLowerCase()} found.`;
- section.appendChild(msg);
- return section;
- }
- const scrollWrapper = el('div', 'attribute-scroll-wrapper');
- const table = el('table', 'attribute-detail-table');
- const thead = el('thead');
- const headerRow = el('tr');
-
- ['Attribute Name', 'Value'].forEach(text => {
- const th = el('th');
- th.textContent = text;
- headerRow.appendChild(th);
- });
- thead.appendChild(headerRow);
- table.appendChild(thead);
- const tbody = el('tbody');
- attributeEntries.forEach(attr => {
- const row = el('tr');
-
- // --- CORE COMPARISON LOGIC (Global Check) ---
- let colorClass = '';
- const hasMandatoryBaseline = mandatoryOriginalValuesSet.size > 0;
-
- if (hasMandatoryBaseline) {
- // Normalize the current attribute's value
- const currentValueNormalized = String(attr.value).trim().toLowerCase();
-
- // Check if the current value exists ANYWHERE in the mandatory original values set
- if (mandatoryOriginalValuesSet.has(currentValueNormalized)) {
- colorClass = 'green-text'; // Found a match in the global mandatory set
- } else {
- colorClass = 'red-text'; // Did NOT find a match
- }
- }
- // --- END CORE COMPARISON LOGIC ---
-
- const nameTd = el('td', 'attribute-name');
- nameTd.textContent = formatString(attr.name).replace(/_/g, ' ');
- row.appendChild(nameTd);
- const valueTd = el('td', 'attribute-value');
- const displayValue = formatString(attr.value) + ' (' + formatString(attr.source) + ')';
-
- valueTd.textContent = displayValue || 'N/A';
-
- // Apply the determined color class to the value cell
- if (colorClass) {
- valueTd.classList.add(colorClass);
- }
- row.appendChild(valueTd);
- tbody.appendChild(row);
- });
- table.appendChild(tbody);
- scrollWrapper.appendChild(table);
- section.appendChild(scrollWrapper);
- return section;
- }
- // function renderAttributesAsTable(attributes, title, mandatoryData = null) {
- // const section = el('div', 'attribute-section');
- // let attributeEntries = [];
- // const processAttribute = (key, values) => {
- // const valuesArray = Array.isArray(values) ? values : [values];
- // valuesArray.forEach(v => {
- // attributeEntries.push({
- // name: key,
- // value: v.value,
- // source: v.source || 'N/A'
- // });
- // });
- // };
- // Object.keys(attributes).forEach(key => {
- // const attribute = attributes[key];
-
- // if (Array.isArray(attribute)) {
- // processAttribute(key, attribute);
- // } else if (typeof attribute === 'object' && attribute !== null) {
- // Object.keys(attribute).forEach(subKey => {
- // const subAttribute = attribute[subKey];
- // if (Array.isArray(subAttribute)) {
- // // Combines parent key (e.g., 'size') and sub-key (e.g., 'waist_size')
- // processAttribute(`${key} (${subKey.replace(/_/g, ' ')})`, subAttribute);
- // }
- // });
- // }
- // });
- // const titleEl = el('div', 'section-title');
- // titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- // section.appendChild(titleEl);
-
- // if (attributeEntries.length === 0) {
- // const msg = el('p', 'no-attributes-message');
- // msg.textContent = `No ${title.toLowerCase()} found.`;
- // section.appendChild(msg);
- // return section;
- // }
- // // --- SCROLL WRAPPER ADDITION ---
- // const scrollWrapper = el('div', 'attribute-scroll-wrapper');
- // const table = el('table', 'attribute-detail-table');
- // const thead = el('thead');
- // const headerRow = el('tr');
-
- // ['Attribute Name', 'Value'].forEach(text => {
- // const th = el('th');
- // th.textContent = text;
- // headerRow.appendChild(th);
- // });
- // thead.appendChild(headerRow);
- // table.appendChild(thead);
- // const tbody = el('tbody');
- // attributeEntries.forEach(attr => {
- // const row = el('tr');
-
- // const nameTd = el('td', 'attribute-name');
- // nameTd.textContent = formatString(attr.name).replace(/_/g, ' ');
- // row.appendChild(nameTd);
- // const valueTd = el('td', 'attribute-value');
- // const displayValue = Array.isArray(attr.value)
- // ? (attr.value.map(v => formatString(v.value))).join(', ') +'('+ formatString(attr.source)+ ')'
- // : formatString(attr.value) +'('+ formatString(attr.source)+ ')';
- // valueTd.textContent = displayValue || 'N/A';
- // row.appendChild(valueTd);
- // // const sourceTd = el('td', 'attribute-source');
- // // sourceTd.textContent = attr.source || 'Unknown';
- // // row.appendChild(sourceTd);
- // tbody.appendChild(row);
- // });
- // table.appendChild(tbody);
- // scrollWrapper.appendChild(table); // Append table to wrapper
- // section.appendChild(scrollWrapper); // Append wrapper to section
- // return section;
- // }
- // ------------------------------------------------------------------
- // --- 3. MAIN RENDER FUNCTION (REPLACEMENT) ---
- // ------------------------------------------------------------------
- // function renderInlineForTable() {
- // const api = API_RESPONSE_AI;
- // const table = $('#tableContainer');
- // if (!table) return;
- // // Remove existing detail rows
- // table.querySelectorAll('tr.detail-row').forEach(r => r.remove());
- // PRODUCT_BASE.forEach((p, idx) => {
- // if (!isProductSelected(p.item_id)) return;
-
- // const res = findApiResultForProduct(p, idx, api);
-
- // const tbody = table.querySelector('tbody');
- // const baseRow = tbody ? tbody.querySelector(`#row-${p.id}`) : null;
- // if (!baseRow) return;
- // // --- Detail Row Construction ---
- // const detail = el('tr', 'detail-row');
- // // td.colSpan must match the number of columns in your main table
- // const td = el('td'); td.colSpan = 7;
- // const content = el('div', 'detail-content-tables');
- // // // 1. MANDATORY Attributes Table (NOW USES CARD COMPARISON)
- // // const mandatorySection = renderMandatoryComparisonCards( // <-- NEW FUNCTION NAME
- // // res?.mandatory || {},
- // // 'Mandatory Attributes Comparison'
- // // );
- // // content.appendChild(mandatorySection);
- // // // 2. COMBINED Attributes Section (Additional, OCR, Visuals) - REMAINS THE SAME
- // // content.appendChild(el('hr', 'section-separator'));
- // // 1. MANDATORY Attributes Table (USES COMPARISON FUNCTION)
- // const mandatoryTable = renderMandatoryComparisonTable(
- // res?.mandatory || {},
- // 'Mandatory Attributes Comparison'
- // );
- // content.appendChild(mandatoryTable);
- // // 2. COMBINED Attributes Section (Additional, OCR, Visuals)
- // content.appendChild(el('hr', 'section-separator'));
-
- // const combinedTitle = el('div', 'section-title');
- // combinedTitle.innerHTML = '<h2>Additional & AI-Driven Attributes</h2>';
- // content.appendChild(combinedTitle);
-
- // const combinedAttributesContainer = el('div', 'combined-attributes-container');
-
- // // Use the general renderer for these sections
- // const additionalTable = renderAttributesAsTable(
- // res?.additional || {},
- // 'Additional Attributes'
- // );
-
- // const ocrTable = renderAttributesAsTable(
- // res?.ocr_results?.extracted_attributes || {},
- // 'OCR Results'
- // );
-
- // const visualsTable = renderAttributesAsTable(
- // res?.visual_results?.visual_attributes || {},
- // 'Visual Results'
- // );
- // // Append all sections to the combined container
- // combinedAttributesContainer.appendChild(additionalTable);
- // combinedAttributesContainer.appendChild(ocrTable);
- // combinedAttributesContainer.appendChild(visualsTable);
- // content.appendChild(combinedAttributesContainer);
-
- // // --- Summary Counts ---
- // const mandCount = Object.keys(res?.mandatory || {}).length;
- // const addCount = Object.keys(res?.additional || {}).length;
- // const ocrExtractedCount = Object.keys(res?.ocr_results?.extracted_attributes || {}).length;
- // const visualExtractedCount = Object.keys(res?.visual_results?.visual_attributes || {}).length;
-
- // const counts = el('div', 'attribute-summary-pills');
- // const c1 = el('span', 'pill primary'); c1.textContent = `Mandatory: ${mandCount}`;
- // const c2 = el('span', 'pill secondary'); c2.textContent = `Additional: ${addCount}`;
- // const c3 = el('span', 'pill secondary'); c3.textContent = `OCR Keys: ${ocrExtractedCount}`;
- // const c4 = el('span', 'pill secondary'); c4.textContent = `Visual Keys: ${visualExtractedCount}`;
- // counts.appendChild(c1); counts.appendChild(c2); counts.appendChild(c3); counts.appendChild(c4);
- // content.appendChild(counts);
-
- // // Final assembly and insertion
- // td.appendChild(content);
- // detail.appendChild(td);
- // baseRow.insertAdjacentElement('afterend', detail);
- // });
- // // Update summary statistics
- // $('#statTotal').textContent = api.total_products ?? 0;
- // $('#statOk').textContent = api.successful ?? 0;
- // $('#statKo').textContent = api.failed ?? 0;
- // const apiSummary = $('#api-summary');
- // if (apiSummary) apiSummary.style.display = 'block';
- // }
- function renderInlineForTable() {
- const api = API_RESPONSE_AI;
- const table = $('#tableContainer');
- if (!table) return;
- // Remove existing detail rows
- table.querySelectorAll('tr.detail-row').forEach(r => r.remove());
- PRODUCT_BASE.forEach((p, idx) => {
- if (!isProductSelected(p.item_id)) return;
-
- const res = findApiResultForProduct(p, idx, api);
-
- const tbody = table.querySelector('tbody');
- const baseRow = tbody ? tbody.querySelector(`#row-${p.id}`) : null;
- if (!baseRow) return;
- // --- Detail Row Construction ---
- const detail = el('tr', 'detail-row');
- // td.colSpan must match the number of columns in your main table
- const td = el('td'); td.colSpan = 6;
- const content = el('div', 'detail-content-tables');
- // 1. MANDATORY Attributes Table
- // 🚨 Note: The color check (Requirement #2) must be implemented inside renderMandatoryComparisonTable.
- const mandatoryData = res?.mandatory || {};
- if (Object.keys(mandatoryData).length > 0) {
- const mandatoryTable = renderMandatoryComparisonTable(
- mandatoryData,
- 'Mandatory Attributes Comparison'
- );
- content.appendChild(mandatoryTable);
- }
- // 2. COMBINED Attributes Section (Additional, OCR, Visuals)
- const additionalData = res?.additional || {};
- const ocrData = res?.ocr_results?.extracted_attributes || {};
- const visualsData = res?.visual_results?.visual_attributes || {};
-
- const hasCombinedData = Object.keys(additionalData).length > 0 ||
- Object.keys(ocrData).length > 0 ||
- Object.keys(visualsData).length > 0;
- if (hasCombinedData) {
- content.appendChild(el('hr', 'section-separator'));
-
- const combinedTitle = el('div', 'section-title');
- combinedTitle.innerHTML = '<h2>Additional & AI-Driven Attributes</h2>';
- content.appendChild(combinedTitle);
-
- const combinedAttributesContainer = el('div', 'combined-attributes-container');
-
- // Render Additional Table (Conditional based on data existence)
- if (Object.keys(additionalData).length > 0) {
- const additionalTable = renderAttributesAsTable(
- additionalData,
- 'Additional Attributes',
- mandatoryData
- );
- combinedAttributesContainer.appendChild(additionalTable);
- }
-
- // Render OCR Table (Conditional based on data existence)
- if (Object.keys(ocrData).length > 0) {
- const ocrTable = renderAttributesAsTable(
- ocrData,
- 'OCR Results',
- mandatoryData
- );
- combinedAttributesContainer.appendChild(ocrTable);
- }
-
- // Render Visuals Table (Conditional based on data existence)
- if (Object.keys(visualsData).length > 0) {
- const visualsTable = renderAttributesAsTable(
- visualsData,
- 'Visual Results',
- mandatoryData
- );
- combinedAttributesContainer.appendChild(visualsTable);
- }
- content.appendChild(combinedAttributesContainer);
- }
-
- // --- Summary Counts ---
- const mandCount = Object.keys(mandatoryData).length;
- const addCount = Object.keys(additionalData).length;
- const ocrExtractedCount = Object.keys(ocrData).length;
- const visualExtractedCount = Object.keys(visualsData).length;
-
- const counts = el('div', 'attribute-summary-pills');
- const c1 = el('span', 'pill primary'); c1.textContent = `Mandatory: ${mandCount}`;
- const c2 = el('span', 'pill secondary'); c2.textContent = `Additional: ${addCount}`;
- const c3 = el('span', 'pill secondary'); c3.textContent = `OCR Keys: ${ocrExtractedCount}`;
- const c4 = el('span', 'pill secondary'); c4.textContent = `Visual Keys: ${visualExtractedCount}`;
- counts.appendChild(c1); counts.appendChild(c2); counts.appendChild(c3); counts.appendChild(c4);
- content.appendChild(counts);
-
- // Final assembly and insertion
- td.appendChild(content);
- detail.appendChild(td);
- baseRow.insertAdjacentElement('afterend', detail);
- });
- // Update summary statistics
- $('#statTotal').textContent = api.total_products ?? 0;
- $('#statOk').textContent = api.successful ?? 0;
- $('#statKo').textContent = api.failed ?? 0;
- const apiSummary = $('#api-summary');
- if (apiSummary) apiSummary.style.display = 'block';
- }
- function renderInlineAttributes() {
- if (layoutMode === 'cards') renderInlineForCards(); else renderInlineForTable();
- // renderInlineForCards();
- // renderInlineForTable();
- }
- // --- Main rendering ---
- function renderProducts() {
- if (layoutMode === 'cards') {
- $('#cardsContainer').style.display = '';
- $('#tableContainer').style.display = 'none';
- // console.log("PRODUCT_BASE",PRODUCT_BASE);
- renderProductsCards();
- } else {
- $('#cardsContainer').style.display = 'none';
- $('#tableContainer').style.display = '';
- renderProductsTable();
- }
- updateSelectionInfo();
- renderPagination();
- // If there is a selection, re-render inline attributes (persist across toggle)
- if (selectedProductsWithAttributes.length > 0) renderInlineAttributes();
- }
- // --- Submit & Reset ---
- function submitAttributes() {
- // Check the length of the new array
- if (selectedProductsWithAttributes.length === 0) {
- alert('Please select at least one product.');
- return;
- }
- // if (selectedIds.size === 0) { alert('Please select at least one product.'); return; }
- // console.log("selectedIds",selectedIds);
- jQuery('#full-page-loader').show();
- // let inputArray = {
- // "product_ids" : [...selectedIds]
- // }
- const extractAdditional = document.getElementById('extract_additional').checked;
- const processImage = document.getElementById('process_image').checked;
- // const selectedMultiples = document.getElementById('#mandatory-attributes');
- // const selectedValues = Array.from(selectedMultiples.selectedOptions).map(option => option.value);
- const selectElement = document.getElementById('mandatory-attributes');
- const selectedValues = Array.from(selectElement.selectedOptions).map(option => option.value);
- // console.log(selectedValues); // Logs an array of selected values
- // console.log("thresholdValueDisplay",thresholdValueDisplay.value);
- const threshold = parseFloat(document.getElementById('thresholdRange').value);
- // Transform the new state array into the required API format
- const itemIds = selectedProductsWithAttributes.map(p => p.item_id);
-
- // Create the mandatory_attrs map: { item_id: { attr_name: [values] } }
- // NOTE: The backend API you showed expects a flattened list of "mandatory_attrs"
- // like: { "color": ["color", "shade"], "size": ["size", "fit"] }
- // It seems to ignore the selected product-specific values and uses a general list of synonyms.
- // Assuming the request needs a general map of *all unique* selected attributes across all selected products:
-
- let mandatoryAttrsMap = {};
- selectedProductsWithAttributes.forEach(product => {
- // Merge attributes from all selected products
- Object.assign(mandatoryAttrsMap, product.mandatory_attrs);
- });
- // If the API expects the complex, product-specific payload from your Q1 example:
- const payloadForQ1 = selectedProductsWithAttributes.map(p => ({
- item_id: p.item_id,
- mandatory_attrs: p.mandatory_attrs
- }));
- let inputArray = {
- "products": payloadForQ1,
- "model": "llama-3.1-8b-instant",
- "extract_additional": extractAdditional,
- "process_image": processImage,
- "multiple": selectedValues,
- "threshold_abs": 0.6, // Lower threshold to be more permissive
- // "margin": 0.3, // Larger margin to include more candidates
- // "use_adaptive_margin": true,
- // "use_semantic_clustering": true
- }
- let raw = JSON.stringify(inputArray);
- fetch('/attr/batch-extract/', {
- method: 'POST', // or 'POST' if your API expects POST
- headers: {
- 'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]')?.value || '',
- 'Content-Type': "application/json"
- },
- body: raw
- })
- .then(response => response.json())
- .then(data => {
- // console.log("response data",data);
- API_RESPONSE_AI = data;
- renderInlineAttributes();
- jQuery('#full-page-loader').hide();
- });
- }
- function resetAll() {
- selectedProductsWithAttributes = []; // Reset the main array
- // selectedIds.clear();
- lastSeen.clear();
- renderProducts();
- // Clear summary
- document.getElementById('statTotal').textContent = '0';
- document.getElementById('statOk').textContent = '0';
- document.getElementById('statKo').textContent = '0';
- $('#api-summary').style.display = 'none';
- // ✅ Clear Select2 selections
- jQuery('#mandatory-attributes').val(null).trigger('change');
- // ✅ Reset threshold input (and display)
- const thresholdInput = document.getElementById('thresholdRange');
- const thresholdDisplay = document.getElementById('thresholdValue');
- thresholdInput.value = '0.65'; // or any default value you prefer
- if (thresholdDisplay) {
- thresholdDisplay.textContent = '0.65';
- }
- }
- function setLayout(mode) {
- layoutMode = mode;
- const btnCards = document.getElementById('btnCards');
- const btnTable = document.getElementById('btnTable');
- if (mode === 'cards') { btnCards.classList.add('active'); btnCards.setAttribute('aria-selected', 'true'); btnTable.classList.remove('active'); btnTable.setAttribute('aria-selected', 'false'); }
- else { btnTable.classList.add('active'); btnTable.setAttribute('aria-selected', 'true'); btnCards.classList.remove('active'); btnCards.setAttribute('aria-selected', 'false'); }
- renderProducts();
- }
- // Upload elements (Bootstrap modal version)
- const uploadModalEl = document.getElementById('uploadModal');
- const dropzone = document.getElementById('dropzone');
- const uploadFiles = document.getElementById('uploadFiles');
- const fileInfo = document.getElementById('fileInfo');
- const uploadBar = document.getElementById('uploadBar');
- const uploadStatus = document.getElementById('uploadStatus');
- // Reset modal on show
- uploadModalEl.addEventListener('shown.bs.modal', () => {
- uploadStatus.textContent = '';
- uploadStatus.className = ''; // clear success/error class
- uploadBar.style.width = '0%';
- uploadBar.setAttribute('aria-valuenow', '0');
- uploadFiles.value = '';
- uploadFiles.setAttribute('accept', ACCEPT_TYPES);
- fileInfo.textContent = 'No files selected.';
- });
- function describeFiles(list) {
- if (!list || list.length === 0) { fileInfo.textContent = 'No files selected.'; return; }
- const names = Array.from(list).map(f => `${f.name} (${Math.round(f.size/1024)} KB)`);
- fileInfo.textContent = names.join(', ');
- }
- // Drag & drop feedback
- ['dragenter','dragover'].forEach(evt => {
- dropzone.addEventListener(evt, e => { e.preventDefault(); e.stopPropagation(); dropzone.classList.add('drag'); });
- });
- ['dragleave','drop'].forEach(evt => {
- dropzone.addEventListener(evt, e => { e.preventDefault(); e.stopPropagation(); dropzone.classList.remove('drag'); });
- });
- // Handle drop
- dropzone.addEventListener('drop', e => {
- uploadFiles.files = e.dataTransfer.files;
- describeFiles(uploadFiles.files);
- });
- // Click to browse
- // dropzone.addEventListener('click', () => uploadFiles.click());
- // Picker change
- uploadFiles.addEventListener('change', () => describeFiles(uploadFiles.files));
- function startUpload() {
- const files = uploadFiles.files;
- if (!files || files.length === 0) { alert('Please select file(s) to upload.'); return; }
- jQuery('#full-page-loader').show();
- uploadStatus.textContent = 'Uploading...';
- uploadStatus.className = ''; // neutral
- uploadBar.style.width = '0%';
- uploadBar.setAttribute('aria-valuenow', '0');
- const form = new FormData();
- Array.from(files).forEach(f => form.append('file', f));
- // form.append('uploaded_by', 'Vishal'); // example extra field
- const xhr = new XMLHttpRequest();
- xhr.open('POST', UPLOAD_API_URL, true);
- // If you need auth:
- // xhr.setRequestHeader('Authorization', 'Bearer <token>');
- xhr.upload.onprogress = (e) => {
- if (e.lengthComputable) {
- const pct = Math.round((e.loaded / e.total) * 100);
- uploadBar.style.width = pct + '%';
- uploadBar.setAttribute('aria-valuenow', String(pct));
- }
- };
- xhr.onreadystatechange = () => {
- if (xhr.readyState === 4) {
- const ok = (xhr.status >= 200 && xhr.status < 300);
- try {
- const resp = JSON.parse(xhr.responseText || '{}');
- uploadStatus.textContent = ok ? (resp.message || 'Upload successful') : (resp.error || `Upload failed (${xhr.status})`);
- } catch {
- uploadStatus.textContent = ok ? 'Upload successful' : `Upload failed (${xhr.status})`;
- }
- uploadStatus.className = ok ? 'success' : 'error';
- // Optional: auto-close the modal on success after 1.2s:
- // if (ok) setTimeout(() => bootstrap.Modal.getInstance(uploadModalEl).hide(), 1200);
- }
- };
- xhr.onerror = () => {
- uploadStatus.textContent = 'Network error during upload.';
- uploadStatus.className = 'error';
- };
- xhr.send(form);
- setTimeout(()=>{
- jQuery('#uploadModal').modal('hide');
- window.location.reload();
- },3000)
- jQuery('#full-page-loader').hide();
- }
- // Wire Start button
- document.getElementById('uploadStart').addEventListener('click', startUpload);
- // Cancel button already closes the modal via data-bs-dismiss
- // --- Pagination state ---
- let page = 1;
- let pageSize = 50; // default rows per page
- function totalPages() {
- return Math.max(1, Math.ceil(PRODUCT_BASE.length / pageSize));
- }
- function clampPage() {
- page = Math.min(Math.max(1, page), totalPages());
- }
- function getCurrentSlice() {
- clampPage();
- const start = (page - 1) * pageSize;
- return PRODUCT_BASE.slice(start, start + pageSize);
- }
- function renderPagination() {
- const bar = document.getElementById('paginationBar');
- if (!bar) return;
- const tp = totalPages();
- clampPage();
- bar.innerHTML = `
- <div class="page-size">
- <label for="pageSizeSelect">Rows per page</label>
- <select id="pageSizeSelect">
- <option value="5" ${pageSize===5 ? 'selected' : ''}>5</option>
- <option value="10" ${pageSize===10 ? 'selected' : ''}>10</option>
- <option value="20" ${pageSize===20 ? 'selected' : ''}>20</option>
- <option value="50" ${pageSize===50 ? 'selected' : ''}>50</option>
- <option value="all" ${pageSize>=PRODUCT_BASE.length ? 'selected' : ''}>All</option>
- </select>
- </div>
- <div class="pager">
- <button class="pager-btn" id="prevPage" ${page<=1 ? 'disabled' : ''} aria-label="Previous page">‹</button>
- <span class="page-info">Page ${page} of ${tp}</span>
- <button class="pager-btn" id="nextPage" ${page>=tp ? 'disabled' : ''} aria-label="Next page">›</button>
- </div>
- `;
- // wire events
- document.getElementById('prevPage')?.addEventListener('click', () => { if (page > 1) { page--; renderProducts(); } });
- document.getElementById('nextPage')?.addEventListener('click', () => { if (page < tp) { page++; renderProducts(); } });
- const sel = document.getElementById('pageSizeSelect');
- if (sel) {
- sel.addEventListener('change', () => {
- const val = sel.value;
- pageSize = (val === 'all') ? PRODUCT_BASE.length : parseInt(val, 10);
- page = 1; // reset to first page when size changes
- renderProducts();
- });
- }
- }
- // Function to add/remove product from the state and manage its attributes
- function toggleProductSelection(itemId, isChecked, attributes = {}) {
- const index = selectedProductsWithAttributes.findIndex(p => p.item_id === itemId);
- // console.log("index",index);
- if (isChecked) {
- // If selecting, ensure the product object exists in the array
- if (index === -1) {
- selectedProductsWithAttributes.push({
- item_id: itemId,
- mandatory_attrs: attributes
- });
- } else {
- // Update attributes if the product is already selected
- selectedProductsWithAttributes[index].mandatory_attrs = attributes;
- }
- } else {
- // If deselecting, remove the product object from the array
- if (index !== -1) {
- selectedProductsWithAttributes.splice(index, 1);
- }
- }
- updateSelectionInfo();
- }
- // Function to get the current mandatory attributes for a selected item
- function getSelectedAttributes(itemId) {
- const productEntry = selectedProductsWithAttributes.find(p => p.item_id === itemId);
- return productEntry ? productEntry.mandatory_attrs : {};
- }
- // Helper to check if a product is selected
- function isProductSelected(itemId) {
- return selectedProductsWithAttributes.some(p => p.item_id === itemId);
- }
- // Helper to check if a specific attribute/value is selected
- function isAttributeValueSelected(itemId, attrName, value) {
- const attrs = getSelectedAttributes(itemId);
- const values = attrs[attrName];
- return values ? values.includes(value) : false; // Default all selected when first loaded
- }
- // $('.attribute-select').select2({
- // placeholder: 'Select product attributes'
- // });
- function getAtributeList(){
- jQuery('#full-page-loader').show();
- try{
- fetch('/attr/products/attributes', {
- method: 'GET', // or 'POST' if your API expects POST
- headers: {
- 'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]')?.value || ''
- }
- })
- .then(response => response.json())
- .then(data => {
- // console.log("data",data);
- attributesFullData = data;
- let attributesData = data;
- // Step 1: Extract unique mandatory attribute names
- const mandatoryAttributes = [...new Set(
- attributesData
- .filter(attr => attr.is_mandatory === "Yes")
- .map(attr => attr.attribute_name)
- )];
- // Step 2: Populate the select element
- const $select = jQuery('#mandatory-attributes');
- $select.append(new Option("Select All", "select_all")); // Add "Select All" option first
- mandatoryAttributes.forEach(attr => {
- $select.append(new Option(attr, attr));
- });
- // Step 3: Initialize Select2 with placeholder
- // $select.select2({
- // placeholder: "Select mandatory attributes",
- // allowClear: true
- // });
- // Step 4: Handle 'Select All' logic
- $select.on('select2:select', function (e) {
- if (e.params.data.id === "select_all") {
- // Select all real options except "Select All"
- const allOptions = mandatoryAttributes;
- $select.val(allOptions).trigger('change');
- }
- });
- jQuery('#full-page-loader').hide();
- });
- }catch(err){
- console.log("err",err);
- jQuery('#full-page-loader').hide();
- }
-
- }
- document.addEventListener("DOMContentLoaded", function () {
- // Update span when range changes
- thresholdInput.addEventListener('input', function () {
- // console.log("this.value",this.value);
- thresholdValueDisplay.textContent = this.value;
- });
- });
- // Get threshold value when needed
- function getThreshold() {
- // console.log("parseFloat(thresholdInput.value)",parseFloat(thresholdInput.value));
- return parseFloat(thresholdInput.value);
- }
- /**
- * Renders Mandatory attributes using a card-based comparison layout.
- * Highlights mismatches prominently.
- * @param {Object} attributes - The mandatory attribute data.
- * @param {string} title - The title for the section.
- * @returns {HTMLElement} A div containing the comparison cards.
- */
- function renderMandatoryComparisonCards(attributes, title) {
- const section = el('div', 'attribute-section');
-
- // --- 1. Flatten Mandatory Attributes ---
- let attributeEntries = [];
- Object.keys(attributes).forEach(key => {
- const valuesArray = Array.isArray(attributes[key]) ? attributes[key] : [attributes[key]];
- valuesArray.forEach(v => {
- const aiValue = v.value || 'N/A';
- const originalValue = v.original_value || 'N/A';
-
- // Comparison is case-insensitive and ignores leading/trailing whitespace
- const isMatch = (String(aiValue).trim().toLowerCase() === String(originalValue).trim().toLowerCase());
- attributeEntries.push({
- name: key,
- aiValue: aiValue,
- originalValue: originalValue,
- isMatch: isMatch,
- source: v.source || 'N/A'
- });
- });
- });
- // --- 2. Section Header ---
- const titleEl = el('div', 'section-title');
- titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- section.appendChild(titleEl);
-
- if (attributeEntries.length === 0) {
- const msg = el('p', 'no-attributes-message');
- msg.textContent = `No ${title.toLowerCase()} found.`;
- section.appendChild(msg);
- return section;
- }
- // --- 3. Card Container ---
- const cardsContainer = el('div', 'comparison-cards-container');
- attributeEntries.forEach(attr => {
- // Main Card Element
- const card = el('div', `comparison-card ${attr.isMatch ? 'match' : 'mismatch-card'}`);
- // Card Header (Attribute Name)
- const header = el('div', 'card-header');
- header.textContent = attr.name.replace(/_/g, ' ');
- card.appendChild(header);
- // Content Wrapper
- const content = el('div', 'card-content');
- // Existing Value Box
- const originalBox = el('div', 'value-box original-box');
- originalBox.innerHTML = `
- <div class="value-label">Manually Identified Value</div>
- <div class="value-text">${attr.originalValue}</div>
- `;
- content.appendChild(originalBox);
- // AI Value Box
- const aiBox = el('div', `value-box ai-box ${attr.isMatch ? 'found-value' : 'mismatch-value'}`);
- aiBox.innerHTML = `
- <div class="value-label">AI Generated Value <span class="value-source">(${attr.source})</span></div>
- <div class="value-text">${attr.aiValue}</div>
- `;
- content.appendChild(aiBox);
- card.appendChild(content);
- // Mismatch Indicator (only visible on mismatch-card via CSS)
- if (!attr.isMatch) {
- const indicator = el('div', 'mismatch-indicator');
- // indicator.innerHTML = '❌ MISMATCH';
- indicator.innerHTML = attr.isMatch ? '✅ MATCH' : '❌ MISMATCH';
- card.appendChild(indicator);
- }
- cardsContainer.appendChild(card);
- });
- section.appendChild(cardsContainer);
- return section;
- }
- // Example JavaScript (Assuming you have access to API_RESPONSE_AI)
- // document.getElementById('downloadResultBtn').addEventListener('click', () => {
- // // 1. Convert the data to a JSON string
- // const jsonString = JSON.stringify(API_RESPONSE_AI, null, 2);
-
- // // 2. Create a Blob from the JSON string
- // const blob = new Blob([jsonString], { type: 'application/json' });
-
- // // 3. Create a temporary URL and link element
- // const url = URL.createObjectURL(blob);
- // const a = document.createElement('a');
-
- // // 4. Set download attributes
- // a.href = url;
- // a.download = 'api_generated_results.json';
-
- // // 5. Simulate a click to trigger download
- // document.body.appendChild(a);
- // a.click();
-
- // // 6. Clean up
- // document.body.removeChild(a);
- // URL.revokeObjectURL(url);
- // });
- /**
- * Renders Mandatory attributes using a card-based comparison layout.
- * Highlights mismatches prominently.
- * @param {Object} attributes - The mandatory attribute data.
- * @param {string} title - The title for the section (used for the header).
- * @returns {HTMLElement} A div containing the comparison cards.
- */
- // function renderMandatoryComparisonCards(attributes, title) {
- // const section = el('div', 'attribute-section mandatory-comparison-section');
-
- // // --- 1. Flatten Mandatory Attributes ---
- // let attributeEntries = [];
- // Object.keys(attributes).forEach(key => {
- // const valuesArray = Array.isArray(attributes[key]) ? attributes[key] : [attributes[key]];
- // valuesArray.forEach(v => {
- // const aiValue = v.value || 'N/A';
- // const originalValue = v.original_value || 'N/A';
-
- // const isMatch = (String(aiValue).trim().toLowerCase() === String(originalValue).trim().toLowerCase());
- // attributeEntries.push({
- // name: key,
- // aiValue: aiValue,
- // originalValue: originalValue,
- // isMatch: isMatch,
- // source: v.source || 'N/A'
- // });
- // });
- // });
- // // --- 2. Section Header ---
- // const titleEl = el('div', 'section-title');
- // titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- // section.appendChild(titleEl);
-
- // if (attributeEntries.length === 0) {
- // const msg = el('p', 'no-attributes-message');
- // msg.textContent = `No ${title.toLowerCase()} found.`;
- // section.appendChild(msg);
- // return section;
- // }
- // // --- 3. Card Container ---
- // const cardsContainer = el('div', 'comparison-cards-container');
- // attributeEntries.forEach(attr => {
- // const card = el('div', `comparison-card ${attr.isMatch ? 'match' : 'mismatch-card'}`);
- // const header = el('div', 'card-header');
- // header.textContent = attr.name.replace(/_/g, ' ');
- // card.appendChild(header);
- // const content = el('div', 'card-content');
- // // Existing Value Box
- // const originalBox = el('div', 'value-box original-box');
- // originalBox.innerHTML = `
- // <div class="value-label">Manually Identified Value</div>
- // <div class="value-text">${attr.originalValue}</div>
- // `;
- // content.appendChild(originalBox);
- // // AI Value Box
- // const aiBox = el('div', `value-box ai-box ${attr.isMatch ? 'found-value' : 'mismatch-value'}`);
- // aiBox.innerHTML = `
- // <div class="value-label">AI Generated Value <span class="value-source">(${attr.source})</span></div>
- // <div class="value-text">${attr.aiValue}</div>
- // `;
- // content.appendChild(aiBox);
- // card.appendChild(content);
- // // Mismatch Indicator
- // if (!attr.isMatch) {
- // const indicator = el('div', 'mismatch-indicator');
- // // indicator.innerHTML = '❌ MISMATCH';
- // indicator.innerHTML = attr.isMatch ? '✅ MATCH' : '❌ MISMATCH';
- // card.appendChild(indicator);
- // }
- // cardsContainer.appendChild(card);
- // });
- // section.appendChild(cardsContainer);
- // return section;
- // }
- function renderMandatoryComparisonCards(attributes, title) {
- const section = el('div', 'attribute-section mandatory-comparison-section');
-
- // --- 1. Flatten Mandatory Attributes ---
- let attributeEntries = [];
- Object.keys(attributes).forEach(key => {
- const valuesArray = Array.isArray(attributes[key]) ? attributes[key] : [attributes[key]];
- valuesArray.forEach(v => {
- const aiValue = v.value || 'N/A';
- const originalValue = v.original_value || 'N/A';
-
- const isMatch = (String(aiValue).trim().toLowerCase() === String(originalValue).trim().toLowerCase());
- attributeEntries.push({
- name: key,
- aiValue: aiValue,
- originalValue: originalValue,
- isMatch: isMatch,
- source: v.source || 'N/A'
- });
- });
- });
- // --- 2. Section Header ---
- const titleEl = el('div', 'section-title');
- titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- section.appendChild(titleEl);
-
- if (attributeEntries.length === 0) {
- const msg = el('p', 'no-attributes-message');
- msg.textContent = `No ${title.toLowerCase()} found.`;
- section.appendChild(msg);
- return section;
- }
- // --- 3. Card Container ---
- const cardsContainer = el('div', 'comparison-cards-container');
- attributeEntries.forEach(attr => {
- // --- CHANGE 1: Apply 'match-card' or 'mismatch-card' explicitly ---
- const cardClass = attr.isMatch ? 'match-card' : 'mismatch-card';
- const card = el('div', `comparison-card ${cardClass}`);
- const header = el('div', 'card-header');
- header.textContent = attr.name.replace(/_/g, ' ');
- card.appendChild(header);
- const content = el('div', 'card-content');
- // Existing Value Box
- const originalBox = el('div', 'value-box original-box');
- originalBox.innerHTML = `
- <div class="value-label">Manually Identified Value</div>
- <div class="value-text">${attr.originalValue}</div>
- `;
- content.appendChild(originalBox);
- // AI Value Box
- // Removed 'found-value' class here as styling should rely on the parent card class
- const aiBox = el('div', `value-box ai-box ${attr.isMatch ? '' : 'mismatch-value'}`);
- aiBox.innerHTML = `
- <div class="value-label">AI Generated Value <span class="value-source">(${attr.source})</span></div>
- <div class="value-text">${attr.aiValue}</div>
- `;
- content.appendChild(aiBox);
- card.appendChild(content);
- // --- CHANGE 2: Display the indicator for ALL cards, controlling color via CSS ---
- const indicator = el('div', 'match-status-indicator');
- indicator.innerHTML = attr.isMatch ? '✅ MATCH' : '❌ MISMATCH';
- card.appendChild(indicator);
- cardsContainer.appendChild(card);
- });
- section.appendChild(cardsContainer);
- return section;
- }
- /**
- * Renders Additional, OCR, or Visual attributes in a simple card layout.
- * @param {Object} attributes - The attribute data (Additional, OCR, or Visual).
- * @param {string} title - The title for the section.
- * @returns {HTMLElement} A div containing the attribute cards.
- */
- function renderSimpleAttributeCards(attributes, title) {
- const section = el('div', 'attribute-section simple-attribute-section');
-
- // --- 1. Flatten Attributes ---
- let attributeEntries = [];
- const processAttribute = (key, values) => {
- const valuesArray = Array.isArray(values) ? values : [values];
- valuesArray.forEach(v => {
- attributeEntries.push({
- name: key,
- value: v.value,
- source: v.source || 'N/A'
- });
- });
- };
- Object.keys(attributes).forEach(key => {
- const attribute = attributes[key];
-
- if (Array.isArray(attribute)) {
- processAttribute(key, attribute);
- } else if (typeof attribute === 'object' && attribute !== null) {
- Object.keys(attribute).forEach(subKey => {
- const subAttribute = attribute[subKey];
- if (Array.isArray(subAttribute)) {
- processAttribute(`${key} (${subKey.replace(/_/g, ' ')})`, subAttribute);
- }
- });
- }
- });
- // --- 2. Section Header ---
- const titleEl = el('div', 'section-title');
- titleEl.innerHTML = `<h3>${title} (${attributeEntries.length})</h3>`;
- section.appendChild(titleEl);
-
- if (attributeEntries.length === 0) {
- const msg = el('p', 'no-attributes-message');
- msg.textContent = `No ${title.toLowerCase()} found.`;
- section.appendChild(msg);
- return section;
- }
- // --- 3. Card Container ---
- const cardsContainer = el('div', 'comparison-cards-container simple-cards-container');
- attributeEntries.forEach(attr => {
- // Simple Card Element
- const card = el('div', 'simple-card');
- // Card Header (Attribute Name)
- const header = el('div', 'card-header');
- header.textContent = formatString(attr.name).replace(/_/g, ' ');
- card.appendChild(header);
- // Content Wrapper
- const content = el('div', 'card-content');
-
- // Value Box
- const valueBox = el('div', 'value-box single-value-box');
- const displayValue = Array.isArray(attr.value)
- ? (attr.value.map(v => formatString(v.value))).join(', ') +'('+ formatString(attr.source)+ ')'
- : formatString(attr.value) +'('+ formatString(attr.source)+ ')';
- // : attr.value;
-
- valueBox.innerHTML = `
- <div class="value-label">Extracted Value <span class="value-source">(${formatString(attr.source)})</span></div>
- <div class="value-text">${displayValue || 'N/A'}</div>
- `;
- content.appendChild(valueBox);
- card.appendChild(content);
- cardsContainer.appendChild(card);
- });
- section.appendChild(cardsContainer);
- return section;
- }
|