Geen omschrijving
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 16KB

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