暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

productdetails.js 19KB

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