import { Component, inject } from '@angular/core'; import { FormBuilder, FormGroup, FormControl, ReactiveFormsModule, Validators, } from '@angular/forms'; import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import { AppHeader } from '../../share/header/app-header'; import { AppSideMenu } from '../../share/side-menu/app-side-menu'; import { FamilyService } from '../../services/family-service'; import { Danka } from '../../models/danka'; import { Family } from '../../models/family'; @Component({ selector: 'app-family-edit', imports: [AppHeader, AppSideMenu, ReactiveFormsModule], templateUrl: './family-edit.html', styleUrl: './family-edit.scss', }) export class FamilyEdit { danka: Danka | undefined; family: Family | undefined; families: Family[] = []; dankaId: string = ''; familyId: string | null = null; relationMode: string = ''; baseFamilyId: string = ''; constructor( private familyService: FamilyService, private route: ActivatedRoute, private router: Router, ) { this.dankaId = this.route.snapshot.params['dankaId']; this.familyId = this.route.snapshot.params['familyId']; this.families = this.familyService.getFamiliesByDankaId(this.dankaId); this.relationMode = this.route.snapshot.queryParamMap.get('relationMode') ?? ''; this.baseFamilyId = this.route.snapshot.queryParamMap.get('baseFamilyId') ?? ''; if (this.familyId) { this.family = this.familyService.getFamilyById(this.familyId); if (this.family) { this.familyForm.patchValue({ furigana: this.family.furigana, name: this.family.name, relationship: this.family.relationship, birthDate: this.family.birthDate, note: this.family.note, fatherId: this.family.fatherId, motherId: this.family.motherId, spouseId: this.family.spouseId, gender: this.family.gender, }); } } if (!this.familyId && this.relationMode === 'spouse') { this.familyForm.patchValue({ spouseId: this.baseFamilyId, }); } if (!this.familyId && this.relationMode === 'child') { const baseFamily = this.familyService.getFamilyById(this.baseFamilyId); if (baseFamily?.gender === 'male') { this.familyForm.patchValue({ fatherId: this.baseFamilyId, }); } if (baseFamily?.gender === 'female') { this.familyForm.patchValue({ motherId: this.baseFamilyId, }); } } } familyForm = new FormGroup({ furigana: new FormControl('', [Validators.required]), name: new FormControl('', [Validators.required]), relationship: new FormControl(''), birthDate: new FormControl('', Validators.required), note: new FormControl(''), fatherId: new FormControl(''), motherId: new FormControl(''), spouseId: new FormControl(''), gender: new FormControl('unknown'), }); getAgeText() { const birthDate = this.familyForm.get('birthDate')?.value; if (!birthDate) { return '生年月日を入力すると自動計算されます'; } const birth = new Date(birthDate); const today = new Date(); let age = today.getFullYear() - birth.getFullYear(); const thisYearsBirthday = new Date(today.getFullYear(), birth.getMonth(), birth.getDate()); if (thisYearsBirthday > today) { age--; } return `${age}歳`; } getFamilyOptions(): Family[] { return this.families.filter((family) => family.id !== this.familyId); } saveFamily() { if (this.familyForm.invalid) { return; } const familyId = this.familyId ?? Date.now().toString(); const dankaId = this.dankaId; const formValue = this.familyForm.value; const updatedFamily = { id: familyId, dankaId: dankaId, furigana: formValue.furigana?.trim() ?? '', name: formValue.name?.trim() ?? '', relationship: formValue.relationship?.trim() ?? '', birthDate: formValue.birthDate?.trim() ?? '', note: formValue.note?.trim() ?? '', fatherId: this.familyForm.value.fatherId ?? '', motherId: this.familyForm.value.motherId ?? '', spouseId: this.familyForm.value.spouseId ?? '', gender: this.familyForm.value.gender ?? 'unknown', }; this.familyService.saveFamily(updatedFamily); this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } }); } cancelFamilyEdit(): void { const dankaId = this.dankaId; this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } }); } deleteFamily() { const dankaId = this.dankaId; if (!this.familyId) { return; } this.familyService.deleteFamily(this.familyId); this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } }); } setAsHouseholder() {} }