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

@Component({
  selector: 'app-seat-change-list',
  templateUrl: './seat-change-list.component.html',
  styleUrls: ['./seat-change-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SeatChangeListComponent  extends BaseList implements OnInit, OnChanges {
  @Input() testingInfoData:any ;
  @Input() subjectList: any = [];
  column = ['1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'];
  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 : []);
  }

  isAvailable(data){
    const noShowStatusArray = ['03','04','06','10','13']
    if(!data.test_status_code) return true
    if(noShowStatusArray.some(x => x == data.test_status_code)) return false
    return true
  }

  openDialog(id){
    const dialogRef = this.dialog.open(DialogChangeSeatComponent, {
      width: '500px',
      height: '460px',
      data: id
    });
    dialogRef.afterClosed().pipe(
      switchMap(x => {
        return this.VECandidateSV.getAllByTestinUid(this.testingInfoData.testing_information_uid).pipe(
          tap(x => this.subjectList.data = x),
          tap(x => this.cdf.detectChanges())
        )
      })
    ).subscribe();
  }

  openDialogChangeRoom(id){
    const dialogRef = this.dialog.open(DialogChangeRoomComponent, {
      width: '500px',
      height: '400px',
      data: id
    });
    dialogRef.afterClosed().pipe(
      switchMap(x => {
        return this.VECandidateSV.getAllByTestinUid(this.testingInfoData.testing_information_uid).pipe(
          tap(x => this.subjectList.data = x),
          tap(x => this.cdf.detectChanges())
        )
      })
    ).subscribe();
  }

  changeExamDay(data){
    console.log(data)
    const obj = {
      data: data,
      information: this.testingInfoData
    }
    const dialogRef = this.dialog.open(ChangeExamDayDialogComponent, {
      width: '800px',
      height: '600px',
      data: obj
    });
    dialogRef.afterClosed().pipe(
      switchMap(x => {
        return this.VECandidateSV.getAllByTestinUid(this.testingInfoData.testing_information_uid).pipe(
          tap(x => this.subjectList.data = x),
          tap(x => this.cdf.detectChanges())
        )
      })
    ).subscribe()
  }

}
