reposiciones.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Get references to the HTML elements
  2. const form = document.getElementById('form');
  3. const steps = Array.from(form.querySelectorAll('.step'));
  4. const nextButton = document.getElementById('next-button');
  5. const prevButton = document.getElementById('prev-button');
  6. let currentStep = 0;
  7. // #clave_profesor on change => show step 2
  8. const clave_profesor = document.getElementById('clave_profesor');
  9. const horario_reponer = document.getElementById('horario_reponer');
  10. const fechas_clase = document.getElementById('fechas_clase');
  11. const fecha_reponer = $('#fecha_reponer');
  12. const hora_reponer = $('#hora_reponer');
  13. const minutos_reponer = $('#minutos_reponer');
  14. clave_profesor.addEventListener('change', async () => {
  15. const step2 = document.getElementById('step-2');
  16. clave_profesor.disabled = true;
  17. // get option which value is the same as clave_profesor.value
  18. const option = document.querySelector(`option[value="${clave_profesor.value}"]`);
  19. // make a form data with #form
  20. const profesor_id = document.getElementById('profesor_id');
  21. profesor_id.value = option.dataset.id;
  22. const formData = new FormData(form);
  23. const response = await fetch(`./action/action_horario_profesor.php`, {
  24. method: 'POST',
  25. body: formData
  26. });
  27. const data = await response.json();
  28. if (data['success'] === false) {
  29. const message = "Hubo un error al obtener los horarios del profesor.";
  30. const title = 'Error';
  31. const color = 'danger';
  32. triggerMessage(message, title, color);
  33. return;
  34. }
  35. const horarios = data.data;
  36. const initial = document.createElement('option');
  37. initial.value = '';
  38. initial.textContent = 'Seleccione un horario';
  39. initial.selected = true;
  40. initial.disabled = true;
  41. horario_reponer.innerHTML = '';
  42. horario_reponer.appendChild(initial);
  43. horarios.forEach((horario) => {
  44. const dias = ['Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado', 'Domingo'];
  45. const option = document.createElement('option');
  46. option.value = `${horario.id}`;
  47. // materia máx 25 caracteres, if materia.length > 25 then slice(0, 20)
  48. const max = 25;
  49. option.textContent = `${horario.materia.slice(0, max) + (horario.materia.length > max ? '...' : '')} - Grupo: ${horario.grupo} - ${horario.hora.slice(0, 5)}-${horario.hora_final.slice(0, 5)} - Salon: ${horario.salon} - ${horario.dia}`;
  50. option.dataset.materia = `${horario.materia}`;
  51. option.dataset.grupo = `${horario.grupo}`;
  52. option.dataset.hora = `${horario.hora.slice(0, 5)}`; // slice(0, 5) => HH:MM
  53. option.dataset.hora_final = `${horario.hora_final.slice(0, 5)}`;
  54. option.dataset.salon = `${horario.salon}`;
  55. option.dataset.dia = `${horario.dia}`;
  56. option.dataset.id = `${horario.id}`;
  57. horario_reponer.appendChild(option);
  58. });
  59. currentStep = 1;
  60. step2.style.display = 'block';
  61. prevButton.disabled = false;
  62. });
  63. // disable clave_profesor
  64. // from second step to first step
  65. prevButton.addEventListener('click', () => {
  66. const inputs = [clave_profesor, horario_reponer, fechas_clase, fecha_reponer, hora_reponer];
  67. switch (currentStep) {
  68. case 1:
  69. case 2:
  70. case 3:
  71. const step = document.getElementById(`step-${currentStep + 1}`);
  72. step.style.display = 'none';
  73. inputs[currentStep - 1].disabled = false;
  74. inputs[currentStep - 1].value = '';
  75. if (--currentStep === 0) {
  76. prevButton.disabled = true;
  77. }
  78. break;
  79. case 4:
  80. const step5 = document.getElementById('step-5');
  81. step5.style.display = 'none';
  82. fecha_reponer.prop('disabled', false);
  83. fecha_reponer.val('');
  84. hora_reponer.parent().removeClass('disabled');
  85. hora_reponer.siblings('.datalist-input').text('hh');
  86. hora_reponer.val('');
  87. minutos_reponer.parent().removeClass('disabled');
  88. minutos_reponer.siblings('.datalist-input').text('mm');
  89. minutos_reponer.val('');
  90. currentStep--;
  91. break;
  92. }
  93. nextButton.disabled = true;
  94. });
  95. // #horario_reponer on change => show step 3
  96. horario_reponer.addEventListener('change', async () => {
  97. const selected = horario_reponer.querySelector(`option[value="${horario_reponer.value}"]`);
  98. horario_reponer.title = `Materia: ${selected.dataset.materia} - Grupo: ${selected.dataset.grupo} - Horario: ${selected.dataset.hora}-${selected.dataset.hora_final} - Salon: ${selected.dataset.salon} - Día: ${selected.dataset.dia}`;
  99. const step3 = document.getElementById('step-3');
  100. horario_reponer.disabled = true;
  101. // make a form data with #form
  102. const response = await fetch(`./action/action_fechas_clase.php?horario_id=${horario_reponer.value}`, {
  103. method: 'GET',
  104. });
  105. const data = await response.json();
  106. if (data['success'] === false) {
  107. const message = "Hubo un error al obtener las fechas de clase.";
  108. const title = 'Error';
  109. const color = 'danger';
  110. triggerMessage(message, title, color);
  111. return;
  112. }
  113. const meses = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
  114. const fechas = data.data;
  115. const initial = document.createElement('option');
  116. initial.value = '';
  117. initial.textContent = 'Seleccione la fecha de la falta';
  118. initial.selected = true;
  119. initial.disabled = true;
  120. fechas_clase.innerHTML = '';
  121. fechas_clase.appendChild(initial);
  122. fechas_clase.title = 'Seleccione la fecha de la falta';
  123. fechas.forEach((fecha) => {
  124. const option = document.createElement('option');
  125. option.value = `${fecha}`;
  126. option.textContent = `${fecha.dia_mes} de ${meses[fecha.month - 1]} de ${fecha.year}`;
  127. fechas_clase.appendChild(option);
  128. });
  129. step3.style.display = 'block';
  130. currentStep = 2;
  131. });
  132. // #fechas_clase on change => show step 4
  133. fechas_clase.addEventListener('change', () => {
  134. const step4 = document.getElementById('step-4');
  135. step4.style.display = 'block';
  136. fechas_clase.disabled = true;
  137. currentStep = 3;
  138. });
  139. // when both #fecha_reponer and #hora_reponer are selected => show step 5
  140. const lastStep = () => {
  141. // timeout to wait for the value to be set
  142. setTimeout(() => {
  143. if (fecha_reponer.val() !== '' && hora_reponer.val() !== '' && minutos_reponer.val() !== '') {
  144. const step5 = document.getElementById('step-5');
  145. step5.style.display = 'block';
  146. // disable both
  147. fecha_reponer.prop('disabled', true);
  148. hora_reponer.parent().addClass('disabled');
  149. minutos_reponer.parent().addClass('disabled');
  150. const nextButton = document.getElementById('next-button');
  151. // remove property disabled
  152. nextButton.removeAttribute('disabled');
  153. currentStep = 4;
  154. }
  155. }, 100);
  156. };
  157. fecha_reponer.on('change', lastStep);
  158. // on click on the sibling ul>li of #hora_reponer and #minutos_reponer
  159. hora_reponer.siblings('ul').children('li').on('click', lastStep);
  160. minutos_reponer.siblings('ul').children('li').on('click', lastStep);
  161. // Initialize the form
  162. hideSteps();
  163. showCurrentStep();
  164. function hideSteps() {
  165. steps.forEach((step) => {
  166. step.style.display = 'none';
  167. });
  168. }
  169. function showCurrentStep() {
  170. steps[currentStep].style.display = 'block';
  171. prevButton.disabled = currentStep === 0;
  172. }
  173. function handleSubmit(event) {
  174. event.preventDefault();
  175. // Handle form submission
  176. // You can access the form data using the FormData API or serialize it manually
  177. }
  178. export {};