import {Component, OnInit, ChangeDetectionStrategy, Input} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {tap} from 'rxjs/operators';
import {
  TestingInformationService
} from '../../../../../../core/service/testing-information/testing-information.service';
import {BaseList} from "../../../../../../core/base/base-list";

@Component({
  selector: 'app-setup-period-by-subject-list',
  templateUrl: './setup-period-by-subject-list.component.html',
  styleUrls: ['./setup-period-by-subject-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SetupPeriodBySubjectListComponent  extends BaseList implements OnInit {
  testInforId: any;
  subjectList: any = [];
  column = [
    'subject_code',
    'subject_name_th',
    'add_date_status',
    '4'
  ];
  reload: boolean;
  constructor(
    private router: Router,
    private activeRouter: ActivatedRoute,
    private TestinforSV: TestingInformationService,
  ) {
    super();
  }

  ngOnInit(): void {
    this.activeRouter.params.pipe(
      // tap(x => x.id ? this.state = 'edit' : this.state = 'add'),
      tap(x => x.testInforId ? this.testInforId = x.testInforId : this.testInforId = null),
      tap(x =>
      this.TestinforSV.get(x.testInforId).pipe(
        tap(y => {
          const data: any = y;
          this.subjectList = this.updateMatTable(data.testing_subjects ? data.testing_subjects : []);
          this.reload = false;
          setTimeout(() => {
            this.reload = true;
          }, 2000);
        })
      ).subscribe()
      ),
    ).subscribe();
  }

  onEdit(id){
    this.router.navigate([`special-period/view/${this.testInforId}/`, id]);
  }

}
