import {Component, OnInit, ChangeDetectionStrategy, Input, OnChanges, ChangeDetectorRef} from '@angular/core';
import {BaseList} from '../../../../../../core/base/base-list';
import {MatDialog} from '@angular/material/dialog';
import {DialogChangeSeatComponent} from '../../dialog/dialog-change-seat/dialog-change-seat.component';
import {VEligibleCandidatesService} from '../../../../../../core/service/testing-register/v-eligible-candidates.service';
import {tap} from 'rxjs/operators';
import {DialogChangeRoomComponent} from '../../dialog/dialog-change-room/dialog-change-room.component';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-candidate-list-list',
  templateUrl: './candidate-list-list.component.html',
  styleUrls: ['./candidate-list-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CandidateListListComponent extends BaseList implements OnInit, OnChanges {
  @Input() subjectList: any = [];
  column = [
    'student_code',
    'examinee_full_name',
    'branch_name_th',
    'testing_field_name_th',
    'testing_room_name_th',
    'testing_seat',
    'subject_code',
    'testing_datetime',
    'period_id',
    'testing_register_approved'
  ];
  constructor(
    private router: Router,
    public activeRouter: ActivatedRoute,
    private dialog: MatDialog,
    private cdf: ChangeDetectorRef,
    private VECandidateSV: VEligibleCandidatesService
  ) {
    super();
  }

  ngOnInit(): void {
  }

  ngOnChanges(){
    this.subjectList = this.updateMatTable(this.subjectList ? this.subjectList : []);
  }

  openDialog(id){
    console.log(id);
    const dialogRef = this.dialog.open(DialogChangeSeatComponent, {
      width: '500px',
      height: '460px',
      data: id
    });
    dialogRef.afterClosed().pipe(
      tap(x => {
        this.activeRouter.params.pipe(
          tap(y => console.log(x.id)),
          tap(y => {
            this.subjectList = this.VECandidateSV.getAllByTestinUid(x.id);
          }),
          tap(y => this.cdf.detectChanges())

        ).subscribe();
      })
    ).subscribe();
  }

  openDialogChangeRoom(id){
    console.log(id);
    const dialogRef = this.dialog.open(DialogChangeRoomComponent, {
      width: '500px',
      height: '400px',
      data: id
    });
    dialogRef.afterClosed().pipe(
      tap(x => {
        this.VECandidateSV.getAll().pipe(
          tap(y => console.log(y)),
          tap(y => this.subjectList = y),
          tap(y => this.cdf.detectChanges())
        ).subscribe();
      }),
    ).subscribe();
  }

}
