import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, ReactiveFormsModule, Validators, } from '@angular/forms'; import { ActivatedRoute, Router, } from '@angular/router'; import { AppHeader } from '../../share/header/app-header'; import { AppSideMenu } from '../../share/side-menu/app-side-menu'; import { DankaService } from '../../services/dankaService'; import { KakochoService } from '../../services/kakocho-service'; import { FamilyService } from '../../services/family-service'; import { Danka } from '../../models/danka'; import { Kakocho } from '../../models/kakocho'; import { Family } from '../../models/family'; @Component({ selector: 'app-kakocho-edit', imports: [ AppHeader, AppSideMenu, ReactiveFormsModule, ], templateUrl: './kakocho-edit.html', styleUrl: './kakocho-edit.scss', }) export class KakochoEdit implements OnInit { danka?: Danka; kakocho?: Kakocho; sourceFamily?: Family; kakochoForm: FormGroup; dankaId: string | undefined; returnTab: 'family' | 'kakocho' = 'kakocho'; constructor( private fb: FormBuilder, private dankaService: DankaService, private kakochoService: KakochoService, private familyService: FamilyService, private route: ActivatedRoute, private router: Router, ) { this.kakochoForm = this.fb.group({ name: ['', Validators.required], furigana: [''], relationship: [''], kaimyo: [''], deathDate: [''], ageAtDeath: [''], note: [''], }); } ngOnInit(): void { this.init(); } async init(): Promise { const dankaId = this.route.snapshot.params['dankaId']; const familyId = this.route.snapshot.queryParams['familyId']; const kakochoId = this.route.snapshot.params['kakochoId']; this.dankaId = dankaId; if (dankaId) { this.danka = await this.dankaService.getDankaById(dankaId); } if (familyId) { this.sourceFamily = await this.familyService.getFamilyById(familyId); this.returnTab = 'family'; } // 編集モード if (kakochoId) { this.kakocho = await this.kakochoService.getKakochoById(kakochoId); if (this.kakocho) { this.kakochoForm.patchValue({ name: this.kakocho.name, furigana: this.kakocho.furigana, relationship: this.kakocho.relationship, kaimyo: this.kakocho.kaimyo, deathDate: this.kakocho.deathDate, ageAtDeath: this.kakocho.ageAtDeath, note: this.kakocho.note, }); } } // 新規(家族からの引き継ぎ) else if (this.sourceFamily) { this.kakochoForm.patchValue({ name: this.sourceFamily.name, furigana: this.sourceFamily.furigana, relationship: this.sourceFamily.relationship, }); } } async saveKakocho(): Promise { // form値取得 const formValue = this.kakochoForm.value; // 編集 if (this.kakocho) { const updatedKakocho: Kakocho = { ...this.kakocho, ...formValue, }; await this.kakochoService.updateKakocho( updatedKakocho ); } else { // 新規追加 const newKakocho: Kakocho = { id: crypto.randomUUID(), dankaId: this.danka?.id ?? '', familyId: this.sourceFamily?.id ?? '', name: formValue.name, furigana: formValue.furigana, relationship: formValue.relationship, kaimyo: formValue.kaimyo, deathDate: formValue.deathDate, ageAtDeath: formValue.ageAtDeath, note: formValue.note, }; this.kakochoService.addKakocho( newKakocho ); if (this.sourceFamily) { // await this.familyService.deleteFamily(this.sourceFamily.id); this.sourceFamily.death = true; await this.familyService.saveFamily(this.sourceFamily); } } // 一覧へ戻る this.router.navigate([ '/danka-detail', this.danka?.id, ], { queryParams: { tab: 'kakocho' } }); } deleteKakocho(): void { if (!this.kakocho) { return; } this.kakochoService.deleteKakocho(this.kakocho.id); this.router.navigate(['/danka-detail', this.danka?.id], { queryParams: { tab: 'kakocho' } }); } cancelKakochoEdit() { this.router.navigate(['/danka-detail', this.danka?.id], { queryParams: { tab: this.returnTab } }); } }