| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120 |
- 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', '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);
- const tdDesc = document.createElement('td');
- // Create a container for descriptions
- const descContainer = document.createElement('div');
- descContainer.classList.add('description-cell');
- // Short description (always visible)
- const shortDesc = document.createElement('div');
- shortDesc.classList.add('short-desc');
- shortDesc.innerHTML = `<strong>Short Description:</strong> ${p.product_short_description || 'N/A'}`;
- // Long description (hidden initially)
- const longDesc = document.createElement('div');
- longDesc.classList.add('long-desc');
- longDesc.innerHTML = `<strong>Long Description:</strong> ${p.product_long_description || 'N/A'}`;
- longDesc.style.display = 'none';
- // Button to toggle long description
- const toggleBtn = document.createElement('button');
- toggleBtn.textContent = 'Show Long Description';
- toggleBtn.classList.add('btn');
- toggleBtn.classList.add('btn-primary');
- toggleBtn.classList.add('btn-sm');
- toggleBtn.classList.add('btn-toggle-desc');
- toggleBtn.style.fontSize = 'smaller';
- toggleBtn.style.margin = '5px';
- // Toggle logic
- toggleBtn.addEventListener('click', () => {
- const isVisible = longDesc.style.display === 'block';
- longDesc.style.display = isVisible ? 'none' : 'block';
- toggleBtn.textContent = isVisible ? 'Show Long Description' : 'Hide Long Description';
- });
- // Append elements in order
- descContainer.appendChild(shortDesc);
- descContainer.appendChild(toggleBtn);
- descContainer.appendChild(longDesc);
- tdDesc.appendChild(descContainer);
- 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;
- }
|