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 19KB

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