import { Component, Input, computed, inject, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ParentApiService } from '../../core/parent-api.service';
import { I18nService } from '../../core/i18n.service';
import { RoutineView } from '../../core/models';
import { EmojiPickerComponent } from '../../shared/emoji-picker.component';
import { ColorPickerComponent } from '../../shared/color-picker.component';
const DAYS: { key: string; es: string; ca: string }[] = [
{ key: 'MONDAY', es: 'Lunes', ca: 'Dilluns' },
{ key: 'TUESDAY', es: 'Martes', ca: 'Dimarts' },
{ key: 'WEDNESDAY', es: 'Miércoles', ca: 'Dimecres' },
{ key: 'THURSDAY', es: 'Jueves', ca: 'Dijous' },
{ key: 'FRIDAY', es: 'Viernes', ca: 'Divendres' },
];
/** Pestaña Rutinas de tarde por día de la semana, de un niño. */
@Component({
selector: 'app-routines-tab',
imports: [FormsModule, EmojiPickerComponent, ColorPickerComponent],
template: `
{{ i18n.t('tabRoutines') }}:
@for (d of days; track d.key) {
}
Nueva rutina
@for (r of routinesForDay(); track r.id; let i = $index; let last = $last) {
{{ r.icon }}
{{ i18n.label(r.labelEs, r.labelCa) }}
} @empty {
{{ i18n.t('none') }}
}
`,
styles: [
`
.field { margin-bottom: var(--space-4); }
.field__label { display: flex; align-items: center; gap: 6px; font-weight: 700; color: var(--text-1); margin-bottom: 6px; }
.field__info { cursor: help; color: var(--text-4); font-size: 0.85rem; }
.field__input {
font-family: var(--font-body); font-size: 1rem; padding: 12px 14px;
border: 2px solid var(--border-2); border-radius: var(--radius-sm);
background: var(--surface); color: var(--text-strong); width: 100%; box-sizing: border-box;
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
@media (max-width: 520px) { .grid2 { grid-template-columns: 1fr; } }
`,
],
})
export class RoutinesTabComponent {
@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 days = DAYS;
protected readonly day = signal('MONDAY');
protected readonly routines = signal([]);
protected readonly routinesForDay = computed(() =>
this.routines().filter((r) => r.dayOfWeek === this.day()),
);
protected readonly routineIcons = ['🎒', '🥪', '📝', '🎹', '🍽️', '🛁', '🦷', '📚', '🧹', '🐕', '🛏️', '🎨', '⚽', '🧺'];
protected icon = '';
protected labelEs = '';
protected labelCa = '';
protected color = '#A78BD0';
private reload(): void {
this.api.listRoutines(this._childId).subscribe((l) => this.routines.set(l));
}
add(): void {
const order = this.routinesForDay().length;
this.api
.createRoutine({
childId: this._childId,
dayOfWeek: this.day(),
labelEs: this.labelEs,
labelCa: this.labelCa,
icon: this.icon,
color: this.color,
orderIndex: order,
})
.subscribe(() => {
this.labelEs = this.labelCa = this.icon = '';
this.reload();
});
}
/** Mueve una rutina del día arriba (-1) o abajo (+1) y persiste el nuevo orden. */
move(index: number, delta: number): void {
const list = [...this.routinesForDay()];
const target = index + delta;
if (target < 0 || target >= list.length) {
return;
}
[list[index], list[target]] = [list[target], list[index]];
this.api.reorderRoutines(list.map((r) => r.id)).subscribe(() => this.reload());
}
remove(r: RoutineView): void {
this.api.deleteRoutine(r.id).subscribe(() => this.reload());
}
}