events.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. var remainingMenu = 0;
  2. var package_id = 1;
  3. $(document).ready(function() {
  4. var events = null;
  5. var budss_qty = 0;
  6. var budss_event_date = '';
  7. var budss_event_time = '';
  8. var timer = 0;
  9. var remainingTime = 0;
  10. //Clear session
  11. window.localStorage.clear();
  12. window.localStorage.setItem('budss_product_id', 1);
  13. //Initialize transaction
  14. $.get('/api/initialize/',function(response) {
  15. if (response !== '' && response !== undefined) {
  16. window.localStorage.setItem('budss_uid', response);
  17. }
  18. else {
  19. console.warning('Unable to initialize the transaction...');
  20. }
  21. });
  22. $('#field').on('focus', function() {
  23. if ($('#field').val() === '0') {
  24. $('#field').val('');
  25. }
  26. });
  27. $('#field').on('blur', function() {
  28. if ($('#field').val() === '') {
  29. $('#field').val(0);
  30. }
  31. });
  32. $('#field').on('change', function() {
  33. budss_qty = $('#field').val();
  34. window.localStorage.setItem('budss_qty', budss_qty);
  35. });
  36. $('#Date-20').on('change', function() {
  37. budss_event_date = $('#Date-20').val();
  38. $('#days-of-the-week-2').html('');
  39. window.localStorage.setItem('budss_event_date', budss_event_date);
  40. $('#days-of-the-week-2').append($('<option>', {value: '', text: 'Select a time...'}));
  41. $.each(events, function(index, value) {
  42. if (value['datum'] === budss_event_date) {
  43. $('#days-of-the-week-2').append($('<option>', {value: value['id'], text: value['time']}));
  44. }
  45. });
  46. });
  47. $('#days-of-the-week-2').on('change', function() {
  48. budss_event_time = $('#days-of-the-week-2').val();
  49. window.localStorage.setItem('budss_event_time', budss_event_time);
  50. });
  51. $('#bookingStep1Btn').on('click', function(event) {
  52. const participants = window.localStorage.getItem('budss_qty');
  53. if (participants !== null && participants > 0) {
  54. $('#step1Error').hide();
  55. let enabledDates = [];
  56. $.getJSON('/api/getevents/?package_id=' + package_id + '&participants=' + participants, function(data) {
  57. events = data;
  58. $.each(data, function(index, value) {
  59. enabledDates.push(value['date']);
  60. });
  61. $("#Date-20").datepicker({
  62. minDate: 0,
  63. dateFormat: 'yy-mm-dd',
  64. beforeShowDay: checkDate
  65. });
  66. function checkDate(in_date) {
  67. in_date = in_date.getFullYear() + '-' + (in_date.getMonth() + 1) + '-' + in_date.getDate();
  68. if (enabledDates.includes(in_date)) {
  69. return [true, "av", "Available"];
  70. }
  71. else {
  72. return [false, "notav", 'Not Available'];
  73. }
  74. }
  75. $('.step-1').hide();
  76. $('.step-2').show();
  77. setTimeout(() => {
  78. const viewTop = $('.cta-wrap').offset().top;
  79. $('html, body').animate({
  80. scrollTop: viewTop-200
  81. }, 100);
  82. },100);
  83. });
  84. }
  85. else {
  86. $('#step1Error').show();
  87. }
  88. });
  89. $('#bookingStepBack').on('click', function(event) {
  90. $('.step-2').hide();
  91. $('.step-1').show();
  92. });
  93. $('#bookingStepBack2').on('click', function(event) {
  94. $('.step-3').hide();
  95. $('.step-2').show();
  96. });
  97. $('#bookingStep2Btn').on('click', function(event) {
  98. const participants = window.localStorage.getItem('budss_qty');
  99. const eventid = window.localStorage.getItem('budss_event_time');
  100. const uid = window.localStorage.getItem('budss_uid');
  101. remainingMenu = parseInt(participants);
  102. console.log('Remaining menu items 1: ', remainingMenu);
  103. $.get('/api/reservation/?event_id='+eventid+'&qty='+participants+'&uuid='+uid, function(result) {
  104. const response = JSON.parse(result);
  105. remainingTime = parseInt(response['timer']);
  106. timer = setInterval(function() {
  107. remainingTime--;
  108. window.localStorage.setItem('remainingTime', remainingTime);
  109. if (remainingTime < 60) {
  110. $('.cart_timer').css('color','#ff0000');
  111. }
  112. if (remainingTime === 0) {
  113. window.location.reload();
  114. }
  115. const minutes = Math.floor(remainingTime / 60);
  116. const remainingSeconds = remainingTime % 60;
  117. $('.cart_timer').html(`${minutes}:${remainingSeconds.toString().padStart(2, '0')}`);
  118. },1000);
  119. });
  120. $('#menuContainer').html('');
  121. if (budss_event_date !== '' && budss_event_time !== '') {
  122. $('.step-2').hide();
  123. let menu = events.map(function(item) {
  124. if (item['id'] === parseInt(budss_event_time)) {
  125. return item['products'];
  126. }
  127. });
  128. menu = menu.filter(function( element ) {
  129. return element !== undefined;
  130. });
  131. $.each(menu[0], function(index, value) {
  132. if (value['currency'] === 'huf') {
  133. $.post('/api/getproducts', value, function(response) {
  134. $('#menuContainer').append(response);
  135. });
  136. }
  137. });
  138. const participants = window.localStorage.getItem('budss_qty');
  139. $('.menu_counter').html(participants);
  140. if (!localStorage.getItem('budss_products')) {
  141. localStorage.setItem('budss_products', JSON.stringify({}));
  142. }
  143. $('.step-3').show();
  144. }
  145. else {
  146. $('#step2Error').show();
  147. }
  148. });
  149. $('#bookingStep3Btn').on('click', function(event) {
  150. $('#step3Error').hide();
  151. if ($('.menu_counter').html() === '0') {
  152. window.location.href='/order';
  153. }
  154. else {
  155. $('#step3Error').show();
  156. }
  157. });
  158. });
  159. function decProductQty(id) {
  160. const actual = parseInt($('.menu_'+id).val());
  161. const participants = parseInt(window.localStorage.getItem('budss_qty'));
  162. let products = JSON.parse(window.localStorage.getItem('budss_products'));
  163. if (actual > 0 && remainingMenu <= participants) {
  164. const newValue = actual - 1;
  165. const remaining = participants - newValue;
  166. const hash = 'id_'+id;
  167. remainingMenu++;
  168. if (!products[hash]) {
  169. products[hash] = 0;
  170. }
  171. products[hash] = newValue;
  172. window.localStorage.setItem('budss_products', JSON.stringify(products));
  173. $('.menu_counter').html(remainingMenu.toString());
  174. $('.menu_'+id).val(newValue);
  175. }
  176. if (remainingMenu === 0) {
  177. $('#bookingStep3Btn').css('background-color', '#e5ff29');
  178. }
  179. else {
  180. $('#bookingStep3Btn').css('background-color', '#afafaf');
  181. }
  182. }
  183. function incProductQty(id) {
  184. const actual = parseInt($('.menu_'+id).val());
  185. const participants = parseInt(window.localStorage.getItem('budss_qty'));
  186. let products = JSON.parse(window.localStorage.getItem('budss_products'));
  187. if (remainingMenu <= participants && remainingMenu > 0) {
  188. const newValue = actual + 1;
  189. const remaining = participants - newValue;
  190. const hash = 'id_'+id;
  191. remainingMenu--;
  192. if (!products[hash]) {
  193. products[hash] = 0;
  194. }
  195. products[hash] = newValue;
  196. window.localStorage.setItem('budss_products', JSON.stringify(products));
  197. $('.menu_counter').html(remainingMenu.toString());
  198. $('.menu_'+id).val(newValue);
  199. }
  200. if (remainingMenu === 0) {
  201. $('#bookingStep3Btn').css('background-color', '#e5ff29');
  202. }
  203. else {
  204. $('#bookingStep3Btn').css('background-color', '#afafaf');
  205. }
  206. }
  207. function setService(obj, id) {
  208. $('.service-card_wrapper').removeClass('active-service');
  209. $(obj).addClass('active-service');
  210. package_id = id;
  211. window.localStorage.setItem('budss_product_id', id);
  212. }