Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

productdetails.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. function startDetails(){
  2. // http://127.0.0.1:5502/productdetails.html?productId=106633230000024
  3. if(window.location.search.split('=')[0] == ''){
  4. window.location.href = "/notfound.html";
  5. return;
  6. }
  7. let productId = window.location.search.split('=')[1].split('&')[0];
  8. console.log(productId);
  9. let itemtagscombinationRes = null;
  10. let labels = {};
  11. let isThereInLabel = {};
  12. let currentComb = null;
  13. let group = {};
  14. let currentClick = null;
  15. debugger;
  16. if(productId){
  17. updateProductId(productId);
  18. getProductDetails(productId);
  19. appendSpecs(productId)
  20. }else{
  21. window.location.href = "/notfound.html"
  22. }
  23. function updateProductId(val){
  24. $('#productidtag').val(val);
  25. }
  26. function updateItemId(val){
  27. $('#itemidtag').val(val);
  28. }
  29. let specification_wrap_action = {
  30. remove(){
  31. debugger;
  32. $('.specification-wrap').addClass('d-none');
  33. },
  34. add(){
  35. $('.specification-wrap').removeClass('d-none');
  36. }
  37. }
  38. let description_action = {
  39. addVal(value){
  40. $('.description-wrap > p').html(value);
  41. },
  42. removeVal(value){
  43. $('.description-wrap > p').html('');
  44. }
  45. }
  46. function getSpecsItemHTML({name,des,itemid,id}){
  47. return `<div id="${id}" data-specitemid="${itemid}" class="">
  48. <div class="row border-bottom">
  49. <div class="col-sm-6 bg-gray-3 py-2">
  50. ${name}
  51. </div>
  52. <div class="col-sm-6 py-2">
  53. ${des}
  54. </div>
  55. </div>
  56. </div>`;
  57. return ` <tr id="${id}" data-specitemid="${itemid}">
  58. <td style="width:200px">${name}</td>
  59. <td class="">${des}</td>
  60. </tr>`;
  61. }
  62. // function getSpecsItemHTML({name,des,itemid,id}){
  63. // return ` <tr id="${id}" data-specitemid="${itemid}">
  64. // <td style="width:200px">${name}</td>
  65. // <td class="">${des}</td>
  66. // </tr>`;
  67. // }
  68. async function appendSpecs(id){
  69. let resData = await API_SERVICES_ACTIONS.getAPIService(`apis/v4/bizgaze/integrations/products/itemspecifications/itemid/${id}`,true);
  70. if(resData.isError){
  71. $('.productdetailstabs').addClass('d-none');
  72. $('.specContainerleft').html('')
  73. $('.specContainerRight').html('')
  74. return;
  75. }
  76. $('.productdetailstabs ').removeClass('d-none');
  77. const res = resData.response;
  78. console.log(JSON.parse(res.result));
  79. const data = JSON.parse(res.result);
  80. console.log(data);
  81. let html = '';
  82. let leftHtml = '';
  83. let rightHtml = '';
  84. if(data.length == 0){
  85. $('.productdetailstabs ').addClass('d-none');
  86. $('.specContainerleft').html('')
  87. $('.specContainerRight').html('')
  88. return;
  89. }
  90. for(let i=0;i<data.length;i++){
  91. const {specificationname,specificationitemid,itemid,description} = data[i];
  92. if((i+1)%2 == 0){
  93. rightHtml += getSpecsItemHTML({
  94. name:specificationname,id:specificationitemid,itemid,des:description
  95. })
  96. }else{
  97. leftHtml += getSpecsItemHTML({
  98. name:specificationname,id:specificationitemid,itemid,des:description
  99. });
  100. }
  101. }
  102. debugger;
  103. $('.specContainerleft').html(leftHtml)
  104. $('.specContainerRight').html(rightHtml)
  105. }
  106. function getParentHTML(name,productid,id,isMain){
  107. return `<div class="pb-2 $${name?.replace(" ",'')}-parent">
  108. <h5 class="py-2 smallHeadingProductDetails">${name}</h5>
  109. <div class="d-flex gap-2 ${id}container" data-tagid="${id}">
  110. </div>
  111. </div>`;
  112. }
  113. function onClickHandler(e) {
  114. $(e).parent().find('.borderselector').removeClass('borderselector');
  115. $(e).addClass('borderselector')
  116. }
  117. function getChildHTML(tagid,name,isTrue){
  118. return `
  119. <div data-type="${tagid}-${name}" data-tagiditem="${tagid}" class="specsmallcard optiontaganwi ${isTrue?'borderselector':''} card small-font p-3">
  120. ${name}
  121. </div>
  122. `
  123. }
  124. function addeventlisteners(){
  125. $('.optiontaganwi').each(function (i,el){
  126. $(el).click(function (e){
  127. $(e.target).parent().find('.borderselector').removeClass('borderselector');
  128. $(e.target).addClass('borderselector')
  129. searchComb();
  130. currentClick = e.target;
  131. // getAllCombination();
  132. })
  133. });
  134. }
  135. function updateDetailsByObj(obj){
  136. $('.productname').html(obj.itemname)
  137. $('.price').html(obj.pricelist);
  138. appendSpecs(obj.itemid)
  139. $('#skudetailitem').val(obj.sku)
  140. $('.des_productdes').html(obj.itemdescription);
  141. $('#addtocart').attr('data-qty',obj.quantity);
  142. let defaultImg = obj.itemimageurl ? imgServerNameBuild(obj.itemimageurl): `./dist/assets/imgs/nophoto.png`;
  143. console.log(obj.itemimageurl,'obj.itemimageurlobj.itemimageurlobj.itemimageurl');
  144. debugger;
  145. $('.productDetailsMain > img').attr('src',defaultImg);
  146. updateItemId(obj.itemid);
  147. }
  148. function searchComb(){
  149. let valuesSelected = [];
  150. $('#addtocart').removeClass('disabled');
  151. $('.msgErrorDetailsItem').addClass('d-none');
  152. $('.borderselector').each(function (i,el){
  153. let val = $(el).html();
  154. valuesSelected.push(val.trim());
  155. });
  156. let labelRecord = {};
  157. for(let label in labels){
  158. labelRecord[label] = false;
  159. }
  160. console.log(labelRecord);
  161. specification_wrap_action.add();
  162. console.log(group);
  163. let unFiler = null;
  164. for(let i=0;i<itemtagscombinationRes.length;i++){
  165. const currItem = itemtagscombinationRes[i];
  166. const {tagnames} = currItem;
  167. let isTrue = true;
  168. let labelFilter = labelRecord;
  169. for(let i=0;i<valuesSelected.length;i++){
  170. if(!tagnames.includes(valuesSelected[i])){
  171. isTrue = false
  172. unFiler = labelFilter;
  173. break;
  174. }
  175. labelFilter[group[valuesSelected[i]]] = true;
  176. }
  177. if(isTrue){
  178. console.log(itemtagscombinationRes[i],'itemtagscombinationRes[i]');
  179. const {quantity} = itemtagscombinationRes[i];
  180. if(quantity>=$('#quantity').val() && quantity>=1){
  181. currentComb = itemtagscombinationRes[i];
  182. // $('.productname').html(itemtagscombinationRes[i].itemname)
  183. // $('.price').html(itemtagscombinationRes[i].pricelist);
  184. // location.hash = itemtagscombinationRes[i].itemname.trim();
  185. // appendSpecs(itemtagscombinationRes[i].itemid)
  186. history.replaceState(null, null, `#itemid=${itemtagscombinationRes[i].itemid}#${itemtagscombinationRes[i].itemname.trim()}`);
  187. // $('#skudetailitem').val(itemtagscombinationRes[i].sku)
  188. // $('.des_productdes').html(itemtagscombinationRes[i].itemdescription);
  189. updateDetailsByObj(itemtagscombinationRes[i]);
  190. }else{
  191. debugger;
  192. let name = itemtagscombinationRes[i].itemname.split('-')[0];
  193. $('#addtocart').addClass('disabled');
  194. let itemsName = name;
  195. $('.borderselector').each(function (i,e){
  196. itemsName += ` ${$(e).html().trim()}`;
  197. });
  198. updateItemId(null);
  199. specification_wrap_action.remove();
  200. $('#skudetailitem').val(-1)
  201. $('.productname').html(itemsName)
  202. history.replaceState(null, null, `#`);
  203. $('.msgErrorDetailsItem').removeClass('d-none');
  204. $('.des_productdes').html('');
  205. $('.price').html('');
  206. }
  207. initAddToCart()
  208. return;
  209. }
  210. }
  211. let name = itemtagscombinationRes[0].itemname.split('-')[0];
  212. let itemsName = name;
  213. $('.borderselector').each(function (i,e){
  214. itemsName += `- ${$(e).html().trim()}`;
  215. })
  216. specification_wrap_action.remove();
  217. $('.productname').html(itemsName)
  218. $('#skudetailitem').val(-1)
  219. $('.price').html('');
  220. $('#addtocart').addClass('disabled');
  221. $('.msgErrorDetailsItem').removeClass('d-none');
  222. initAddToCart()
  223. }
  224. function getAllCombination(){
  225. const currClickAttr = $(currentClick).html().trim();
  226. let isThere = [];
  227. for(let item in isThereInLabel){
  228. let {tagnames} = isThereInLabel[item];
  229. if(tagnames.includes(currClickAttr)){
  230. isThere.push( isThereInLabel[item])
  231. }
  232. }
  233. let selected = [];
  234. $('.borderselector').each(function (i,e){
  235. selected.push($(e).html().trim())
  236. });
  237. // console.log(selected);
  238. // console.log(isThere);
  239. let ishashObj = {};
  240. for(let i=0;i<isThere.length;i++){
  241. let {tagnames,tagids} = isThere[i];
  242. tagnames = tagnames.split('|');
  243. tagids = tagids.split('|');
  244. for(let j=0;j<tagnames.length;j++){
  245. if(!ishashObj[tagnames[j]]){
  246. ishashObj[tagnames[j]] = tagids[j];
  247. }
  248. }
  249. }
  250. // console.log(ishashObj);
  251. // for(let i=0;i<itemtagscombinationRes.length;i++){
  252. // }
  253. }
  254. async function getProductDetails(id){
  255. debugger
  256. // let res = await getStatAPIService(`${SERVERNAME}/apis/v4/bizgaze/integrations/products/productattributelist/productid/${id}`);
  257. let resData = await API_SERVICES_ACTIONS.getAPIService(`apis/v4/bizgaze/integrations/products/productattributelist/productid/${id}`,true);
  258. if(resData.isError){
  259. alert(resData.errorMsg.message);
  260. return;
  261. }
  262. let res = resData.response;
  263. res = JSON.parse(res.result);
  264. // console.log(res);
  265. if(!res) return;
  266. let html = '';
  267. for(let i=0;i<res.length;i++){
  268. const currItem = res[i];
  269. // parenttagid means under what label
  270. // tag id means it know unquie id
  271. const {parentattribute,parenttagid,tagid,attribute,productid} = currItem;
  272. if(labels[parentattribute]){
  273. labels[parentattribute].items.push({
  274. attribute,
  275. tagid:tagid
  276. });
  277. }else{
  278. labels[parentattribute] = {
  279. id:parenttagid,
  280. items:[{
  281. attribute,
  282. tagid:tagid
  283. }]
  284. };
  285. html += getParentHTML(parentattribute,productid,parenttagid,i==0);
  286. }
  287. }
  288. $('#spec-container-details').html(html);
  289. // console.log(labels,'labels');
  290. let childIds = [];
  291. for(let item in labels){
  292. let {id,items} = labels[item];
  293. // $(`[data-tagid=${id}]`)
  294. let html = '';
  295. for(let i=0;i<items.length;i++){
  296. const {attribute,tagid} = items[i]
  297. childIds.push(tagid);
  298. group[attribute] = item;
  299. html += getChildHTML(tagid,attribute,i==0);
  300. }
  301. $(`[data-tagid="${id}"]`).html(html);
  302. }
  303. itemtagscombinationRes = await API_SERVICES_ACTIONS.getAPIService(`apis/v4/bizgaze/integrations/products/itemtagscombination/productid/${id}`,true);
  304. itemtagscombinationRes = JSON.parse(itemtagscombinationRes.response.result);
  305. console.log(itemtagscombinationRes);
  306. let valuesSelected = [];
  307. $('.borderselector').each(function (i,el){
  308. let val = $(el).html();
  309. valuesSelected.push(val.trim());
  310. });
  311. debugger;
  312. let searchParams = window.location.search.split("&");
  313. let skuId = window.location.hash.split('#')[1].split('=')[1];
  314. // skuId = skuId.split('=')[1]
  315. let isGo = true;
  316. for(let i=0;i<itemtagscombinationRes.length;i++){
  317. const currItem = itemtagscombinationRes[i];
  318. const {tagnames,sku,itemid} = currItem;
  319. isThereInLabel[tagnames] = itemtagscombinationRes[i];
  320. let isTrue = true;
  321. if(isGo){
  322. // for(let i=0;i<valuesSelected.length;i++){
  323. // if(!tagnames.includes(valuesSelected[i])){
  324. // isTrue = false
  325. // }
  326. // }
  327. if(skuId != itemid){
  328. isTrue = false
  329. }
  330. if(isTrue){
  331. debugger;
  332. console.log(itemtagscombinationRes[i],'itemtagscombinationRes[i]');
  333. currentComb = itemtagscombinationRes[i];
  334. // location.hash = itemtagscombinationRes[i].itemname.trim();
  335. // $('.productname').html(itemtagscombinationRes[i].itemname);
  336. // $('#skudetailitem').val(itemtagscombinationRes[i].sku)
  337. // $('.price').html(itemtagscombinationRes[i].pricelist)
  338. // debugger;
  339. // appendSpecs(itemtagscombinationRes[i].itemid);
  340. // $('.des_productdes').html(itemtagscombinationRes[i].itemdescription);
  341. updateDetailsByObj(itemtagscombinationRes[i]);
  342. isGo = false;
  343. let {tagnames,tagids,pricelist} = itemtagscombinationRes[i];
  344. tagids = tagids.split('|');
  345. tagnames = tagnames.split('|');
  346. $('.optiontaganwi').removeClass('borderselector');
  347. for(let i=0;i<tagids.length;i++){
  348. $(`[data-tagiditem="${tagids[i]}"]`).addClass('borderselector');
  349. }
  350. // tagiditem
  351. }
  352. }
  353. }
  354. if(isGo){
  355. console.log(itemtagscombinationRes[0]);
  356. let {tagnames,tagids,pricelist} = itemtagscombinationRes[0];
  357. tagids = tagids.split('|');
  358. tagnames = tagnames.split('|');
  359. $('.optiontaganwi').removeClass('borderselector');
  360. currentComb = itemtagscombinationRes[0];
  361. // updateItemId(itemtagscombinationRes[0].itemid);
  362. for(let i=0;i<tagids.length;i++){
  363. $(`[data-type="${tagids[i]}-${tagnames[i]}"]`).addClass('borderselector');
  364. }
  365. // $('.price').html(pricelist);
  366. // $('.des_productdes').html(itemtagscombinationRes[0].itemdescription);
  367. // appendSpecs(itemtagscombinationRes[0].itemid);
  368. updateDetailsByObj(itemtagscombinationRes[0]);
  369. //console.log(childIds);
  370. //
  371. }
  372. //getAllCombination(valuesSelected)
  373. // for(let i=0;i<itemtagscombinationRes.length;i++){
  374. // const {tagnames} = itemtagscombinationRes[i];
  375. // }
  376. $('#spec-container-details').children().slice(1).find('.specsmallcard').removeClass('specsmallcard');
  377. addeventlisteners();
  378. return;
  379. // let resTwo = await postStatAPIService(`${SERVERNAME}/apis/v4/bizgaze/integrations/products/itemtagscombination/productid/${id}`);
  380. //let html = '';
  381. let attributesObj = {};
  382. for(let i=0;i<res.length;i++){
  383. const {parentattribute,tagid,productid} = res[i];
  384. // let attributes = await postStatAPIService(`${SERVERNAME}/apis/v4/bizgaze/integrations/products/productattributelist/parenttagid/${tagid}`);
  385. // attributesObj[tagid] = JSON.parse(attributes.data.result);
  386. // console.log(attributes.data.result,tagid);
  387. html += getParentHTML(parentattribute,productid,tagid,i==0);
  388. }
  389. $('#spec-container-details').html(html);
  390. // let attributes = await postStatAPIService(`${SERVERNAME}/apis/v4/bizgaze/integrations/products/productattributelist/parenttagid/${tagid}`);
  391. return;
  392. for(let attr in attributesObj){
  393. const arr = attributesObj[attr];
  394. let html = '';
  395. for(let i=0;i<arr.length;i++){
  396. const {attribute,tagid} = arr[i];
  397. html += getChildHTML(tagid,attribute,i==0);
  398. }
  399. $(`[data-tagid="${attr}"]`).html(html);
  400. }
  401. let defaultValues = await postStatAPIService(`${SERVERNAME}/apis/v4/bizgaze/integrations/products/sellableitemlist/productid/${id}`);
  402. defaultValues = JSON.parse(defaultValues.data.result);
  403. let {itemname,price} = defaultValues[0];
  404. $('.productname').html(itemname);
  405. $('.price').html(price)
  406. $('#spec-container-details').children().slice(1).find('.specsmallcard').removeClass('specsmallcard');
  407. // getQuantityById(id)
  408. }
  409. }
  410. startDetails();