auditoría.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { createApp, reactive } from 'https://unpkg.com/petite-vue?module';
  2. const store = reactive({
  3. loading: false,
  4. current: {
  5. comentario: '',
  6. clase_vista: null,
  7. empty: '',
  8. },
  9. facultades: {
  10. data: [],
  11. async fetch() {
  12. this.data = [];
  13. const res = await fetch('action/action_facultad.php');
  14. this.data = await res.json();
  15. },
  16. },
  17. filters: {
  18. facultad_id: null,
  19. fecha: null,
  20. fecha_inicio: null,
  21. fecha_fin: null,
  22. profesor: null,
  23. estados: [],
  24. switchFecha: false,
  25. switchFechas() {
  26. $(function () {
  27. store.filters.fecha_inicio = store.filters.fecha_fin = store.filters.fecha = null;
  28. $("#fecha, #fecha_inicio, #fecha_fin").datepicker({
  29. minDate: -3,
  30. maxDate: new Date(),
  31. dateFormat: "yy-mm-dd",
  32. showAnim: "slide",
  33. });
  34. const fecha = $("#fecha"), inicio = $("#fecha_inicio"), fin = $("#fecha_fin");
  35. inicio.on("change", function () {
  36. store.filters.fecha_inicio = inicio.val();
  37. fin.datepicker("option", "minDate", inicio.val());
  38. });
  39. fin.on("change", function () {
  40. store.filters.fecha_fin = fin.val();
  41. inicio.datepicker("option", "maxDate", fin.val());
  42. });
  43. fecha.on("change", function () {
  44. store.filters.fecha = fecha.val();
  45. });
  46. });
  47. }
  48. },
  49. estados: {
  50. data: [],
  51. async fetch() {
  52. this.data = [];
  53. const res = await fetch('action/action_estado_supervisor.php');
  54. this.data = await res.json();
  55. },
  56. getEstado(id) {
  57. return this.data.find((estado) => estado.estado_supervisor_id === id);
  58. },
  59. printEstados() {
  60. if (store.filters.estados.length > 0)
  61. document.querySelector('#estados').innerHTML = store.filters.estados.map((estado) => `<span class="mx-2 badge badge-${store.estados.getEstado(estado).estado_color}">
  62. <i class="${store.estados.getEstado(estado).estado_icon}"></i> ${store.estados.getEstado(estado).nombre}
  63. </span>`).join('');
  64. else
  65. document.querySelector('#estados').innerHTML = `Todos los registros`;
  66. }
  67. },
  68. toggle(arr, element) {
  69. const newArray = arr.includes(element) ? arr.filter((item) => item !== element) : [...arr, element];
  70. // if all are selected, then unselect all
  71. if (newArray.length === this.estados.data.length)
  72. return [];
  73. return newArray;
  74. },
  75. });
  76. createApp({
  77. store,
  78. get clase_vista() {
  79. return store.current.clase_vista;
  80. },
  81. registros: {
  82. data: [],
  83. async fetch() {
  84. this.loading = true;
  85. this.data = [];
  86. const res = await fetch('action/action_auditoria.php');
  87. this.data = await res.json();
  88. this.loading = false;
  89. },
  90. invertir() {
  91. this.data = this.data.reverse();
  92. },
  93. mostrarComentario(registro_id) {
  94. const registro = this.data.find((registro) => registro.registro_id === registro_id);
  95. store.current.comentario = registro.comentario;
  96. $('#ver-comentario').modal('show');
  97. },
  98. get relevant() {
  99. /*
  100. facultad_id: null,
  101. fecha: null,
  102. fecha_inicio: null,
  103. fecha_fin: null,
  104. profesor: null,
  105. asistencia: null,
  106. estado_id: null,
  107. if one of the filters is null, then it is not relevant
  108. */
  109. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] || store.filters[filtro]?.length > 0);
  110. return this.data.filter((registro) => {
  111. return filters.every((filtro) => {
  112. switch (filtro) {
  113. case 'fecha':
  114. return registro.registro_fecha_ideal === store.filters[filtro];
  115. case 'fecha_inicio':
  116. return registro.registro_fecha_ideal >= store.filters[filtro];
  117. case 'fecha_fin':
  118. return registro.registro_fecha_ideal <= store.filters[filtro];
  119. case 'profesor':
  120. const textoFiltro = store.filters[filtro].toLowerCase();
  121. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  122. const clave = registro.profesor_clave.toLowerCase();
  123. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  124. console.log(clave, filtroClave);
  125. return clave.includes(filtroClave);
  126. }
  127. else {
  128. const nombre = registro.profesor_nombre.toLowerCase();
  129. return nombre.includes(textoFiltro);
  130. }
  131. case 'facultad_id':
  132. return registro.facultad_id === store.filters[filtro];
  133. case 'estados':
  134. if (store.filters[filtro].length === 0)
  135. return true;
  136. return store.filters[filtro].includes(registro.estado_supervisor_id);
  137. default:
  138. return true;
  139. }
  140. });
  141. });
  142. },
  143. },
  144. get profesores() {
  145. return this.registros.data.map((registro) => ({
  146. profesor_id: registro.profesor_id,
  147. profesor_nombre: registro.profesor_nombre,
  148. profesor_correo: registro.profesor_correo,
  149. profesor_clave: registro.profesor_clave,
  150. profesor_grado: registro.profesor_grado,
  151. })).sort((a, b) => a.profesor_nombre.localeCompare(b.profesor_nombre));
  152. },
  153. async mounted() {
  154. await this.registros.fetch();
  155. await store.facultades.fetch();
  156. await store.estados.fetch();
  157. store.filters.switchFechas();
  158. }
  159. }).mount('#app');