| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- 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() {}
- }
|