faltas.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { createApp, reactive } from 'https://unpkg.com/petite-vue?module';
  2. const filter = reactive({
  3. facultad: -1,
  4. profesor: '',
  5. porcentaje: 0,
  6. faltas: 0,
  7. tipoFaltas: true,
  8. });
  9. const app = createApp({
  10. filter,
  11. facultades: [],
  12. profesores: [],
  13. faltas: [],
  14. mensaje: {
  15. titulo: '',
  16. texto: '',
  17. },
  18. async refresh() {
  19. if (filter.facultad == -1 || (filter.porcentaje < 10 && filter.faltas < 1)) {
  20. console.log('Facultad: ', filter.facultad, 'Porcentaje: ', filter.porcentaje, 'Faltas: ', filter.faltas);
  21. return;
  22. }
  23. $('#cargando').modal('show');
  24. try {
  25. this.faltas = await fetch(`action/profesor_faltas.php?facultad=${this.filter.facultad}&${this.filter.tipoFaltas ? 'supervisor' : 'profesor'}&${this.filter.faltas > 0 ? 'faltas' : 'porcentaje'}=${this.filter.faltas > 0 ? this.filter.faltas : this.filter.porcentaje}`).then(res => res.json());
  26. if (this.faltas.error) {
  27. $('.modal#mensaje').modal('show');
  28. this.mensaje.titulo = 'Información';
  29. this.mensaje.texto = this.faltas.error;
  30. }
  31. } catch (error) {
  32. $('.modal#mensaje').modal('show');
  33. this.mensaje.titulo = 'Error';
  34. this.mensaje.texto = 'No se pudo cargar los datos';
  35. }
  36. finally {
  37. $('#cargando').modal('hide');
  38. }
  39. },
  40. async toExcel() {
  41. if (filter.facultad == -1 || filter.porcentaje < 10) {
  42. return;
  43. }
  44. $('#cargando').modal('show');
  45. try {
  46. const response = await fetch(`export/faltas_excel.php`, {
  47. method: 'POST',
  48. body: JSON.stringify(this.faltas.map(falta => ({
  49. 'profesor_clave': falta.profesor.profesor_clave,
  50. 'profesor_correo': falta.profesor.profesor_correo,
  51. 'profesor_nombre': falta.profesor.profesor_nombre,
  52. 'faltas': falta.faltas,
  53. 'porcentaje': `${falta.porcentaje}%`,
  54. 'total': falta.total,
  55. }))),
  56. })
  57. const blob = await response.blob();
  58. window.saveAs(blob, `faltas_${this.facultades.find(facultad => facultad.facultad_id == filter.facultad).facultad_nombre}_${new Date().toISOString().slice(0, 10)}.xlsx`);
  59. } catch (error) {
  60. $('.modal#mensaje').modal('show');
  61. this.mensaje.titulo = 'Error';
  62. this.mensaje.texto = 'No se pudo cargar los datos';
  63. console.log('Error: ', error);
  64. }
  65. finally {
  66. $('#cargando').modal('hide');
  67. }
  68. },
  69. async mounted() {
  70. try {
  71. this.facultades = await fetch('action/action_facultad.php').then(res => res.json());
  72. this.profesores = await fetch('action/action_profesor.php').then(res => res.json());
  73. } catch (error) {
  74. $('.modal#mensaje').modal('show');
  75. this.mensaje.titulo = 'Error';
  76. this.mensaje.texto = 'No se pudo cargar los datos';
  77. console.log('Error: ', error);
  78. }
  79. }
  80. }).mount('#app');