import { Component, Input, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { IonicModule, ModalController, AlertController, ToastController } from '@ionic/angular'; import { EventoAcademico } from '../../services/data'; import { EventosService } from '../../services/eventos.service'; import { CapacitorCalendar } from '@ebarooni/capacitor-calendar'; @Component({ selector: 'app-evento-modal', templateUrl: './evento-modal.component.html', styleUrls: ['./evento-modal.component.scss'], standalone: true, imports: [CommonModule, IonicModule, FormsModule] }) export class EventoModalComponent implements OnInit { @Input() materiaCodigo!: string; @Input() materiaNombre!: string; @Input() eventoExistente?: EventoAcademico; tipo: 'Parcial' | 'TP' | 'Final' | 'Otro' = 'Parcial'; fecha: string = new Date().toISOString(); aula: string = ''; link: string = ''; notas: string = ''; isEdit: boolean = false; constructor( private modalCtrl: ModalController, private eventosService: EventosService, private alertCtrl: AlertController, private toastCtrl: ToastController ) { } ngOnInit() { if (this.eventoExistente) { this.isEdit = true; this.tipo = this.eventoExistente.tipo; this.fecha = this.eventoExistente.fecha; this.aula = this.eventoExistente.aula || ''; this.link = this.eventoExistente.link || ''; this.notas = this.eventoExistente.notas || ''; } } dismiss() { this.modalCtrl.dismiss(); } async guardar() { const nuevoEvento: EventoAcademico = { id: this.isEdit ? this.eventoExistente!.id : Date.now().toString(), materiaCodigo: this.materiaCodigo, tipo: this.tipo, fecha: this.fecha, aula: this.aula, link: this.link, notas: this.notas }; await this.eventosService.saveEvento(nuevoEvento); this.modalCtrl.dismiss(nuevoEvento, 'saved'); } async eliminar() { if (this.eventoExistente) { const alert = await this.alertCtrl.create({ header: 'Eliminar Evento', message: '¿Estás seguro que querés eliminar este hito de tu agenda?', buttons: [ { text: 'Cancelar', role: 'cancel' }, { text: 'Eliminar', role: 'destructive', handler: async () => { await this.eventosService.deleteEvento(this.eventoExistente!.id); this.modalCtrl.dismiss(null, 'deleted'); } } ] }); await alert.present(); } } async exportarCalendario() { try { const startDate = new Date(this.fecha); const endDate = new Date(startDate.getTime() + 2 * 60 * 60 * 1000); // +2 horas por defecto let descString = this.notas || ''; if (this.link) descString += `\n\nLink de cursada: ${this.link}`; await CapacitorCalendar.createEventWithPrompt({ title: `${this.tipo} de ${this.materiaNombre}`, description: descString, location: this.aula ? `Aula ${this.aula}, Universidad de la Ciudad` : 'Universidad de la Ciudad', startDate: startDate.getTime(), endDate: endDate.getTime(), isAllDay: false }); } catch (e) { console.error('Error abriendo enlace de Calendario:', e); const toast = await this.toastCtrl.create({ message: 'No pudimos abrir tu Calendario. Verificá si tenés una app instalada.', duration: 2500, color: 'warning' }); toast.present(); } } }