Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

productdetails.js 18KB

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