<commit_before>import {Component, Inject, Input, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import 'rxjs/add/operator/startWith';
import {Episode} from '../../model/Episode';
import 'rxjs/add/operator/mergeMap';
import {IcpcCode} from '../../model/IcpcCode';
import {Patient} from '../../model/Patient';

@Component({
  selector: 'icpc-create-subvisit-dialog',
  templateUrl: 'create-subvisit-dialog.html',
})
export class CreateSubVisitDialogComponent implements OnInit {

  @Input()
  public patient: Patient;
  @Input()
  public episodes: Episode[];

  @Input()
  public episode: Episode;
  public dialogTitle: string;

  public selectedTab: number = 0;
  public formGroup: FormGroup;

  constructor(public dialogRef: MatDialogRef<CreateSubVisitDialogComponent>,
              @Inject(MAT_DIALOG_DATA) public data: any,
              fb: FormBuilder) {
    this.episode = data.episode;
    this.patient = data.patient;
    this.episodes = data.episodes;
    this.dialogTitle = data.dialogTitle;
    this.formGroup = fb.group({
      date: new FormControl(null, [Validators.required]),
      episode: new FormControl(),
      diagnosis: new FormControl(),
      reasons: new FormControl(),
      actions: new FormControl()
    });
    this.formGroup.valueChanges.subscribe(v => console.log(v));
  }

  public ngOnInit() {
    if (this.episode) {
      this.formGroup.patchValue({
        episode: this.episode
      });
    }
  }

  public updateActions(codes: IcpcCode[]) {
    let actions = this.actionsField;
    actions.setValue(actions.value ? actions.value.concat(codes) : codes);
  }

  public get reasonsField() {
    return this.formGroup.get('reasons');
  }

  public get diagnosisField() {
    return this.formGroup.get('diagnosis');
  }

  public get actionsField() {
    return this.formGroup.get('actions');
  }

  public changeSelectedTab(index: number) {
    this.selectedTab = index;
    this.actionsField.setValue([]);
    this.updateActions([this.diagnosisField.value, ...this.reasonsField.value]);
  }

  public get previousActionCodes() {
    return [this.diagnosisField.value, ...(this.reasonsField.value || [])];
  }

  public get previousReasonCodes() {
    return [...(this.actionsField.value || [])];
  }

  public get previousDiagnosisCodes() {
    return [...(this.actionsField.value || [])];
  }

  public nextTab() {
    this.selectedTab++;
  }

  public get diagnosesEnabled() {
    return !!(this.reasonsField.value || []).length;
  }

  public get actionsEnabled() {
    return !!this.diagnosisField.value && !!(this.reasonsField.value || []).length;
  }

  public get canSelectNext() {
    switch (this.selectedTab) {
      case 0:
        return !!(this.reasonsField.value || []).length;
      case 1:
        return !!this.diagnosisField.value;
      case 2:
        return false;
    }<commit_msg>FIX: fixed list to show previous tab lists<commit_after>import {Component, Inject, Input, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import 'rxjs/add/operator/startWith';
import {Episode} from '../../model/Episode';
import 'rxjs/add/operator/mergeMap';
import {IcpcCode} from '../../model/IcpcCode';
import {Patient} from '../../model/Patient';

@Component({
  selector: 'icpc-create-subvisit-dialog',
  templateUrl: 'create-subvisit-dialog.html',
})
export class CreateSubVisitDialogComponent implements OnInit {

  @Input()
  public patient: Patient;
  @Input()
  public episodes: Episode[];

  @Input()
  public episode: Episode;
  public dialogTitle: string;

  public selectedTab: number = 0;
  public formGroup: FormGroup;

  constructor(public dialogRef: MatDialogRef<CreateSubVisitDialogComponent>,
              @Inject(MAT_DIALOG_DATA) public data: any,
              fb: FormBuilder) {
    this.episode = data.episode;
    this.patient = data.patient;
    this.episodes = data.episodes;
    this.dialogTitle = data.dialogTitle;
    this.formGroup = fb.group({
      date: new FormControl(null, [Validators.required]),
      episode: new FormControl(),
      diagnosis: new FormControl(),
      reasons: new FormControl(),
      actions: new FormControl()
    });
    this.formGroup.valueChanges.subscribe(v => console.log(v));
  }

  public ngOnInit() {
    if (this.episode) {
      this.formGroup.patchValue({
        episode: this.episode
      });
    }
  }

  public updateActions(codes: IcpcCode[]) {
    let actions = this.actionsField;
    actions.setValue(actions.value ? actions.value.concat(codes) : codes);
  }

  public get reasonsField() {
    return this.formGroup.get('reasons');
  }

  public get diagnosisField() {
    return this.formGroup.get('diagnosis');
  }

  public get actionsField() {
    return this.formGroup.get('actions');
  }

  public changeSelectedTab(index: number) {
    this.selectedTab = index;
    this.actionsField.setValue([]);
    this.updateActions([this.diagnosisField.value, ...this.reasonsField.value]);
  }

  public get previousActionCodes() {
    return (this.reasonsField.value || []).concat(this.diagnosisField.value);
  }

  public get previousReasonCodes() {
    return [];
  }

  public get previousDiagnosisCodes() {
    return this.reasonsField.value || [];
  }

  public nextTab() {
    this.selectedTab++;
  }

  public get diagnosesEnabled() {
    return !!(this.reasonsField.value || []).length;
  }

  public get actionsEnabled() {
    return !!this.diagnosisField.value && !!(this.reasonsField.value || []).length;
  }

  public get canSelectNext() {
    switch (this.selectedTab) {
      case 0:
        return !!(this.reasonsField.value || []).length;
      case 1:
        return !!this.diagnosisField.value;
      case 2:
        return false;
    }
{'fraction_non_alphanumeric': 0.14107323658454268, 'fraction_numerical': 0.0013499831252109348, 'mean_word_length': 3.433059087509349, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836719', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1804, 'n_words': 392}