import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute } from '@angular/router';
import { throwError } from 'rxjs';
import { catchError, filter, switchMap, tap } from 'rxjs/operators';
import { BaseSubList } from 'src/app/core/base/base-sub-list';
import { GenerateGroupSeatDetailService } from 'src/app/core/service/student/generate-group-seat-detail.service';
import { StudentGenerateSeatService } from 'src/app/core/service/student/student-generate-seat.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { TestingInformationService } from 'src/app/core/service/testing-information/testing-information.service';
import Swal from 'sweetalert2';
import { StudentDialogComponent } from '../student-dialog/student-dialog.component';

@Component({
  selector: 'app-create-seat',
  templateUrl: './create-seat.component.html',
  styleUrls: ['./create-seat.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CreateSeatComponent extends BaseSubList implements OnInit {
  studentList: any = [];
  data;
  testingInfomationData;
  totalSeatGenerate;
  totalStudent;
  paramsData;
  constructor(
    public activeRoute: ActivatedRoute,
    private generateGroupSeatDetailSV: GenerateGroupSeatDetailService,
    private testingInfomationSV: TestingInformationService,
    private studentGenerateSeatSV: StudentGenerateSeatService,
    private swSV: SweetalertService,
    private cdr: ChangeDetectorRef,
    private dialog: MatDialog
  ) {
    super(activeRoute)
   }

  ngOnInit(): void {
    this.activeRoute.params.pipe(
      tap(x => this.paramsData = x),
      tap(x => this.totalSeatGenerate = x.total_seat_generate),
      tap(x => this.totalStudent = x.total_student),
      switchMap(x => this.generateGroupSeatDetailSV.getDetail(x)),
      tap(x => this.studentList = this.updateMatTable(x)),
      tap(x => this.data = x[0]),
      switchMap(x => this.testingInfomationSV.searchForSeat(`?registration_type=1&testing_information_uid=${x[0].testing_information_uid}`)),
      tap(x => this.testingInfomationData = x[0]),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  add(){
    Swal.fire({
      title: 'กำลังสร้างที่นั่งสอบ...',
      html: 'กรุณารอ...',
      allowEscapeKey: false,
      allowOutsideClick: false,
      didOpen: () => {
        Swal.showLoading()
      }
    });

    this.studentGenerateSeatSV.createSeat(this.paramsData).pipe(
      tap(x => Swal.hideLoading()),
      tap(x => Swal.close()),
      tap(x => this.swSV.success('สร้างที่นั่งสอบสำเร็จ')),
      tap(x => window.history.back()),
      catchError(err => {
        Swal.hideLoading()
        Swal.close()
        this.swSV.errText(err.error)
        return throwError(err)
      })
    ).subscribe()
  }

  addStudent(){
    const obj = {
      params_data: this.paramsData,
      student_list: this.studentList.data
    }
    const dialogRef = this.dialog.open(StudentDialogComponent, {
      width: '500px',
      data: obj,
    });

    dialogRef.afterClosed().pipe(
      switchMap(x => this.generateGroupSeatDetailSV.getDetail(this.paramsData)),
      tap(x => this.studentList = this.updateMatTable(x)),
      tap(x => this.data = x[0]),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

}
