import { Component, Input, inject, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ParentApiService } from '../../core/parent-api.service';
import { I18nService } from '../../core/i18n.service';
import { EventAdminView } from '../../core/models';
/** Pestaña Eventos: exámenes y deberes con fecha, por niño. */
@Component({
selector: 'app-events-tab',
imports: [FormsModule],
template: `
@for (e of events(); track e.id) {
{{ e.type === 'EXAM' ? '📋' : '📎' }}
{{ i18n.label(e.titleEs, e.titleCa) }} · {{ e.date }}
} @empty {
{{ i18n.t('none') }}
}
`,
})
export class EventsTabComponent {
@Input({ required: true }) set childId(value: number) {
this._childId = value;
this.reload();
}
private _childId!: number;
private readonly api = inject(ParentApiService);
protected readonly i18n = inject(I18nService);
protected readonly events = signal([]);
protected type: 'EXAM' | 'HOMEWORK' = 'EXAM';
protected date = '';
protected titleEs = '';
protected titleCa = '';
private reload(): void {
this.api.listEvents(this._childId).subscribe((l) => this.events.set(l));
}
add(): void {
const icon = this.type === 'EXAM' ? '📋' : '📎';
const color = this.type === 'EXAM' ? '#EC8FA4' : '#5B8DEF';
this.api
.createEvent({
childId: this._childId,
date: this.date,
type: this.type,
titleEs: this.titleEs,
titleCa: this.titleCa,
icon,
color,
})
.subscribe(() => {
this.titleEs = this.titleCa = this.date = '';
this.reload();
});
}
remove(e: EventAdminView): void {
this.api.deleteEvent(e.id).subscribe(() => this.reload());
}
}